title: Python - Using socket to send data to every IP address on a network
body: <p>What I am looking for is for my python server, which is just a private server that responds to client input, for when it starts to send it's IP address to every IP on a network on port 4005. I do not know a way to figure out exactly which IPs are valid to send to on a network.</p>
  <p>Here is the code that I thought would work, but raises an exception:</p>
  <pre><code>File &quot;E:\Python\server client comms\messageEveryIP.py&quot;, line 11, in &lt;module&gt;
      s.bind((curIP, listeningPort))
  OSError: [WinError 10049] The requested address is not valid in its context
  </code></pre>
  <p>In my case it errors out on 192.168.1.2 because there is no machine on that IP.</p>
  <pre><code>import socket
  host = socket.gethostbyname(socket.gethostname())
  listeningPort = 4005
  
  s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  
  i = 1
  while i &lt; 255:
      curIP = '192.168.1.' + str(i)
      listeningAddress = (curIP, listeningPort)
      s.bind((curIP, listeningPort))
      s.sendto(host.encode('utf-8'), listeningAddress)
      s.close()
      i += 1
  </code></pre>