python socket代码_python socket 原汁原味代码

python socket代码 摘自 Programming Python 原汁原味代码

客户端

############################################################################

# Server side: open a TCP/IP socket on a port, listen for a message from

# a client, and send an echo reply; this is a simple one-shot listen/reply

# conversation per client, but it goes into an infinite loop to listen for

# more clients as long as this server script runs; the client may run on

# a remote machine, or on same computer if it uses 'localhost' for server

############################################################################

from socket import *                    # get socket constructor and constants

myHost = ''                             # server machine, '' means local host

myPort = 50007                          # listen on a non-reserved port number

sockobj = socket(AF_INET, SOCK_STREAM)       # make a TCP socket object

sockobj.bind((myHost, myPort))               # bind it to server port number

sockobj.listen(5)                            # listen, allow 5 pending connects

while True:                                  # listen until process killed

connection, address = sockobj.accept()   # wait for next client connect

print 'Server connected by', address     # connection is a new socket

while True:

data = connection.recv(1024)         # read next line on client socket

if not data: break                   # send a reply line to the client

connection.send('Echo=>' + data)     # until eof when socket closed

connection.close()

客户端

############################################################################

# Client side: use sockets to send data to the server, and print server's

# reply to each message line; 'localhost' means that the server is running

# on the same machine as the client, which lets us test client and server

# on one machine;  to test over the Internet, run a server on a remote

# machine, and set serverHost or argv[1] to machine's domain name or IP addr;

# Python sockets are a portable BSD socket interface, with object methods

# for the standard socket calls available in the sytstem's C library;

############################################################################

import sys

from socket import *              # portable socket interface plus constants

serverHost = 'localhost'          # server name, or: 'starship.python.net'

serverPort = 50007                # non-reserved port used by the server

message = ['1']           # default text to send to server

if len(sys.argv) > 1:

serverHost = sys.argv[1]                # or server from cmd line arg 1

if len(sys.argv) > 2:                   # or text from cmd line args 2..n

message = sys.argv[2:]              # one message for each arg listed

sockobj = socket(AF_INET, SOCK_STREAM)      # make a TCP/IP socket object

sockobj.connect((serverHost, serverPort))   # connect to server machine and port

for line in message:

sockobj.send(line)                      # send line to server over socket

data = sockobj.recv(1024)               # receive line from server: up to 1k

print 'Client received:', repr(data)    # make sure it is quoted, was `x`

sockobj.close()                             # close socket to send eof to server

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值