#服务端
import socket
import threading
# import multiprocessing
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)#面向网络的,面向连接的套接字
server.bind(('127.0.0.1',9527))
server.listen(5)
def action(conn):
while True:
data=conn.recv(1024)
print(data)
conn.send(data)
if __name__ == '__main__':
while True:
conn,addr=server.accept()
print(addr)
print(f'{addr}成功连接')
p=threading.Thread(target=action,args=(conn,))
p.start()
#客户端
import socket
client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect(('127.0.0.1',9527))
while True:
msg=input('发给麦迪文:').strip()
if msg=='q':
print('正在退出!')
break
if not msg:
continue
client.send(msg.encode('utf8'))
data=client.recv(1024)
print(data)
# server端
#这是从麻老师那边抄来的https://www.cnblogs.com/maqiaobin/p/11721972.html,客户端用我的也行
from socket import *
from multiprocessing import Process
server=socket(AF_INET,SOCK_STREAM)
server.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)#头皮发麻,这个我们学过吗
server.bind(('127.0.0.1',9527))
server.listen(5)
def talk(conn,client_addr):
while True:
try:
msg=conn.recv(1024)
if not msg:break
conn.send(msg.upper())#这里的upper其实没必要
except Exception:
break
if __name__ == '__main__':
while True:
conn,client_addr=server.accept()
p=Process(target=talk,args=(conn,client_addr))
p.start()