Through the previous two:
- Python Web development: Start with WSGI
- Python Web Development: Develop WSGI middleware
We already know that our usual Python Web projects, such as Django and Flask, are essentially callable objects that take in specific parameters, complete the business logic, and return data. An important intention of wsGI Ref is to decouple app and server and provide developers with a richer choice through a well-regulated API.
This article will deploy a simple app using uWSGi Server. This app could not be simpler, but it will give you a better understanding of the nature of Python Web Apps.
The app code (app.py) is only 3 lines long:
def application(env, start_response):
start_response('200 OK', [('Content-Type'.'text/html')])
return ["Hello World".encode('utf-8')]
Copy the code
Most of the following is from the official UWSGI documentation:
Note that py2 and Py3 return a different Response body in their own versions of Python. See the previous article for details. Python3 is used by default for this article.
Install the necessary environments and dependencies:
apt-get install build-essential python-dev -y
pip3 install uwsgi
Copy the code
Run the following command:
uwsgi --http :9090 --wsgi-file app.py
Copy the code
This will create an HTTP server on port 9090.
Then open port 9090 on the server:
sudo iptables -A INPUT -p tcp --dport 9090 -j ACCEPT
iptables-save
Copy the code
Visit this address: lcj.im:9090/ and you can see the output of Hello World.
Of course, the production environment can’t be so simple. It’s much more complicated than this. But this simple example will help you see the nature of Python Web apps, and give you a fuller view of Web frameworks like Django and Flask.
If you love computer science and basic logic like me, welcome to follow my wechat official account: