[Reprint] 7868028
UDP Server Code:
#!/usr/bin/Env python#-*-coding:utf-8-*- fromSocket Import * fromTime Import Ctimehost='127.0.0.1'PORT=21567BUFSIZE=1024x768ADDR=(host,port) Udpsersock=socket (af_inet, Sock_dgram) udpsersock.bind (ADDR) whileTrue:print'wating for message ...'data, addr=Udpsersock.recvfrom (BUFSIZE) udpsersock.sendto ('[%s]%s'%(CTime (), data), addr) Print'... received from and retuned to:', Addrudpsersock.close ()
UDP Customer Terminal code:
#!/usr/bin/env python fromSocket Import *HOST='localhost'PORT=21567BUFSIZE=1024x768ADDR=(HOST, PORT) Udpclisock=socket (af_inet, SOCK_DGRAM) whileTrue:data= Raw_input ('>') ifNot data: Breakudpclisock.sendto (data,addr) data,addr=Udpclisock.recvfrom (BUFSIZE)ifNot data: Breakprint dataudpclisock.close ()
A simple example of Python network programming UDP server and customer service terminal