Python/Flask: Difference between revisions
< Python
| Line 76: | Line 76: | ||
<p>Hello, World!</p> | <p>Hello, World!</p> | ||
</pre> | </pre> | ||
== WSGI == | |||
=== Apache WSGI === | |||
<pre> | |||
$ cd hello-app | |||
$ python -m venv .venv | |||
$ . .venv/bin/activate | |||
$ pip install . # install your application | |||
$ pip install mod_wsgi | |||
</pre> | |||
wsgi.py : | |||
<pre> | |||
## IIMPORT PATTERN | |||
from hello import app | |||
application = app | |||
## OR | |||
## FACTORY PATTERN | |||
from hello import create_app | |||
application = create_app() | |||
</pre> | |||
Configure mod_wsgi-express and specify the number of worker processes: | |||
mod_wsgi-express start-server wsgi.py --processes 4 | |||
Example running mod_wsgi-express as root, to bind to port 80/443, but then drop permissions and binding app as different user: | |||
sudo /home/hello/.venv/bin/mod_wsgi-express start-server /home/hello/wsgi.py \ | |||
--user hello --group hello --port 80 --processes 4 | |||
See https://flask.palletsprojects.com/en/stable/deploying/mod_wsgi/ | |||
== keywords == | == keywords == | ||
Revision as of 22:19, 13 December 2025
Flask
Installation
## Linux / macOS mkdir myproject cd myproject # python3 -m venv .flask python3 -m venv .venv . .venv/bin/activate
## Windows mkdir myproject cd myproject py -3 -m venv .venv .venv\Scripts\activate
## Next Common Step # pip install Flask pip3 install Flask
# End deactivate
References:
Quick Start
Flask Hello World
helloworld.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>\n"
Run the server:
flask --app helloworld run
NOTE: As a shortcut, if the file is named app.py or wsgi.py, you don’t have to use --app. See Command Line Interface for more details.
- Meaning, just simply rename helloworld.py to app.py (or wsgi.py) and you can simply run run:
flask run
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
Note:
* Running on http://127.0.0.1:5000
Test the server:
curl http://127.0.0.1:5000 <p>Hello, World!</p>
References:
Hello World Headers
$ curl -i http://127.0.0.1:5000 HTTP/1.1 200 OK Server: Werkzeug/3.1.4 Python/3.12.3 Date: Sat, 13 Dec 2025 22:05:42 GMT Content-Type: text/html; charset=utf-8 Content-Length: 20 Connection: close <p>Hello, World!</p>
WSGI
Apache WSGI
$ cd hello-app $ python -m venv .venv $ . .venv/bin/activate $ pip install . # install your application $ pip install mod_wsgi
wsgi.py :
## IIMPORT PATTERN from hello import app application = app ## OR ## FACTORY PATTERN from hello import create_app application = create_app()
Configure mod_wsgi-express and specify the number of worker processes:
mod_wsgi-express start-server wsgi.py --processes 4
Example running mod_wsgi-express as root, to bind to port 80/443, but then drop permissions and binding app as different user:
sudo /home/hello/.venv/bin/mod_wsgi-express start-server /home/hello/wsgi.py \ --user hello --group hello --port 80 --processes 4
See https://flask.palletsprojects.com/en/stable/deploying/mod_wsgi/