python实现的多线程字符转换成大写的tcp服务端代码,下面的代码用于将用户通过tcp发过来的字符串转换成大写后返回,如果客户端发送过来空字符串,则结束通信
import SocketServer import netstring import sys import string class MyRequestHandler(SocketServer.BaseRequestHandler): def handle(self): print "Connected:", self.client_address while 1: rq = netstring.readns(self.request) print "From:", self.client_address, rq sys.stdout.flush() if rq == "": break netstring.writens(self.request, string.upper(rq)) myServer = SocketServer.ThreadingTCPServer(('', 8081), MyRequestHandler) myServer.serve_forever()