标签:put python 通信 客户 rip utf-8 socket client strip
import socket
#基于网络的udp协议的socket
server=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
server.bind(("127.0.0.1",8888))
while 1:
print("开启UDP协议服务端")
from_client_data=server.recvfrom(1024)#阻塞等待客户链接
print(f'来自客户端{from_client_data[1]}:{from_client_data[0].decode("utf-8")}')
toclient=input("请输入内容")
server.sendto(toclient.encode("utf-8"),from_client_data[1])
#基于udp协议的socket无须建立管道,先开启服务端或者客户端都行
#基于udp协议的socket接收一个消息
import socket
#基于网络的udp协议的socket
client=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
while 1:
print("链接进入服务端")
to_server_data=input("请输入发送").strip()
client.sendto(to_server_data.encode("utf-8"), ("127.0.0.1", 8888))
data,addr=client.recvfrom(1024)
print(f'来自服务端{addr}内容:{data.decode("utf-8")}')
标签:put python 通信 客户 rip utf-8 socket client strip
原文地址:https://www.cnblogs.com/SkyRabbit/p/11379268.html