您好我有两个接口eth0(192.168.1.115)和wlan0(192.168.0.1)。我想发送一个广播。这是我的代码。但它只在eth0发送广播,我不知道为什么?
import socket
import time
import json
import threading
class BroadCaster(object):
def __init__(self, MESSAGE):
super(BroadCaster, self).__init__()
self.MESSAGE = json.dumps(MESSAGE)
self.UDP_IP = '<broadcast>'
self.UDP_PORT = PUERTO_DIFUSION
self.sock1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock1.bind(('',0))
self.sock1.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
self.sock1.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock2.bind(('192.168.0.1',0))
self.sock2.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
self.sock2.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
def run(self):
def broadcast(self):
while True:
print '* * Enviando * *'
self.sock1.sendto(self.MESSAGE, (self.UDP_IP, self.UDP_PORT))
self.sock2.sendto(self.MESSAGE, ('192.168.0.255', self.UDP_PORT))
time.sleep( TIEMPO_ANUNCIOS )
# manejo de hilos
thread = threading.Thread(target=broadcast(self))
thread.setDaemon(True)
thread.start()
data = {'Nombre': OBJETO_PYRO, 'IP': DIRECCION_WS+':'+str(PUERTO_WS), 'IP_2':DIRECCION_BLUETOOTH ,'Mensaje': MENSAJE}
caster = BroadCaster(data)
caster.run()
我无法通过tcpdump看到广播(我认为它不会发送任何内容),但如果我把
self.sock2.sendto(self.MESSAGE, ('192.168.0.2', self.UDP_PORT))
它有效,'192.168.0.2'是它试图看到广播的另一台电脑。