码迷,mamicode.com
首页 > 编程语言 > 详细

python创建udp服务端和客户端

时间:2018-08-26 15:40:22      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:nbsp   serve   %s   port   code   udp   客户   turn   for   

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()

 

python创建udp服务端和客户端

标签:nbsp   serve   %s   port   code   udp   客户   turn   for   

原文地址:https://www.cnblogs.com/Selling-fish-bears/p/9537350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!