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.
Here is the code that I thought would work, but raises an exception:
File "E:\Python\server client comms\messageEveryIP.py", line 11, in <module>
s.bind((curIP, listeningPort))
OSError: [WinError 10049] The requested address is not valid in its context
In my case it errors out on 192.168.1.2 because there is no machine on that IP.
import socket
host = socket.gethostbyname(socket.gethostname())
listeningPort = 4005
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
i = 1
while i < 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
OSErrorand the code will run. might not be the best solution.Exception: OSError, IP not valid in context 192.168.1.1 Exception: OSError, IP not valid in context Traceback (most recent call last): File "E:\Python\server client comms\messageEveryIP.py", line 13, in <module> s.bind((curIP, listeningPort)) OSError: [WinError 10049] The requested address is not valid in its contextI added the exception print line myself.bind()is used to assign socket to local network card (LAN/WiFi) in your computer - it is not used to connect to other computer. And you have to use IP of your network card or0.0.0.0to use all network cards in your computer. You should use'192.168.1.' + str(i)insendto()