Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot connect to python -m SimpleHTTPServer server

this this probably a very simple question, but I haven't been able to find an answer anywhere. On the online articles about it, they didn't show the exact process to share a directory using SimpleHTTPServer. I've run the command successfully and have the server running, but I can only access it on the machine that started it.

192.168.1.2:8000

I've tried it on a Windows machine and iPad (although that doesn't really make a difference) on the local network. To access it, I've been using my local IP address, which I found by running ifconfig | grep inet, which returns (among other matches):

inet 192.168.1.2 netmask 255.255.255.0 broadcast 192.168.1.255

And after searching a bit online, I found: https://github.com/shbhrsaha/instant-sharing/blob/master/share.py.

There's function which supposedly gives you a handy url to share with your friends, but I tried running locally, and all I got was "localhost.localdomain", which obviously returns 127.0.0.1

How can I make this work?

like image 433
Student Loans Avatar asked Jan 07 '15 04:01

Student Loans


People also ask

Does Python have HTTP server?

You can run python http server on any port, default port is 8000. Try to use port number greater than 1024 to avoid conflicts. Then open your favourite browser and type localhost:9000 .


2 Answers

In Ubuntu I had the same problem, then I narrowed it down to be a firewall problem. First, check if your firewall is active.

sudo ufw status

If it is active by default it blocks all incoming connections from anywhere.

You can check whether you have granted access to your running port. The following command will list down all the available rules. if your port is not there with access given to other ports then you need to grant access.

sudo ufw status numbered

[This is what the issue] Now grant access on the port for desired ip addresses/ all. I allowed all incoming connections to the port 8000 on all ip adress by following command.

sudo ufw allow from any to any port 8000 proto udp

Initially, I thought this should be tcp instead of udp but worked with udp only. Something to dig up later.

like image 53
Gayan Kavirathne Avatar answered Nov 13 '22 06:11

Gayan Kavirathne


When you start SimpleHTTPServer it tells which IP addresses it is listening to:

  python -m SimpleHTTPServer
  Serving HTTP on 0.0.0.0 port 8000 ...

Address 0.0.0.0 means it listening to all available IP addresses. Thus in this case you should simply reach the server by going http://192.168.1.two:8000

If it doesn't work then it is most likely a network issue. You can test this out with telnet command (both Windows and UNIX available): telnet will open a TCP/IP connection on a certain IP and certain port.

E.g. on UNIX you can do:

  telnet 192.168.1.2 8000 

If you get:

 telnet 192.162.1.2 8000
 Trying 192.162.1.2...
 telnet: connect to address 192.162.1.2: Connection refused
 telnet: Unable to connect to remote host

... it means SimpleHTTPServer is running.

If it waits for very long time it means your router/firewall is blocking the connection.

If you get a reply:

telnet localhost 8000
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

... browser should work as well. You can quit telnet by just keep hitting the enter (the SimpleHTTPServer will close the connection).

like image 37
Mikko Ohtamaa Avatar answered Nov 13 '22 07:11

Mikko Ohtamaa