Connecting to a simple sockets python server remotely
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.
Related videos on Youtube
Comments
-
Kex almost 2 years
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 usingnap
: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!
-
Remi Guan over 8 yearsDid you do the port mapping?
-
Some programmer dude over 8 yearsNothing happens in neither the server nor the client? How long have you waited (because connection attempts can take some time to timeout)?
-
Kex over 8 years@KevinGuan haven't done any port mapping, not sure what that is
-
Kex over 8 years@JoachimPileborg nothing happens for either. The client script does work though, it actually reports a connection when I run nap (but the connections is itself)
-
Remi Guan over 8 yearsIt looks like you're trying to using client connect to the server via the Internet, not LAN. So maybe you need to do port mapping. Please see the wiki: en.wikipedia.org/wiki/Port_forwarding
-
Kex over 8 years@KevinGuan yesI'm trying to connect via the internet not LAN.
-
dsgdfg over 8 yearsFriend , use
0.0.0.0
for binding all interface and you need router redirecting for related service ! -
Remi Guan over 8 yearsYeah, before you try this, please try connect to your computer itself first to check the code can run or not. However I mean: run server program at your computer first, and then run the client program to connect to the server program. And like @dsgdfg said, use
0.0.0.0
or127.0.0.1
instead139.xxx.xx.xx
. -
Fujiao Liu over 8 yearstry to change 'host' to linode public ip address on server side, and use the ip on client side to connect.
-
Kex over 8 years@KevinGuan yeah checked the client and server on my local machine using 0.0.0.0 and it works fine
-
Remi Guan over 8 yearsSo your code is so good, now you can learn about the port mapping :)
-
Kex over 8 years@KevinGuan so port mapping is extra code I need to add to my client to pass the connection out over the internet?
-
Remi Guan over 8 yearsNope. you need to configure it on your router. You can use Google and search How to do port mapping.
-
Kex over 8 years@KevinGuan it must be possible to do it by code though right? I want to make a client able to work on any machine without the user having to configure the router first.
-
Remi Guan over 8 yearsHowever you just need configure the router for your server machine. And then, everyone can connect to it use the client program. For example: if you're configure a web server, you have to mapping the
80
port for your server. But after that, everyone can use they're web browser connect to your web server. -
mhawke over 8 yearsIn the server what is
socket.gethostname()
returning? It could be that your server is binding to a local interface, not a public interface. Others have suggested using0.0.0.0
as the host in the server - did you try that? The other thing to check is whether your host allows connections through the firewall to port 1234. If not you need to open port 1234, there is some documentation here: linode.com/docs/security/securing-your-server (see the Firewall section).
-