1,编写Server.py
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind(('localhost',8081))
while True:
data,addr=s.recvfrom(1024)
print 'receive:',data,'from',addr
2,编写Client.py
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
while True:
msg=raw_input()
s.sendto(msg,('localhost',8081))
3,运行程序
不同cmd窗口,分别运行两个py。Client窗口输入信息,Server窗口输出相应信息。