首页 > 代码库 > python socketserver并发实例
python socketserver并发实例
#以下为服务端
improt socketservera
class MyTCPHandler(socketserver.BaseRequestHandler):
def handle(self):
while True:
print("New Conn:",self.client_address)
data=http://www.mamicode.com/self.request.recv(1024)
if not data:break
print ("Client Save:",data.decode())
self.request.send(data)
if __name__==‘__main__’:
HOST,POST=‘localhost’,‘13333‘
server=socketserver.ThreadingTCPServer((HOST,PORT),MyTCPHandler)
server.serve_forever()
#以下为客户端
import socket
ip_port=(‘127.0.0.1‘,50007)
sk=socket.socket()
sk.connect(ip_port)
while True:
meg=imput(">>:").strip()
ak.sendall(bytes(meg,‘utf8‘))
server_reply=ak.recv(1024)
print("Server Reply:",str(server_reply,‘utf8‘)
sk.close()
python socketserver并发实例