Port Forwarded Python Socket Error (SERVER) - python

I am currently working on a Python project to control my PC remotely from across networks. Currently I have a portmap.io connection set up, (IP: 193.xxx.xxx.xx with port 1234, I’m using an OpenVPN)
When I run my Python script:
import socket
from os import system, name
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = “193.xxx.xxx.xx”
PORT = 1234
s.bind((HOST,PORT))
s.listen(1)
conn, addr = s.accept()
s.send(b”Connection established”.encode(“utf-8”))
When I run this Python script, I get this error:
OSError: [WinError 10049] The requested address is not valid in its context
I’m almost 85% sure this is because you can’t use bind() on an ip, so what should I do? The socket needs to go across networks.

(SOLVED)
I used ngrok to create a tcp tunnel and used 0.0.0.0 as my local host, and used the provided details for the remote connection

Related

OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions.What to do?

I created a simple socket program by python by creating two different files Server.py and a Client.py and tried to connect it by s.connect((host,port)) but
OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions
occurs
I have looked up many solutions on the net regarding this issue which could be due to Firewall issues, Malware in system, outbound firewall settings , Run by Administrator, even checked if the port was not in use by netstat in cmd but none of them solved my issue.
plz help
Error Image
This is my code:
#SERVER.py
import socket
s= socket.socket()
host= socket.gethostname()
port = 5000
s.bind((host,port))
s.listen(1)
print(host)
print('Waiting for any incoming connections...')
conn, addr = s.accept()
print(addr,'Has connected to the server')
#CLIENT.py
import socket
s = socket.socket()
host=input('Enter host address:')
port=5000
s.connect((host,port))
print('Connected...')
Try localhost, if host is not working. Like for eg. host='127.0.0.1' #( The server's hostname or IP address)and use a port >1023 (As those are non-privileged ports)
If its still not working, you can try changing your network and try the same steps. If any further issue occurs regarding the same error there must be a issue with your system, for that try factory reset. Then it will work 100%
Best of luck.

Firewall is not allowing my python client application to connect to a server running on my machine even after I turn off firewall completely

I am learning about the python socket library and am running into problems whenever I try to connect to the server running on my localhost with a client application.
Here is the server code:
import socket
HOST = '127.0.0.1' # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
print(data)
Here is the code for my client application:
import socket
HOST = "localhost" # The server's hostname or IP address
PORT = 65432 # The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
Here is my error message:
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
Here is what I have tried so far:
Disabling my Window's 10 firewall completely on the windows command prompt with the use of the following command:
netsh advfirewall set allprofiles state off. This did not work
To windows firewall I added an inward rule and outward rule that allows any application on my OS to access a service running on port 65432
I changed my python version from 3.8.2 to 3.7.7 because before hand I was able to run this code perfectly and I was using a python 3.7 version
I tried multiple different methods of setting the HOST variable, which include "localhost", '127.0.0.1', socket.gethost(), and socket.gethostbyname("localhost")
I am able to connect to the server through the use of the Window's telnet application but that is it. To be honest I have exhausted possible solutions that I can find online, and I know that this question has came up on this website a lot, but I have honestly tried every solution I have seen so far - which included three hours of searching.
I appreciate any possible help that you guys can give, thanks.
Since the code was working earlier in the machine,this doesn't seem to be code issue.
Also the code ran fine in my machine.
I suggest you to run through the below steps once again:
Solution 1:
1. edit the server address as 127.0.0.1 or the host private IP in both the code just to be assured there is no discrepancy.
2. Start the server program first and make sure it didn't terminate.
3. Start the client application and check if the server program threw any error or exceptions.
Solution 2:
Change the port number and follow solution 1.
Solution 3:
Switch off the windows firewall from the UI just to be sure.
Follow the solution 1 steps
Solution 4:
Change the server address as host=''

Can't remotely connect to Python Socket

I have created a chat application using python sockets and Tkinter and it all works perfectly locally however the Client is unable to connect to the server remotely (when I enter my public IP address as the host) I have already fully port-forwarded my network and I know how to port forward very well and when I run an online Port Open scanner that checks if a port is open it states that the port is open!?
I have port-forwarded my router on a number of ports and updating the client and server accordingly however the client and telnet could still not connect.. I have also disabled all my windows firewalls and I disabled all of the routers firewalls.
Here is a very simple socket client and server model which I am trying to troubleshoot my problem using.
Server
import socket
s = socket.socket()
host='0.0.0.0'
port = 2000
s.bind((host,port))
s.listen(5)
while True:
c, addr = s.accept()
print ("Got a connection from: ", addr)
c.send(bytes("Thanks for connecting",'utf8'))
c.close()
Client
import socket
s = socket.socket()
port = 2000
s.connect(("109.156.114.183", port))
print (s.recv(1024))
s.close
Telnet
C:\Users\Maks>telnet 109.156.114.183 2000
Connecting To 109.156.114.183...Could not open connection to the host, on port 2000: Connect failed
When I try to connect to the server on 127.0.0.1 or localhost or from within my local network the client connects perfectly and telnet can connect as well. I am 200% sure I have port forwarded correctly because when I run a Port Open scan it says that the port(2000) is open.
Please help!
Thanks-
Maks
I'm at home so there's no firewalls running on my network at all.
That comment was the missing piece to solve your problem. You actually don't connect from remote as your question implies but you try to connect from inside your local network to the externally visible address of your router.
Such a setup is supported by some routers and not by others. It looks like you router does not support it. For more information on this see NAT hairpinning (or NAT loopback).

Connecting to a simple sockets python server remotely

I am trying to setup a very simply sockets app. My server code is:
import socket
s = socket.socket()
host = socket.gethostname()
port = 1234
s.bind((host,port))
s.listen(5) #Here we wait for a client connection
while True:
c, addr = s.accept()
print "Got a connection from: ", addr
c.send("Thanks for connecting")
c.close()
I placed this file on my remote Linode server and run it using python server.py. I have checked that the port is open using nap:
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
1234/tcp open hotline
I now run the client.py on my local machine:
import socket # Import socket module
s = socket.socket() # Create a socket object
port = 1234 # Reserve a port for your service.
s.connect(("139.xxx.xx.xx", port))
print s.recv(1024)
s.close # Close the socket when done
However I am not getting any kind of activity or report of connection. Could someone give me some pointers to what I might have to do? Do I need to include the hostname in the IP address I specify in the client.py? Any help would be really appreciated!
I've just summarize our comments, so your problem is this:
When you trying to using the client program connect to the server via the Internet, not LAN.
You should configure the
port mapping on your router.
And however, you just need configure the
port mapping for your server machine.
After you did that, then you can use the client program connect to your server prigram.

Why am I getting the error "connection refused" in Python? (Sockets)

I'm new to Sockets, please excuse my complete lack of understanding.
I have a server script(server.py):
#!/usr/bin/python
import socket #import the socket module
s = socket.socket() #Create a socket object
host = socket.gethostname() #Get the local machine name
port = 12397 # Reserve a port for your service
s.bind((host,port)) #Bind to the port
s.listen(5) #Wait for the client connection
while True:
c,addr = s.accept() #Establish a connection with the client
print "Got connection from", addr
c.send("Thank you for connecting!")
c.close()
and client script (client.py):
#!/usr/bin/python
import socket #import socket module
s = socket.socket() #create a socket object
host = '192.168.1.94' #Host i.p
port = 12397 #Reserve a port for your service
s.connect((host,port))
print s.recv(1024)
s.close
I go to my desktop terminal and start the script by typing:
python server.py
after which, I go to my laptop terminal and start the client script:
python client.py
but I get the following error:
File "client.py", line 9, in
s.connect((host,port))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 111] Connection refused
I've tried using different port numbers to no avail. However, I was able to get the host name using the same ip and the gethostname() method in the client script and I can ping the desktop (server).
Instead of
host = socket.gethostname() #Get the local machine name
port = 12397 # Reserve a port for your service
s.bind((host,port)) #Bind to the port
you should try
port = 12397 # Reserve a port for your service
s.bind(('', port)) #Bind to the port
so that the listening socket isn't too restricted. Maybe otherwise the listening only occurs on one interface which, in turn, isn't related with the local network.
One example could be that it only listens to 127.0.0.1, which makes connecting from a different host impossible.
This error means that for whatever reason the client cannot connect to the port on the computer running server script. This can be caused by few things, like lack of routing to the destination, but since you can ping the server, it should not be the case. The other reason might be that you have a firewall somewhere between your client and the server - it could be on server itself or on the client. Given your network addressing, I assume both server and client are on the same LAN, so there shouldn't be any router/firewall involved that could block the traffic. In this case, I'd try the following:
check if you really have that port listening on the server (this should tell you if your code does what you think it should): based on your OS, but on linux you could do something like netstat -ntulp
check from the server, if you're accepting the connections to the server: again based on your OS, but telnet LISTENING_IP LISTENING_PORT should do the job
check if you can access the port of the server from the client, but not using the code: just us the telnet (or appropriate command for your OS) from the client
and then let us know the findings.
Assume s = socket.socket()
The server can be bound by following methods:
Method 1:
host = socket.gethostname()
s.bind((host, port))
Method 2:
host = socket.gethostbyname("localhost") #Note the extra letters "by"
s.bind((host, port))
Method 3:
host = socket.gethostbyname("192.168.1.48")
s.bind((host, port))
If you do not exactly use same method on the client side, you will get the error: socket.error errno 111 connection refused.
So, you have to use on the client side exactly same method to get the host, as you do on the server. For example, in case of client, you will correspondingly use following methods:
Method 1:
host = socket.gethostname()
s.connect((host, port))
Method 2:
host = socket.gethostbyname("localhost") # Get local machine name
s.connect((host, port))
Method 3:
host = socket.gethostbyname("192.168.1.48") # Get local machine name
s.connect((host, port))
Hope that resolves the problem.
host = socket.gethostname() # Get the local machine name
port = 12397 # Reserve a port for your service
s.bind((host,port)) # Bind to the port
I think this error may related to the DNS resolution.
This sentence host = socket.gethostname() get the host name, but if the operating system can not resolve the host name to local address, you would get the error.
Linux operating system can modify the /etc/hosts file, add one line in it. It looks like below( 'hostname' is which socket.gethostname() got).
127.0.0.1 hostname
in your server.py file make : host ='192.168.1.94' instead of host = socket.gethostname()
Pay attention to change the port number. Sometimes, you need just to change the port number. I experienced that when i made changes over changes over syntax and functions.
I was being able to ping my connection but was STILL getting the 'connection refused' error. Turns out I was pinging myself! That's what the problem was.
I was getting the same problem in my code, and after thow days of search i finally found the solution, and the problem is the function socket.gethostbyname(socket.gethostname) doesnt work in linux so instead of that you have to use socket.gethostbyname('put the hostname manually') not socket.gethostbyname('localhost'), use socket.gethostbyname('host') looking with ifconfig.
try this command in terminal:
sudo ufw enable
ufw allow 12397

Categories

Resources