标签:erro 建立连接 连接 ros cli font 并发编程 rom comm
基于协程池 实现并发的套接字通信
客户端:
from socket import * client = socket(AF_INET, SOCK_STREAM) client.connect((‘127.0.0.1‘, 8080)) while True: msg = input(">>>:").strip() if not msg:break client.send(msg.encode("utf-8")) data = client.recv(1024) print(data.decode("utf-8")) client.close()
服务端:
from gevent import monkey;monkey.patch_all() import gevent from gevent.pool import Pool from socket import * def communicate(conn): ‘‘‘ 通信功能 ‘‘‘ while True: try: data = conn.recv(1024) if not data:break conn.send(data.upper()) except ConnectionResetError: break conn.close() def server(ip,port): ‘‘‘ 负责建立连接 accept‘‘‘ server = socket(AF_INET, SOCK_STREAM) server.bind((ip,port)) server.listen(5) while True: conn,addr = server.accept() c = pool.spawn(communicate,conn) server.close() if __name__ == "__main__": pool = Pool(5) g = pool.spawn(server,‘127.0.0.1‘, 8080) g.join()
python 并发编程 基于gevent模块 协程池 实现并发的套接字通信
标签:erro 建立连接 连接 ros cli font 并发编程 rom comm
原文地址:https://www.cnblogs.com/mingerlcm/p/11256942.html