使用python实现信息的接受和发送
在python中实现一个信息接受与发送功能,具体方法如下:
import socket
import time
ANY = '0.0.0.0'
MCAST_ADDR = '224.168.2.9'
MCAST_PORT = 1600
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) #允许端口复用
sock.bind((ANY,MCAST_PORT)) #绑定监听多播数据包的端口
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255)
status = sock.setsockopt(socket.IPPROTO_IP,
socket.IP_ADD_MEMBERSHIP,
socket.inet_aton(MCAST_ADDR) + socket.inet_aton(ANY));
sock.setblocking(0)
ts = time.time()
while 1:
try:
data, addr = sock.recvfrom(1024)
except socket.error, e:
pass
else:
print "We got data!"
print "FROM: ", addr
print "DATA: ", data
声明:本站所有作品(图文、音视频)均由用户自行上传分享,或互联网相关知识整合,仅供网友学习交流,若您的权利被侵害,请联系 管理员 删除。
本文链接:http://news.ew35.com/