写个客户端
#!/usr/bin/env python from socket import * HOST = '10.2.167.115' PORT = 20001 BUFSIZE = 1024 ADDR = (HOST, PORT) udpClientSock = socket(AF_INET, SOCK_DGRAM) while True: data = raw_input('Enter the message you want to send >') if not data: break udpClientSock.sendto(data, ADDR)发送给服务端 data, ADDR = udpClientSock.recvfrom(BUFSIZE)接收服务端信息 if not data: break print data udpClientSock.close()
写个服务端
#!/usr/bin/env python # -*- coding:UTF-8 -*- from socket import * from time import ctime HOST = '' PORT = 20001监控端口 BUFSIZE = 1024缓冲区大小 ADDR = (HOST, PORT) udpSerSock = socket(AF_INET, SOCK_DGRAM) udpSerSock.bind(ADDR) while True: print 'waiting for message...' data, addr = udpSerSock.recvfrom(BUFSIZE) udpSerSock.sendto('[%s] %s' % (ctime(), data), addr)给客户端发送信息 print'received from %s >> %s' % (addr, data) udpSerSock.close()
原文地址:http://blog.csdn.net/u010278923/article/details/42556731