Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving from localhost to real URL in Flask?

Tags:

python

flask

I'm developing a site with Python and Flask and want to move from localhost in my browser to my team's development server and development site but I'm having problems. I've already looked at the Flask documentation, which recommends using host='0.0.0.0' or specifying SERVER_NAME. Thusfar, neither of these things have helped. Here's a bit of code to show what I'm doing:

app = Flask(__name__)

if __name__ == '__main__':
    app.secret_key = 'super secret key'
    app.config['SERVER_NAME'] = 'https://example.org/blah'
    app.run(host=app.config['SERVER_NAME'], port=4343, debug=True)

As you can see, instead of localhost:500, I want to be able to go into my browser and visit 'https://example.org/blah' and see the same things.

With this piece of code, I get this error:

(py34)user:~/flask$ python app.py
INFO - Connection with server established!
INFO - Server version meets recommendations (v2.9)
Traceback (most recent call last):
File "app.py", line 18, in <module>
app.run(host=app.config['SERVER_NAME'], port=4343, debug=True)
File "/home/me/anaconda3/envs/py34/lib/python3.4/site-packages/flask/app.py", line 772, in run
run_simple(host, port, self, **options)
File "/home/me/anaconda3/envs/py34/lib/python3.4/site-packages/werkzeug/serving.py", line 674, in run_simple
s.bind((hostname, port))
socket.gaierror: [Errno -2] Name or service not known

If instead of using SERVER_NAME I use host='0.0.0.0', I don't get any errors and it will successfully start "Running on http://0.0.0.0:4343/" but I can't follow that url into my browser the same way I can when I connect to my localhost.

What do I need to do to get my flask app running on https://example.org/blah?

Also, if it helps to know, I have to ssh into my server.

like image 293
SnarkShark Avatar asked Jul 11 '16 23:07

SnarkShark


People also ask

How do you change the URL on a flask?

The url_for() function is used to build a URL to the specific function dynamically. The first argument is the name of the specified function, and then we can pass any number of keyword argument corresponding to the variable part of the URL.

What is the URL to run your application on local host in flask?

The application is running locally on the URL http://127.0.0.1:5000/ . 127.0. 0.1 is the IP that represents your machine's localhost and :5000 is the port number. Open a browser and type in the URL http://127.0.0.1:5000/ .


1 Answers

If you run with host='0.0.0.0' from the server, you should be able to navigate to example.org:4343 and see your app.

Additionally, SERVER_NAME requires the port, per the documentation:

the name and port number of the server. Required for subdomain support (e.g.: 'myapp.dev:5000')

Finally, if you want to show you app without a port number, you either need to run it at port 80 (or 443) with root permissions, or using Nginx/Apache to route the request from your domain to the app.

like image 144
Celeo Avatar answered Oct 26 '22 04:10

Celeo