server.py
import socket
from threading import Thread
def Server():
server = socket.socket()
server.bind(('127.0.0.1', 8000))
server.listen(5)
while True:
conn, addr = server.accept()
Thread(target=talk,args=[conn,]).start()
def talk(connect):
while True:
msg = connect.recv(1024)
print(msg)
if msg.decode('utf-8') == 'bye':
break
answer = "我是server!"
connect.send(answer.encode('utf-8'))
connect.close()
if __name__ == '__main__':
Server()
client.py
import socket
if __name__ == '__main__':
client = socket.socket()
client.connect(('127.0.0.1', 8000))
while True:
send_msg = input("请输入")
client.send(send_msg.encode())
if send_msg == "bye":
break
msg = client.recv(1024)
print(msg.decode('utf-8'))
client.close()
原文地址:https://www.cnblogs.com/pythonPath/p/12399225.html
时间: 2024-11-09 03:09:29