socketserver并发服务
#重要的模板
import socketserver
class Myserver(socketserver.BaseRequestHandler): #自己定义一个class类
def handle(self): #方法名必须为handle()
conn = self.request #获得conn连接
#此处加入自己的逻辑
#每个Client端连接都从handle()开始执行。
server = socketserver.ThreadingTCPServer(('127.0.0.1',9001),Myserver)
server.serve_forever()
#server端————————————————————————————————————————
import time
import socketserver
class Myserver(socketserver.BaseRequestHandler):
def handle(self):
conn = self.request
while True:
try:
content = conn.recv(1024).decode('utf-8')
conn.send(content.upper().encode('utf-8'))
time.sleep(0.5)
except ConnectionResetError:
break
server = socketserver.ThreadingTCPServer(('127.0.0.1',9001),Myserver)
server.serve_forever()
# import socket
#
# sk = socket.socket()
# sk.bind(('127.0.0.1',9001))
# sk.listen()
# while True:
# conn,_ = sk.accept()
# while True:
# try:
# content = conn.recv(1024).decode('utf-8')
# conn.send(content.upper().encode('utf-8'))
# time.sleep(0.5)
# except ConnectionResetError:
# break
# class BaseRequestHandler:
# def __init__(self):
# self.handle()
# def handle(self):
# pass
#
# class Myserver(BaseRequestHandler):
# def handle(self):
# pass
# my = Myserver()
#client端——————————————————————————————————————
import socket
sk = socket.socket()
sk.connect(('127.0.0.1',9001))
while True:
sk.send(b'hello')
content = sk.recv(1024).decode('utf-8')
print(content)