标签:cli lis input 输入 get super sel send elf
import socket
from threading import Thread
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind((‘127.0.0.1‘,8080))
server.listen(5)
# def task(conn,addr):
# while 1:
# try:
# data=conn.recv(1024)
# data_str=data.decode(‘utf-8‘)
# print(f‘从客户端{addr},接收到{data_str}‘)
# conn.send(data.upper())
# except Exception as e:
# print(e)
# break
# conn.close()
class Mythread(Thread):
def __init__(self,conn,addr):
super().__init__()
self.conn=conn
self.addr=addr
def run(self):
while 1:
try:
data=self.conn.recv(1024)
data_str=data.decode(‘utf-8‘)
print(f‘从客户端{self.addr},接收到{data_str}‘)
self.conn.send(data.upper())
except Exception as e:
print(e)
break
self.conn.close()
while 1:
conn, addr = server.accept()
# t=Thread(target=task,args=(conn,addr))
t=Mythread(conn,addr)
t.start()
====================================================================
import socket
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect((‘127.0.0.1‘,8080))
while True:
msg = input(‘请输入:‘).encode(‘utf-8‘)
client.send(msg)
data = client.recv(104).decode(‘utf-8‘)
print(data)
标签:cli lis input 输入 get super sel send elf
原文地址:https://www.cnblogs.com/ccqd/p/14354806.html