python flask to production server - python

I have made a small API to connect to a database using Flask.
When I run it I get this output on local (which works fine in postman)
* Serving Flask app "main" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
I want to run this file (main.py) on a server that I have at 172.22.98.254. But when I run it there it still gives me this output:
* Serving Flask app "main" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
So, when I use my postman doing this
where my post URL is http://172.22.98.254:5000/test, How can I use this from the server that I have. I have an ubuntu server.

By default, app.run() hosts server on localhost(127.0.0.1). To make it accessible,
app.run('0.0.0.0', port=5000)
Although, the server bundled with Flask is not for production, it is recommended to use WSGI server(mod_wsgi, nginx, gunicorn, etc.)
https://flask.palletsprojects.com/en/1.0.x/deploying/wsgi-standalone/

I have changed the host default ip address to server or Local network computer ip address. it works fine. My local ip address is 192.168.1.34, using same port as 5000.
app.run(host='192.168.1.34', port=5000)

Related

Flask App in Docker Container not accessible

I have a simple flask app, which runs in a docker container, which I do not want to access via the localhost. Therefore I set the host = "0.0.0.0".
Flask App:
if __name__ == "__main__":
app.run(
debug=True
, use_reloader = False
, host = "0.0.0.0"
)
When I run this locally everything works as expected.
Then I build my Docker with the following parameters:
# tell the port number the container should expose
EXPOSE 5000
# run the application
CMD ["python", "/usr/src/app/main.py" ]
Afterwards I build my docker container with the following command:
docker build -t my_app:latest .
and run it:
docker run -p 5000:5000 my_app:latest
It starts the container:
* Serving Flask app 'main' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on all addresses (0.0.0.0)
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://127.0.0.1:5000
* Running on http://172.17.0.2:5000 (Press CTRL+C to quit)
Somehow thecontainer is now running on two adresses and only http://127.0.0.1:5000 is accessible.
I already tried various proposed solution but cannot find a solution for my issue.
Thank you for your help!

Docker container Flask Api not responding to Postman request

I am trying to run a Flask API inside a Docker container. After running the container I get the following on terminal-
* Serving Flask app 'return-nlp' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on all addresses.
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://172.17.0.2:3000/ (Press CTRL+C to quit)
On sending a POST request on the given URL, I don't get any response. I have also tried sending the same request to http://127.0.0.1:3000/, my own IP Address but I don't get any response from the container and get the two responses on Postman-
Error: connect ECONNREFUSED 127.0.0.1:3000
Error: connect ETIMEDOUT
thanks to #KalusD. for suggesting using the -p option to publish the port on the host machine, this seems to have solved the issue.
we use the -p option to bind the port from the process running in the container to the port on the host machine
--publish , -p Publish a container's port(s) to the host
Check here for more info from the official docs.

Handling HTTPS post request in python flask

I have made a simple python flask program :
# save this as app.py
from flask import request
from flask import Flask
app = Flask(__name__)
#app.route("/", methods=['POST'])
def hello():
return "Hello, World!"
#app.route("/sms", methods=['POST'])
def sms():
print(request.get_json())
return "sms world"
if __name__ == '__main__':
app.run(port=443, host='0.0.0.0', ssl_context='adhoc')
This handles the HTTP post request. How can I make it handle HTTPS post requests?
When I execute the command flask run I get the following:
Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server
instead. * Debug mode: off * Running on http://127.0.0.1:5000/
(Press CTRL+C to quit)
So it still uses HTTP instead of https
pip install pyopenssl
When you run the script (or start with flask run if you prefer), you will notice that Flask indicates that it is running an https:// istance
first install pyopenssl with the command:
pip install pyopenssl
to launch it in https just add the parameter: ssl_context='adhoc'
if __name__ == '__main__':
app.run(port=443, host='0.0.0.0', ssl_context='adhoc')
Once started the following message will be shown:
* Serving Flask app 'test' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on all addresses.
WARNING: This is a development server. Do not use it in a production deployment.
* Running on https://192.168.0.62:443/ (Press CTRL+C to quit)
Obviously then once the application goes into production this parameter will no longer be needed, but you will have to set your wsgi to communicate in https
You can try ngrok, it is a useful tool to deploy an HTTPS and HTTP service from local to public net.
https://ngrok.com/

Running Flask app in debug mode does not provide the debugger is active or the Debugger PIN on the docker desktop logs

I have been using docker desktop in windows 10 and running the flask application with it. So when I change the python code the docker will auto restart and perform. We can be able to check the logs on the docker desktop's container logs. But now it just shows as below:
Serving Flask app "web_app" (lazy loading)
* Environment: development
* Debug mode: on
Instead it needs to be
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: 139-055-956
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
So that I can be able to check the live logs. Let me know if there is a solution for this to see the live logs with the mentioned details.

Mapping python flask website to a domain name

I have a domain, domain_name.com. On my local machine, a website is made using python flask. In the home directory of the server for domain_name.com, I put all the files in a folder 'school' and in the python3 virtual environment execute the command ./main.py runserver, which gives:
* Serving Flask app "memorizer.application" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
How do I map this to domain_name.com/school? The local machine is on ubuntu 18.04 and the server is linux millennium. I have access to the cpanel.
Below is the sequence to run the script on the server:
. cd school/
. ls
app.py memorizer.db Pipfile.lock setup.cfg
LICENSE migrations public tests
main.py passenger_wsgi.py questions tmp
memorizer Pipfile README.md tox.ini
. pwd
/home/user_name/school
. cat main.py
#!/usr/bin/env python3
from memorizer.application import manager
if __name__ == '__main__':
manager.run()
. source /home/user_name/virtualenv/school/3.5/bin/activate
(school:3.5). ./main.py runserver
* Serving Flask app "memorizer.application" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Categories

Resources