标签:实现 cps tin port and sel 网络编程 种类 ram
socketserver一共有这么几种类型
class socketserver.TCPServer(server_address, RequestHandlerClass, bind_and_activate=True)
class socketserver.UDPServer(server_address, RequestHandlerClass, bind_and_activate=True)
class socketserver.UnixStreamServer(server_address, RequestHandlerClass, bind_and_activate=True) class socketserver.UnixDatagramServer(server_address, RequestHandlerClass,bind_and_activate=True)
让你的socketserver并发起来, 必须选择使用以下一个多并发的类
class socketserver.
ForkingTCPServer
class socketserver.
ForkingUDPServer
class socketserver.
ThreadingTCPServer
class socketserver.
ThreadingUDPServer
用ThreadingTCPServer实现多线程并发。
import socketserver class MyTCPHandler(socketserver.BaseRequestHandler): def handle(self): while True: self.data = self.request.recv(1024).strip() print("{} wrote:".format(self.client_address[0])) print(self.data) self.request.sendall(self.data.upper()) if __name__ == "__main__": HOST, PORT = "localhost", 9999 server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler) server.serve_forever()
以上代码运行结果
import socket client= socket.socket() client.connect((‘localhost‘,9999)) while True: choice=input(‘>>:‘).strip() if len(choice)==0:continue client.send(choice.encode()) recv=client.recv(1024) print(‘recv data:‘,recv)
标签:实现 cps tin port and sel 网络编程 种类 ram
原文地址:http://www.cnblogs.com/xone/p/6136842.html