Mapping python flask website to a domain name - python

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)

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!

Name error depending on debug mode of python flask application

When running the flask app 'app.py' without debug mode, everything runs fine:
app = Flask(__name__)
#app.route("/")
def home():
return "Hey there"
if __name__=="__main__":
app.run()
Server startign:
* 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: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
When running the Flask app in debug mode
app.run(debug=True) I'll get "No module named app" error and the server won't start:
* 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
No module named app
I have noticed that when starting the script in vsCode without debug mode (ctrl+f5) or press the button 'Run Python File' everything works fine.
When I start debugging (f5) in vsCode the 'No module named app' occurs and after the failed server start I jump back to the parent folder (app.py is in /folder/app/app.py - after server start failed I am in the terminal in the '/folder' rather then '/folder/app' like before starting the script.
Does anyone got an idea where the problem is? Appreciate any help
The problem can be resolved by adding a launch.json file to the workspace and adding either an absolute to your app.py file:
"cwd":"path/to/folder/"
or a relative path to the directory of the executed script:
"cwd":"${fileDirname}"

python flask to production server

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)

'flask run' working in one conda env but not another

flask run won't work when I try it in a new conda env but works fine in an older conda env.
I've deleted and recreated new-env and reinstalled flask with the same version: 1.0.3.
(new-env) C:\Users\myname\PycharmProjects\myproject\backend>set FLASK_APP=my_app.py
(new-env) C:\Users\myname\PycharmProjects\myproject\backend>set FLASK_DEBUG=1
(new-env) C:\Users\myname\PycharmProjects\myproject\backend>flask run
* Serving Flask app "my_app.py" (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
c:\users\myname\anaconda3\envs\new-env\python.exe: No module named C:\Users\myname\Anaconda3\envs\new-env\Scripts\flask
Switching to older conda env:
(old-env) C:\Users\myname\PycharmProjects\myproject\backend>flask run
* 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)

Unable to load python flask API with a docker container

I'm trying to learn Docker containers with APIs. I have created a simple Hello World python REST API with flask:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == '__main__':
app.run(host="127.0.0.1", debug=True, port=8080)
This works when I run the script and go to http://localhost:8080/
This is my Dockerfile:
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /hello_world
# Copy the current directory contents into the container at /app
ADD . /hello_world
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
EXPOSE 8080
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "hello_world.py"]
requirements.txt:
Flask
My current directory contains Dockerfile, hello_world.py and requirements.txt.
I can successfully build the image with docker build -t hello_world ."
Running it with docker run -p 8080:8080 -t hello_world gives me the following output:
Serving Flask app "hello_world" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 985-433-141
When I try going to http://127.0.0.1:8080/ I get the "Can't reach this page" error. Do you know what I'm doing wrong? Thank you.

Categories

Resources