3

I am using the following code to open a socket on my computer. When I go to my_ip:5000 on my computer the program responds. However, when I use another computer, nothing happens.

HOST = 'my_ip'                 # Symbolic name meaning the local host
PORT = 8000              # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
    data = conn.recv(1024)
    if not data: break
    conn.send(data)
conn.close()

I'm not sure if this is an issue with the firewall or not. When I start up the test server in django using manage.py runserver my_ip:8000 I am able to connect to the machine from a different computer. I'm not sure what is causing me not to be able to connect from another computer using the code above...

4
  • ping my_ip works from that machine? Commented Oct 20, 2011 at 18:38
  • 1
    Are you trying to reach the server from outside your local network? If that's the case, check if you've forwarded the port correctly. If you're trying to access it from a computer in the same network, be sure to use the internal IP and not the external. Commented Oct 20, 2011 at 18:39
  • Yes, when I type my_ip in to the machine the above code is running on it works. However when I use a different machine, nothing. Commented Oct 20, 2011 at 19:44
  • I'm trying to access it from outside the network Commented Oct 20, 2011 at 19:44

2 Answers 2

9

By setting HOST to 'my_ip', you may listen to a private IP that only resolves to your computer for your computer. The best-known example is 127.0.0.1. Instead, pass an empty string (HOST='') to listen to any requests coming in at the specified port. Make sure to use the same port number, i.e. either 5000 or 8000, on both machines.

Also, check whether a firewall between the computers (or installed on one of them) prevents the connection.

To test whether your computer is reachable in principle, run python -m SimpleHTTPServer in a directory without private information on the server and try to reach the webserver started with that from the client.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the reply. I tried the -m SimpleHTTPServer and it worked on the local machine but not a remote machine. In Django, when I run manage.py runserver my_ip:8000 I am able to use a remote machine to access files. Could this be an issue with the firewall? I'm using Windows 7.
@AlexisK Yes, it could definitely be the local firewall. The result of running django and running the SimpleHTTPServer should definitely be the same though. Are you certain the IP is correct? By the way, you may want to use a packet dumper such as wireshark (potentially on both machines) to debug the problem.
Yeah, I through running the django and SimpleHTTPServer should be the same but for some reason it is not :( I tried SimpleHTTPServer in the exact folder that I ran runserver in and it didn't work.
@AlexisK That strongly indicates the firewall. Does it work if you temporarily disable the Windows firewall? What does wireshark display?
I turned off the firewall and same behavior. runserver works as expected SimpleHTTPServer does not. ;( SimpleHTTPServer says "Serving HTTP on 0.0.0.0 port 8000"
|
2

The code looks to be correct, so your problem lies elsewhere (perhaps a firewall issue or dns problem for "my_ip").

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.