I'm trying to get a Bottle server in Python to work. Here is my code:
from bottle import route, run, template
from socket import gethostname, gethostbyname
from time import sleep
ip = str(gethostbyname(gethostname()))
@route('/')
def index():
return 'Hello World!'
run(host=ip, port=1234)
I run this, and on my computer where I'm running it, I navigate to http://127.0.1.1:1234/
, and my website shows up, with Hello World!
.
However, if I try to connect to it on my phone or my sister's Chromebook, it says that the website refused to connect.
I have tried replacing str(gethostbyname(gethostname()))
with '0.0.0.0'
and 'localhost'
, but none have worked.
Get rid of this line; it's not necessary:
ip = str(gethostbyname(gethostname()))
Make your run
line look like this:
run(host='0.0.0.0', port=1234)
The address 0.0.0.0
means "listen on all addresses".
Lastly, figure out the network address of the host on which your app is running. Then other devices on the same network should be able to connect to <that ip address>:1234
. Devices not on the same network would only be able to connect to the service if you had a publicly routeable address (or if you arranged to forward the appropriate port from a router that has a public address).
You'll want to make sure the system on which your app is running doesn't have firewall rules that would prevent an otherwise successful connection.