python创建udp服务端和客户端
程序员文章站
2022-03-04 08:58:02
1.udp服务端server 2.udp客户端client ......
1.udp服务端server
1 from socket import * 2 from time import ctime 3 4 host = '' 5 port = 8888 6 bufsiz = 1024 7 addr = (host, port) 8 9 udpsersock = socket(af_inet, sock_dgram) 10 udpsersock.bind(addr) 11 12 while true: 13 print('waiting for message...') 14 data, addr = udpsersock.recvfrom(bufsiz) 15 16 content = '[%s] %s' % (bytes(ctime(), "utf-8"), data) 17 udpsersock.sendto(content.encode("utf-8"), addr) 18 print('...received from and returned to:', addr) 19 20 udpsersock.close()
2.udp客户端client
1 from socket import * 2 3 host = 'localhost' 4 port = 8888 5 bufsiz = 1024 6 addr = (host, port) 7 8 udpclisock = socket(af_inet, sock_dgram) 9 10 while true: 11 data = input('> ') 12 if not data: 13 break 14 15 udpclisock.sendto(data.encode("utf-8"), addr) 16 data, addr = udpclisock.recvfrom(bufsiz) 17 if not data: 18 break 19 print(data) 20 21 udpclisock.close()
上一篇: php实现查看邮件是否已被阅读的方法
下一篇: python变量(杂)不定时更新