python接收广播_python Socket模块实现广播消息的例子

python socket模块实现网络广播消息。

例子: 代码示例:

#!/bin/python

#

"""Module for simple UDP broadcast support.

The classes in this module are stepping stones for building discoverable

services on a network. Server replies are to be handled by the importer."""

__author__ = 'Stephen "Zero" Chappell '

__date__ = '4 December 2011'

__version__ = '$Revision: 5 $'

#edit: www.#

import socket

import _thread

import time

class Beacon:

__slots__ = '__sock', '__addr'

def __init__(self, port):

"Initialize the beacon for sending and receiving data."

self.__sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True)

self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)

self.__sock.bind(('0.0.0.0', port))

self.__addr = '255.255.255.255', port

def __del__(self):

"Shutdown and close the underlying socket."

self.__sock.shutdown(socket.SHUT_RDWR)

self.__sock.close()

def recv(self, size):

"Receive a broadcast through the underlying socket."

return self.__sock.recvfrom(size)

def send(self, data):

"Send a broadcast through the underlying socket."

assert self.__sock.sendto(data, self.__addr) == len(data), \

'Not all data was sent through the socket!'

def __gettimeout(self):

return self.__sock.gettimeout()

def __settimeout(self, value):

self.__sock.settimeout(value)

def __deltimeout(self):

self.__sock.setblocking(True)

timeout = property(__gettimeout, __settimeout, __deltimeout,

'Timeout on blocking socket operations.')

#----

def test():

"Test the beacon broadcasting class."

b = Beacon(50000)

_thread.start_new_thread(test_send, (b,))

test_recv(b)

#--测试发送广播数据

def test_send(b):

"Test the beacon's send method."

while True:

b.send(time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()).encode())

time.sleep(1)

#--测试接收网络广播数据

def test_recv(b):

"Test the beacon's recv method."

while True:

data, address = b.recv(1 << 12)

print('From: {}\n{}\n'.format(address, data.decode()))

#----

if __name__ == '__main__':

test()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值