9.16 非阻塞IO模型 cpu占用率過高 服務端: 客戶端; 9.17 I/O多路復用 服務端: from socket import * import select s = socket() s.bind(('127.0.0.1',8080)) s.listen(5) s.setblocking ...
9.16 非阻塞IO模型
cpu占用率過高
服務端:
from socket import * import time s = socket() s.bind(('127.0.0.1',8080)) s.listen(5) s.setblocking(False) #使accept接收不到連接時不在阻塞 r_list=[] while True: try: conn, addr = s.accept() r_list.append(conn) except BlockingIOError: # time.sleep(3) #print('可以去乾其他的活了') #print('rlist: ',len(r_list)) for conn in r_list: try: data=conn.recv(1024) conn.send(data.upper()) except BlockingIOError: continue
客戶端;
from socket import * import os client = socket() client.connect(('127.0.0.1', 8080)) while True: data='%s say hello' %os.getpid() client.send(data.encode('utf-8')) res=client.recv(1024) print(res.decode('utf-8'))
9.17 I/O多路復用
服務端:
from socket import * import select s = socket() s.bind(('127.0.0.1',8080)) s.listen(5) s.setblocking(False) #使accept接收不到連接時不在阻塞 # print(s) r_list=[s,] w_list=[] w_data={} while True: print('被檢測r_list: ',len(r_list)) print('被檢測w_list: ',len(w_list)) #rl中是r_list中建立連接的套接字對象 rl,wl,xl=select.select(r_list,w_list,[],) #r_list=[server,conn] # print('rl: ',len(rl)) #rl=[conn,] # print('wl: ',len(wl)) # 收消息 for r in rl: #r=conn if r == s: conn,addr=r.accept() r_list.append(conn) else: try: data=r.recv(1024) if not data: r.close() r_list.remove(r) continue # r.send(data.upper()) w_list.append(r) w_data[r]=data.upper() except ConnectionResetError: r.close() r_list.remove(r) continue # 發消息 for w in wl: w.send(w_data[w]) w_list.remove(w) w_data.pop(w)View Code
客戶端:
from socket import * import os client = socket() client.connect(('127.0.0.1', 8080)) while True: data='%s say hello' %os.getpid() client.send(data.encode('utf-8')) res=client.recv(1024) print(res.decode('utf-8'))View Code
9.18 socketserver的使用
9.181 基於tcp的socketserver
服務端:
import socketserver # 通信迴圈 class MytcpHandler(socketserver.BaseRequestHandler): def handle(self): while True: try: data = self.request.recv(1024) # 1024 接收數據的最大限制 if not data: break # 針對linux系統 self.request.send(data.upper()) # 註意:收發都是以bytes為單位 except ConnectionResetError: break self.request.close() if __name__ == '__main__': #連接迴圈 server=socketserver.ThreadingTCPServer(('127.0.0.1',8080),MytcpHandler) server.serve_forever() print(server.server_address) print(server.RequestHandlerClass) print(server.socket)
客戶端:
import socket client=socket.socket(socket.AF_INET,socket.SOCK_STREAM) client.connect(('127.0.0.1',8080)) while True: msg=input('>>: ').strip() client.send(msg.encode('utf-8')) data=client.recv(1024) print(data.decode('utf-8')) client.close()
9.182 基於udp的socketserver
服務端:
import socketserver # 通信迴圈 class MyUDPHandler(socketserver.BaseRequestHandler): def handle(self): print(self.request)#(b'13404 hello', <socket.socket fd=460, family=AddressFamily.AF_INET, res=self.request[0]#type=SocketKind.SOCK_DGRAM, proto=0, laddr=('127.0.0.1', 8080)>) print('客戶端發來的數據:',res) self.request[1].sendto(res.upper(),self.client_address) if __name__ == '__main__': #連接迴圈 server=socketserver.ThreadingUDPServer(('127.0.0.1',8080),MyUDPHandler) server.serve_forever()View Code
客戶端:
import socket import os client=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) while True: msg='%s hello' %os.getpid() client.sendto(msg.encode('utf-8'),('127.0.0.1',8080)) res,server_addr=client.recvfrom(1024) print(res)View Code