本文整理汇总了Python中socket.SO_BROADCAST属性的典型用法代码示例。如果您正苦于以下问题:Python socket.SO_BROADCAST属性的具体用法?Python socket.SO_BROADCAST怎么用?Python socket.SO_BROADCAST使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类socket
的用法示例。
在下文中一共展示了socket.SO_BROADCAST属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_BROADCAST [as 别名]
def __init__(self, address='0.0.0.0', broadcast='255.255.255.255'):
# Prepare a socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.bind((address, 0))
self._socket = sock
self._listener = ListenerThread(sock, self._handle_packet)
self._listener.start()
self._packet_handlers = {}
self._current_handler_id = 0
self._broadcast = broadcast
示例2: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_BROADCAST [as 别名]
def __init__(self, device, baud=None, input=True, broadcast=False, source_system=255, source_component=0, use_native=mavutil.default_native):
self._logger = logging.getLogger(__name__)
a = device.split(':')
if len(a) != 2:
self._logger.critical("UDP ports must be specified as host:port")
sys.exit(1)
self.port = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.udp_server = input
self.broadcast = False
self.addresses = set()
if input:
self.port.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.port.bind((a[0], int(a[1])))
else:
self.destination_addr = (a[0], int(a[1]))
if broadcast:
self.port.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
self.broadcast = True
mavutil.set_close_on_exec(self.port.fileno())
self.port.setblocking(False)
mavutil.mavfile.__init__(self, self.port.fileno(), device, source_system=source_system, source_component=source_component, input=input, use_native=use_native)
示例3: send_packet
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_BROADCAST [as 别名]
def send_packet(secret):
try:
broadcast_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
broadcast_socket.bind(('', 0))
broadcast_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
except Exception as err:
print "[!] Error creating broadcast socket: %s" % err
sys.exit(ERROR)
data = "nukemyluks_" + secret
try:
broadcast_socket.sendto(data, ('<broadcast>', DEFAULT_PORT))
except Exception as err:
print "[!] Error sending packet: %s" % err
sys.exit(ERROR)
示例4: _setup_connection
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_BROADCAST [as 别名]
def _setup_connection(self, dstaddr, timeout=None):
port = randint(10000, 60000)
af, socktype, proto, _canonname, _sa = socket.getaddrinfo(dstaddr, port, socket.AF_INET, socket.SOCK_DGRAM)[0]
s = socket.socket(af, socktype, proto)
has_bind = 1
for _i in range(0, 10):
# We try to bind to a port for 10 tries
try:
s.bind((INADDR_ANY, randint(10000, 60000)))
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
has_bind = 1
except socket.error:
pass
if not has_bind:
raise NetBIOSError, ('Cannot bind to a good UDP port', ERRCLASS_OS, errno.EAGAIN)
self.__sock = s
示例5: test_beacon_already_running
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_BROADCAST [as 别名]
def test_beacon_already_running():
#
# NB this one has to run without the beacon fixture
#
# Bind a socket on a random port before attempting
# to start a beacon on that same port.
#
port = random.choice(nw0.config.DYNAMIC_PORTS)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.bind(("", port))
try:
assert nw0.discovery._beacon is None
nw0.discovery._start_beacon(port=port)
assert nw0.discovery._beacon is nw0.discovery._remote_beacon
finally:
s.close()
#
# Make sure any future beacon use assumes it's not
# already running.
#
nw0.discovery._stop_beacon()
示例6: wake_on_lan
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_BROADCAST [as 别名]
def wake_on_lan(macaddress):
"""Power on remote computers using Wake On LAN."""
# Handle MACs with or without separators.
if len(macaddress) == 12:
pass
elif len(macaddress) == 12 + 5:
sep = macaddress[2]
macaddress = macaddress.replace(sep, '')
else:
raise ValueError('Incorrect MAC address format')
print("Sending magic packet to", macaddress)
packet = bytes.fromhex(''.join(['FFFFFFFFFFFF', macaddress * 20]))
# Broadcast it to the LAN.
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
for brd in get_broadcast_list():
sock.sendto(packet, (brd, 9))
示例7: run
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_BROADCAST [as 别名]
def run(self):
UDP_IP = "0.0.0.0"
UDP_PORT = 5108
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while (True):
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
spl = data.split("::")
if (spl[0] == "0"):
print "received discovery broadcast"
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('', 0))
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
uuid = str(configmanager.uuid)
pairport = str(configmanager.port)
hostname = socket.gethostname()
data = uuid+"::"+hostname+"::"+pairport
s.sendto(data, (addr[0], UDP_PORT))
示例8: run
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_BROADCAST [as 别名]
def run(self):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
port = str(ui.local_port_stream)
print(ui.local_ip_stream, '-----ip--', ui.local_port_stream)
msg = 'this is kawaii-player At: port={} https={} pc_to_pc_casting={} msg={}'.format(
port, ui.https_media_server, ui.pc_to_pc_casting, ui.broadcast_message)
msg = bytes(msg , 'utf-8')
if ui.https_media_server:
https_val = 'https'
else:
https_val = 'http'
subnet_mask = ui.local_ip_stream.rsplit('.', 1)[0] + '.255'
notify_msg = '{0}://{1}:{2} started broadcasting. Now Clients can Discover it'.format(
https_val, ui.local_ip_stream, ui.local_port_stream)
send_notification(notify_msg)
print(subnet_mask)
while ui.broadcast_server:
s.sendto(msg, (subnet_mask,12345))
time.sleep(1)
send_notification('Broadcasting Stopped')
示例9: wake_on_lan
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_BROADCAST [as 别名]
def wake_on_lan(mac_address):
if len(mac_address) == 12:
pass
elif len(mac_address) == 12 + 5:
sep = mac_address[2]
mac_address = mac_address.replace(sep, '')
else:
raise ValueError('Wrong MAC Address format!') # (EN) Incorrect MAC format!
data = ''.join(['FFFFFFFFFFFF', mac_address * 20])
send_data = ''
for i in range(0, len(data), 2):
send_data = ''.join([send_data, struct.pack('B', int(data[i: i + 2], 16))])
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.sendto(send_data, ('<broadcast>', 7))
示例10: discover
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_BROADCAST [as 别名]
def discover(match="", timeout=2):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.sendto(ssdpre, (ssdpsrc["ip_address"], ssdpsrc["port"]))
s.settimeout(timeout)
responses = []
print ""
try:
while True:
response = s.recv(1000)
if match in response:
print response
responses.append(response)
except:
pass
return responses
示例11: reboot
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_BROADCAST [as 别名]
def reboot(timeout=2):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.sendto(exptpack1, (ssdpsrc["ip_address"], ssdpsrc["port"]))
s.settimeout(timeout)
s.settimeout(timeout)
trg = raw_input("\nTarget: ")
tpg = int(input("Port: "))
for i in range(4):
sys.stdout.write("\rSending Reboot Payload" + "." * i)
time.sleep(0.05)
print ""
s.sendto(exptpack1, (trg, tpg))
try:
s.connect((str(tpg), int(tpg)))
time.sleep(0.1)
s.send(u"`REBOOT`")
s.close()
time.sleep(1)
s.connect((str(tpg), int(tpg)))
except:
print "UPnP Device Rebooted"
s.close()
示例12: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_BROADCAST [as 别名]
def __init__(self):
super().__init__(databaseSchema=self.DATABASE)
self._devices = dict()
self._broadcastRoom = ''
self._broadcastFlag = threading.Event()
self._broadcastPort = None
self._broadcastTimer = None
self._flashThread = None
self._listenPort = None
self._broadcastSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self._broadcastSocket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
self._broadcastSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self._listenSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._listenSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self._listenSocket.settimeout(2)
示例13: discover
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_BROADCAST [as 别名]
def discover(self, name):
sock = socket.socket(self.sock_family, socket.SOCK_DGRAM)
try:
if self.bcast:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True)
data = brine.dump(("RPYC", "QUERY", (name,)))
sock.sendto(data, (self.ip, self.port))
sock.settimeout(self.timeout)
try:
data, _ = sock.recvfrom(MAX_DGRAM_SIZE)
except (socket.error, socket.timeout):
servers = ()
else:
servers = brine.load(data)
finally:
sock.close()
return servers
示例14: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_BROADCAST [as 别名]
def __init__(self, mac):
self.session_bytes_sent = 0
self.session_bytes_recv = 0
self.source_mac = b"\xff\xff\xff\xff\xff\xff" # put mac of your pc if mikrotik is not responding
self.dest_mac = mac
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
self.sock.bind(('', 0))
self.buffer = []
self.work = True
self.connected = False
self.rm = threading.Thread(target=self.__recv_manager__)
self.rm.start()
self.__send_init__()
示例15: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_BROADCAST [as 别名]
def __init__(self, own_id, is_client=True):
self.address = get_ownip()
self.broadcast = '255.255.255.255'
self.port = settings.discovery_port
self.own_id = own_id
self.mask = IS_CLIENT if is_client else IS_SERVER
# Create UDP socket
self.handle = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
# Ask operating system to let us do broadcasts from socket
self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
if hasattr(socket, 'SO_REUSEPORT'):
self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
else:
self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Bind UDP socket to local port so we can receive pings
self.handle.bind(('', self.port))