在python中,socketserver是1个已集成的模块,它有以下特点:
服务端:
#!/usr/bin/env python
# _*_ coding=utf⑻ _*_
import SocketServer
class SockHandler(SocketServer.BaseRequestHandler):
def handle(self):
print "接收到客户真个1个连接:",self.client_address
data = 'start'
while len(data):
data = self.request.recv(2048)
self.request.send("返回: " + data)
print "客户端关闭... ..."
serAddr = ("0.0.0.0",8888)
print "等待连接... ..."
server = SocketServer.TCPServer(serAddr,SockHandler)
server.serve_forever()
客户端用nc命令进行测试:
$ nc 192.168.1.95 8888
你好
返回: 你好
okay
返回: okay