#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
from cgi import escape
def index(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html;charset=utf-8')])
return ['''Egyszerű Helló Világ wsgi-cgi app
httl://localhost:1234/hello/név ''']
def hello(environ, start_response):
args = environ['myapp.url_args']
if args:
subject = escape(args[0])
else:
subject = 'Világ'
start_response('200 OK', [('Content-Type', 'text/html;charset=utf-8')])
return ['''Helló %(subject)s !''' % {'subject': subject}]
def not_found(environ, start_response, path):
start_response('404 NOT FOUND', [('Content-Type', 'text/html;charset=utf-8')])
return ['A kért oldal [%(site)s] nem található!' % {'site': path}]
urls = [
(r'^$', index),
(r'hello/?$', hello),
(r'hello/(.+)$', hello)
]
def application(environ, start_response):
path = environ.get('PATH_INFO', '').lstrip('/')
for regex, callback in urls:
match = re.search(regex, path)
if match is not None:
environ['myapp.url_args'] = match.groups()
return callback(environ, start_response)
return not_found(environ, start_response, path)
if __name__ == '__main__':
from wsgiref.simple_server import make_server
srv = make_server('localhost', 1234, application)
srv.serve_forever()