标签:bre net utf8 port 循环 error data ESS from
#客户端
import socket
soc = socket.socket()
soc.connect(('127.0.0.1',8090))
while True:
data = input('>>:')
soc.send(data.encode('utf8'))
data = soc.recv(1024)
print(data)
#服务端
import socket
from multiprocessing import Process
# class pro(Process):
# def __init__(self,conn,addr):
# super().__init__()
# self.conn = conn
# self.addr = addr
# def run(self) -> None:
# while True:
# data = self.conn.recv(1024)
# print(data)
# self.conn.send('ok'.encode('utf8'))
# self.conn.close()
def pro(conn,addr):
print(f'客户{addr}连接了')
while True:
try:
data = conn.recv(1024)
print(data)
conn.send('ok'.encode('utf8'))
except ConnectionResetError:
conn.close()
break
if __name__ == '__main__':
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.bind(('127.0.0.1', 8090))
soc.listen(5)
while True:
conn, addr = soc.accept()
Process(target=pro,args = (conn, addr)).start()
标签:bre net utf8 port 循环 error data ESS from
原文地址:https://www.cnblogs.com/cheng825/p/11508160.html