Python网络编程UDP服务器与客服端简单例子
Posted ㄗòsら顆糖
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python网络编程UDP服务器与客服端简单例子相关的知识,希望对你有一定的参考价值。
[转载] https://blog.csdn.net/hu330459076/article/details/7868028
UDP服务器代码:
#!/usr/bin/env python # -*- coding:UTF-8 -*- from socket import * from time import ctime HOST = ‘127.0.0.1‘ PORT = 21567 BUFSIZE = 1024 ADDR = (HOST,PORT) udpSerSock = socket(AF_INET, SOCK_DGRAM) udpSerSock.bind(ADDR) while True: print ‘wating for message...‘ data, addr = udpSerSock.recvfrom(BUFSIZE) udpSerSock.sendto(‘[%s] %s‘%(ctime(),data),addr) print ‘...received from and retuned to:‘,addr udpSerSock.close()
UDP客服端代码:
#!/usr/bin/env python from socket import * HOST = ‘localhost‘ PORT = 21567 BUFSIZE = 1024 ADDR = (HOST, PORT) udpCliSock = socket(AF_INET, SOCK_DGRAM) while True: data = raw_input(‘>‘) if not data: break udpCliSock.sendto(data,ADDR) data,ADDR = udpCliSock.recvfrom(BUFSIZE) if not data: break print data udpCliSock.close()
以上是关于Python网络编程UDP服务器与客服端简单例子的主要内容,如果未能解决你的问题,请参考以下文章