socketserver
一共有这么几个类:
TCP:
class socketserver.TCPServer(server_address, RequestHandlerClass,bind_and_activate=true)
UDP:
class socketserver.UDPServer(server_address, RequestHandlerClass,bind_and_activate=true)
for Unix:不常用
class socketserver.UnixStreamServer(server_address, RequestHandlerClass,bind_and_activate=true)
class socketserver.UnixDatagramServer(server_address, RequestHandlerClass,bind_and_activate=true)
一共有这么几个类:
TCP:
class socketserver.TCPServer(server_address, RequestHandlerClass,bind_and_activate=true)
UDP:
class socketserver.UDPServer(server_address, RequestHandlerClass,bind_and_activate=true)
for Unix:不常用
class socketserver.UnixStreamServer(server_address, RequestHandlerClass,bind_and_activate=true)
class socketserver.UnixDatagramServer(server_address, RequestHandlerClass,bind_and_activate=true)
继承关系:
BaseServer
TCPServer UnixStreamServer
UDPServer UnixDatagramServer
均继承了 BaseServer
1. 创建一个请求处理类,并且这个类要继承BaseRequestHandler, 并且要重写父类的handle()方法;
handle()方法处理所有与客户端的交互
2. 必须实例化TCPServer,并且传递server ip和上面创建的请求处理类给这个这个TCPserver
3. server.handle_request() # 只处理一个请求
server.serve_forever() # 处理多个请求,永远执行
案例1:
import socketserver
class MyTCPHandler(socketserver.BaseRequestHandler):
def handle(self):
while True:
try:
self.data = self.request.recv(1024).strip()
# if not self.data: #代表客户端端开了
# print("客户端断开了!")
# break
print(self.client_address)
print(self.data)
self.request.send(self.data.upper())
except ConnectionResetError as e:
print("客户端断开了!",e)
break
if __name__ =="__main__":
host, port = "127.0.0.1", 9999
#server = socketserver.TCPServer((host, port), MyTCPHandler) #一对一的单并发
server = socketserver.ThreadingTCPServer((host, port), MyTCPHandler) #多线程多并发
#server = socketserver.ForkingTCPServer((host, port), MyTCPHandler) #多进程多并发
server.serve_forever()
客户端:
import socket
conn = socket.socket() # 声明socket类型,同时生成socket连接对象
conn.connect(("127.0.0.1", 9999))
while True:
while True:
msg = input(">>:").strip()
if msg: break
conn.send(msg.encode("utf-8")) #只能发bytes类型
data = conn.recv(1024) #1024 字节 = 1kb
print(data.decode())
conn.close()
在自定义的处理类中可以重写这3个方法:
1. self.setup() #请求进来前处理的动作;
2. self.handle() #请求进来后处理的动作;
3. self.finish() #请求结束后处理的动作;
server.serve_forever() # 处理多个请求,永远执行
案例1:
import socketserver
class MyTCPHandler(socketserver.BaseRequestHandler):
def handle(self):
while True:
try:
self.data = self.request.recv(1024).strip()
# if not self.data: #代表客户端端开了
# print("客户端断开了!")
# break
print(self.client_address)
print(self.data)
self.request.send(self.data.upper())
except ConnectionResetError as e:
print("客户端断开了!",e)
break
if __name__ =="__main__":
host, port = "127.0.0.1", 9999
#server = socketserver.TCPServer((host, port), MyTCPHandler) #一对一的单并发
server = socketserver.ThreadingTCPServer((host, port), MyTCPHandler) #多线程多并发
#server = socketserver.ForkingTCPServer((host, port), MyTCPHandler) #多进程多并发
server.serve_forever()
客户端:
import socket
conn = socket.socket() # 声明socket类型,同时生成socket连接对象
conn.connect(("127.0.0.1", 9999))
while True:
while True:
msg = input(">>:").strip()
if msg: break
conn.send(msg.encode("utf-8")) #只能发bytes类型
data = conn.recv(1024) #1024 字节 = 1kb
print(data.decode())
conn.close()
在自定义的处理类中可以重写这3个方法:
1. self.setup() #请求进来前处理的动作;
2. self.handle() #请求进来后处理的动作;
3. self.finish() #请求结束后处理的动作;
来源:https://www.cnblogs.com/brace2011/p/9291603.html