Python并发编程-协程实现socketserver

时间:2021-04-28 23:56:44
#server.py
from gevent import monkey;monkey.patch_all()
import socket
import gevent
sk = socket.socket() sk.bind(('127.0.0.1',8080))
sk.listen()
def talk(conn):
conn.send(b'hello')
print(conn.recv(1024).decode('utf-8'))
conn.close() while True:
conn,addr = sk.accept()
gevent.spawn(talk,conn)
sk.close() #client.py
import socket sk =socket.socket()
sk.connect(('127.0.0.1',8080))
print(sk.recv(1024).decode('utf-8'))
msg = input('>>>'.encode('utf-8'))
sk.send(msg)
sk.close()