与c/c++ socket编程对照见http://blog.csdn.net/aspnet_lyc/article/details/38946915
server:
import socketPORT = 9999
BACKLOG = 5
MAXLINE = 1024listenfd = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
listenfd.bind(('',PORT))
listenfd.listen(BACKLOG)while True:connfd, connaddr = listenfd.accept()print 'a new connection'buf = []buf = connfd.recv(MAXLINE)print bufconnfd.send('Hello,this is server')connfd.close()
client:
import socketaddr = '127.0.0.1'
port = 9999
sockfd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockfd.connect((addr, port))
sockfd.send('Hello,this is client')
buf = []while True:recv_data = sockfd.recv(1024)if recv_data:buf.append(recv_data)else:breakdata = ''.join(buf)
print data
sockfd.close()