下面的python代码执行后通过tcp监控8081端口,用于将用户发送的请求字符串转换成大写后返回,如果用户发送的是end,则中断连接
import SocketServer import netstring class MyRequestHandler(SocketServer.BaseRequestHandler): def handle(self): print "From:", self.client_address while 1: rq = netstring.readns(self.request) print rq netstring.writens(self.request, rq.upper()) if rq.lower() == "end": break myServer = SocketServer.TCPServer(('', 8081), MyRequestHandler) myServer.handle_request()