Thursday, May 9, 2013

Simple uwsgi + wsgi app

No Nginx, using uWSGI embedded HTTP server.

First, we create a Python virtual environment and install uswgi in it:

vic@vic-X202E ~/projects » mkvirtualenv -p /usr/bin/python uwsgi
Running virtualenv with interpreter /usr/bin/python
New python executable in uwsgi/bin/python
Installing setuptools............................done.
Installing pip...............done.

[uwsgi] vic@vic-X202E ~/projects » pip install uwsgi
Downloading/unpacking uwsgi
  Downloading uwsgi-1.9.9.tar.gz (617kB): 617kB downloaded
  Running egg_info for package uwsgi
Successfully installed uwsgi
Cleaning up...

Then we run uwsgi pointing it to our wsgi application:

[uwsgi] vic@vic-X202E ~/projects/test-uwsgi » uwsgi --http :8080 --wsgi myapp:app
*** Starting uWSGI 1.9.9 (64bit) on [Thu May  9 23:07:54 2013] ***
compiled with version: 4.7.3 on 09 May 2013 18:40:27
os: Linux-3.8.0-19-generic #29-Ubuntu SMP Wed Apr 17 18:16:28 UTC 2013
nodename: vic-X202E
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /home/vic/projects/test-uwsgi
detected binary path: /home/vic/projects/.envs/uwsgi/bin/uwsgi
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 28786
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
uWSGI http bound on fd 4
spawned uWSGI http 1 (pid: 8456)
uwsgi socket 0 bound to TCP address (port auto-assigned) fd 3
Python version: 2.7.4 (default, Apr 19 2013, 18:30:41)  [GCC 4.7.3]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x2034e10
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72688 bytes (70 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x2034e10 pid: 8455 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 8455, cores: 1)

import cgi

def app(environ, start_response):

    parameters = cgi.parse_qs(environ.get('QUERY_STRING', ''))

    # get the `name` parameter if exists
    name = parameters.get('name', ['World'])[0]

    # return headers
    start_response('200 OK', [('Content-Type', 'text/html')])

    # return body
    return ['Hello %s!' % name]

Now you see a simple "Hello world" page on URL http://localhost:8080/?name=Victor.

No comments:

Post a Comment