How to send broadcast IP address to network?
Irmen de Jong
irmen.NOSPAM at xs4all.nl
Sat Aug 24 07:33:12 EDT 2013
On 23-8-2013 14:32, lightaiyee at gmail.com wrote:
> I want to send a broadcast packet to all the computers connected to my home router.
>
> The following 2 lines of code do not work;
> host="192.168.0.102"
> s.connect((host, port))
>
> Can someone advise?
>
> Thank you.
>
Use UDP (datagram) sockets. Use sendto() and not connect(), because UDP is
connectionless. This should work:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.sendto(b"thedata", 0, ("<broadcast>", 9999)) # 9999 = port
sock.close()
Irmen
More information about the Python-list
mailing list