server端:

#!/usr/bin/python
from socket import *
from time import ctime
HOST=''
PORT=21567
BUFSIZ=1024
ADDR=(HOST,PORT)
udpSerSock = socket(AF_INET,SOCK_DGRAM)
udpSerSock.bind(ADDR)
while True:
        print 'waiting for message..'
        data,addr = udpSerSock.recvfrom(BUFSIZ)
        udpSerSock.sendto('[%s] %s' % (ctime(),data),addr)
        print '...received from and return to:',addr

client端:

   

#!/usr/bin/python
from socket import *
HOST = 'localhost'
PORT = 21567
BUFSIZ = 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(BUFSIZ)
        if not data:
                break
        print dataudpClisock.close()