<pre name="code" class="python">#!usr/bin/env python
#-*- coding: utf-8 -*-
#代码学习自《Black Hat Python:Python Programming for Hackers and Pentesters》
#简易 UDP 客户端
import socket
target_host = "127.0.0.1"#本机回送地址(Loopback Address
target_port = 10000
#build a socket object
client = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)#UDP
#send some data
while 1:
data = raw_input()
client.sendto(data,(target_host,target_port))
#receive some data
data,addr = client.recvfrom(4096)#4k
print data
UDP服务端 参考http://www.jb51.net/article/49357.htm
#!usr/bin/env python
#-*- coding: utf-8 -*-
import socket
target_host = '127.0.0.1'
target_port = 10000
server = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
server.bind((target_host,target_port))
while 1:
data,addr = server.recvfrom(4096)#4k
if not data:
break
print "receive data from",addr
print data
server.close()
效果图