这里定义了一个 TCPServer 和 TCPClient。这里创建一个 TCP
服务程序,服务器会把客户发送过来的字符串加上一个时间戳,然后显示,并返回客户端。主要后面无论如何都要记得
close()关上连接,虽然基本上不会执行那一句。
TCPServer.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
from socket import * from time import ctime HOST = '' PORT = 20000 BUFSIZE = 1024 ADDR = (HOST, PORT) tcpSerSock = socket(AF_INET, SOCK_STREAM) tcpSerSock.bind(ADDR) tcpSerSock.listen(5) while True: print 'waiting for connection...' tcpClientSock,clientAddr = tcpSerSock.accept() print '...connected from :', clientAddr while True: data = tcpClientSock.recv(BUFSIZE) if not data: break print '[%s] %s' % (ctime(), data) tcpClientSock.send('[%s] %s' % (ctime(), data)) tcpClientSock.close() tcpSerSock.close()
|
TCPClient.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
from socket import * HOST = 'localhost' PORT = 20000 BUFSIZE = 1024 ADDR = (HOST, PORT) tcpClientSock = socket(AF_INET, SOCK_STREAM) tcpClientSock.connect(ADDR) while True: data = raw_input('Enter a string your want to send >') if not data: break tcpClientSock.send(data) data = tcpClientSock.recv(BUFSIZE) if not data: break print data tcpClientSock.close()
|