uWSGI Server
uWSGI is an open-source, fast, and self-healing application server container that serves as a full stack for building hosting services. It is primarily known for efficiently running Python WSGI applications but supports various other languages and protocols (like Ruby/Rack, Perl/PSGI, PHP, Go) through its pluggable architecture. The current stable version is 2.0.31. The project has been in maintenance mode since October 2022, focusing on stability rather than new features.
Warnings
- gotcha Installing `uwsgi` via `pip` often fails without system-level build tools (C compiler, Python development headers). This is because uWSGI is a C application that gets compiled during installation.
- gotcha Python threads are disabled by default within uWSGI's Python plugin. Application-generated threads will not run unless explicitly enabled.
- breaking Never expose a socket speaking the native `uwsgi` protocol directly to the public network, as it allows dynamic loading of applications (arbitrary code execution).
- gotcha If an HTTP request contains a body (e.g., POST request from a form), your application *must* read (consume) the entire body. Failure to do so can 'clobber' the communication socket with the web server, leading to hung connections or errors.
- gotcha When using UNIX sockets, they are filesystem objects subject to standard UNIX permissions. If your web server lacks write access, it cannot communicate with uWSGI.
- gotcha Receiving 'invalid request block size' errors in logs often indicates that the default internal buffer size (4096 bytes) for request headers is too small.
- gotcha Running uWSGI instances directly as the `root` user is a security risk.
Install
-
pip install uwsgi
Imports
- uwsgi
import uwsgi
Quickstart
import os
def application(environ, start_response):
status = '200 OK'
headers = [('Content-type', 'text/plain')]
start_response(status, headers)
return [b"Hello from uWSGI and Python!"]
# To run: save this as `wsgi_app.py`
# Then execute from your terminal:
# uwsgi --http :9090 --wsgi-file wsgi_app.py --callable application --processes 1 --threads 1