Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python OS Error: A socket operation was attempted to an unreachable network

I have made a very simple python script which should connect using the public ip of a device. However when running I get this error: OSError: [WinError 10051] A socket operation was attempted to an unreachable network. I understand that this means my computer can't find a way to route to the ip, however I don't understand how to fix it.

Code:

import socket

ip = "some ip"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1", 5000))
s.connect((ip, 5000))
s.close()

Any help is appreciated.

like image 235
blunty8484 Avatar asked May 10 '26 15:05

blunty8484


1 Answers

Very simple fix, I just had to remove the bind and change the connect port to 80. Working code:

import socket

ip = "some ip"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Finding connection to target")
s.connect((ip, 80))
print("Connected to " + ip)
s.close()```
like image 53
blunty8484 Avatar answered May 13 '26 05:05

blunty8484



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!