并行的前提是要有多核CPU
#server.py
from multiprocessing import Process
from threading import Thread
import socket
server =socket.socket()
server.bind(('0.0.0.0',8006))
server.listen(1000)
def func(connection):
recv_data=connection.recv(1024)
if recv_data:
print(recv_data.decode('utf-8'))
connection.send(recv_data)
else:
connection.close()
pass
while True:
#多进程,多进程消耗资源大,python框架都是多线程的
t = Thread(target=func, args=(connection,)) # 传给func的位置参数args必须是元祖
t.start()
# #多线程只需要将Process改成Thread
# connection,remote_address=server.accept()
# p=Process(target=func,args=(connection,)) #传给func的位置参数args必须是元祖
# p.start()
#### client.py
import socket
while True:
client=socket.socket()
client.connect(('127.0.0.1', 8001))
msg=input('>>>输入:')
client.send(msg.encode('utf-8'))
print(client.recv(1024).decode('utf-8'))
client.close()