4

I'm developing for web and I have a prototype of my website. For testing locally I usually do

python3 -m http.server

and then go to localhost:8000 on my browser. This works great!

Then I read on the internet I could figure my private IPv4 IP address (10.0.0.101 as per ipconfig) and run

10.0.0.101:8000

on my phone's browser to access my website.

Thing is when I do this I get an "This site can't be reached (ERR_CONNECTION_TIMED_OUT)".


My network config

Between my ISP modem and my devices I have an router (D-Link DIR-815). An ethernet cable connects the router to my desktop computer and my phone is connected via Wi-Fi.


What I've tried

I've tried all items below and a lot of combinations between them (but unlikely all of the combinations because there are too many)

  • Using chrome inside the Bluestacks emulator
  • Forwarding the port 8000 on my router and using <my_public_ip>:8000
  • adding a firewall exception for the 8000 port (both TCP and UDP)
  • using 10.0.0.101:8000 on my desktop browser
    • I did it just for testing and it came as a surprise for me that this didn't work!
  • using 127.0.0.1:8000 on my desktop browser (just for testing)
    • That didn't work. Which came as an even bigger surprise for me.

The Question

What am I missing here? Why can't I access my localhost from my phone?

I've read many question including this one which contains many answer and was the top result on google. Bug the thing is both for the sake of simplicity of using only python and for my education I want to know how can I do this without installing more complex solutions like XAMPP.

PS:

  1. Also I know it is possible because it is shown in this video.
  2. I double checked my private IPv4 IP in my router (dlinkrouter.local)
Henri Augusto
  • 309
  • 3
  • 14
  • 1
    Your router shouldn't have anything to do with it because it shouldn't affect what's going on inside your private network (10.0.0.X). I have one idea. I'm looking closely at all your excellent info to see if you've ruled out my idea or not... – CryptoFool Sep 20 '20 at 02:32
  • 2
    I would expect`127.0.0.1:8000` to work in any case, but what may be happening is that your server is only listening on the loopback interface, and not your NIC card. Do you know what I'm talking about? The standard way to fix this is to explicitly LISTEN on the address `0.0.0.0`. That means "listen on all interfaces". Do you know if you're doing that? I don't see anything that tells me either way, although maybe I missed it. (this is mentioned on the "this one" link you give above, but down a ways.) – CryptoFool Sep 20 '20 at 02:36
  • To be honest I'm pretty new to web dev :) I just know that a loopback interface it is something that allows a server and client to communicate in the same device. But i don't know any details about them. – Henri Augusto Sep 20 '20 at 02:39
  • 1
    a server will listen on certain interfaces, depending on its setup. Sometimes, for security, the default is to only listen on the loopback interface, and so only accept connections from the same machine. this is done by listening on "localhost" or "127.0.0.1". Listening on "0.0.0.0" listens on everything. listening on your public IP will, somewhat strangely, stop "localhost" and "127.0.0.1" from working and make you type your own IP address. What framework are you using as the web host? It's probably a configuration on that system. – CryptoFool Sep 20 '20 at 02:41
  • 1
    Aha...here's exactly this problem/solution for Flask (as an example, even if you aren't using Flask): https://stackoverflow.com/questions/7023052/configure-flask-dev-server-to-be-visible-across-the-network – CryptoFool Sep 20 '20 at 02:44
  • I don't know what framework I'm using haha I'm just using the python cmd in the post (which I got form a simple MDN tutorial). I don't know what I'm doing but I've tried `python3 -m http.server --bind 0.0.0.0` and it doesn't work. – Henri Augusto Sep 20 '20 at 03:31
  • 1
    Oh look. Python has a web server in its standard library. I guess that's not all that surprising. I've been a Python weenie for many years and I never really registered that. - Looking at the docs, it's pretty clear that what I was thinking might be the problem...well, isn't. The docs say clearly "By default, server binds itself to all interfaces." So if you're not overriding the default behavior, your server should be listening for connections from the outside world. And that should mean that anything on your private network should be able to connect. Strange. – CryptoFool Sep 20 '20 at 05:58
  • 1
    I don't know how this will help you. Maybe it will just piss you off. I just tried what you describe, and it works fine for me. I did `python3 -m http.server` on my Mac, and I can connect to it via my local network IP from both my girlfriend's Mac and my iPhone. I get a directory listing. So maybe it IS your router somehow. You mention a firewall. Is that in your router, or on your host? If the former, might you have a firewall running on the server that is blocking the port? That's the last thing I can think of. I'm not running a firewall on my Mac. – CryptoFool Sep 20 '20 at 06:12
  • I figure there is something very wrong with my DIR-815 router. It is known for constantly rebooting and D-link didn't provide a firmware update for fixing that. Also changing configurations is a nightmare because 9 out of 10 times when you try to save your changes the firmware page just gives a static background and changes are not saved. When I finally managed to disable the router's SPI firewall I also disabled my windows firewall. And still couldn't connect. – Henri Augusto Sep 20 '20 at 21:06
  • I officially give up trying to solve this. I'll just use gitpages to test my stuff... But hey, thanks a lot for trying to help :) – Henri Augusto Sep 20 '20 at 21:07
  • Sorry you have to give up. If you ever do figure it out, I'd love to hear what is going on – CryptoFool Sep 20 '20 at 22:01

1 Answers1

0

I had the same problem and I solved it using a different port number. Instead of default 8080, I used 3000.

  1. Write the command on Powershell
python -m http.server 3000
  1. Open CMD and write the command
ipconfig
  1. Get the IPv4 Address
  2. Write the same address on the mobile phone's browser (which is on the same network) as below
  3. Example:
192.168.43.136:3000

That should work.

sglbl
  • 141
  • 1
  • 3