利用线程和进程实现tcp
服务端
from multiprocessing import Process
from threading import Thread
import socket
def school(conn):
while True:
res = conn.recv(1024)
if len(res) == 0:break
res = res.decode('utf-8')
msg = f'{res}已被我校录取'
print(msg)
conn.send(msg.encode("utf-8"))
conn.close()
if __name__ == '__main__':
s = socket.socket()
s.bind(('127.0.0.1',8080))
s.listen(5)
while True:
conn,addr = s.accept()
# t = Thread(target=school,args=(conn,))
t = Process(target=school,args=(conn,))
t.start()
客户端
import socket
s = socket.socket()
s.connect(('127.0.0.1',8080))
while True:
msg = input("your name:")
s.send(msg.encode('utf-8'))
data = s.recv(1024)
print(data.decode('utf-8'))