Skip to content
fake-wsgi-server.py 4.2 KiB
Newer Older
Fabrice Salvaire's avatar
Fabrice Salvaire committed
####################################################################################################
#
# Climbing Asso Portal - A Portal for Climbing Club (Association)
# Copyright (C) 2018 Fabrice Salvaire
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
####################################################################################################

####################################################################################################
#
# cf. https://docs.python.org/3.6/library/wsgiref.html
#     https://www.python.org/dev/peps/pep-3333
#
# Test using
#
#   server side
#     python fake-wsgi-server.py &
#       or
#     /.../gunicorn --bind 127.0.0.1:8000 fake-wsgi-server:demo_app
#
#   client side
#     http localhost:8000
#
####################################################################################################

####################################################################################################
#
# HTTP/1.0 200 OK
# Content-type: text/plain; charset=utf-8
# Date: Wed, 11 Apr 2018 17:09:05 GMT
# Server: WSGIServer/0.2 CPython/3.6.5
#
# ...
# _: /home/opt/python-virtual-env/py36/bin/python
# SERVER_NAME: isis.localdomain
# GATEWAY_INTERFACE: CGI/1.1
# SERVER_PORT: 8000
# REMOTE_HOST:
# CONTENT_LENGTH:
# SCRIPT_NAME:
# SERVER_PROTOCOL: HTTP/1.1
# SERVER_SOFTWARE: WSGIServer/0.2
# REQUEST_METHOD: GET
# PATH_INFO: /
# QUERY_STRING:
# REMOTE_ADDR: 127.0.0.1
# CONTENT_TYPE: text/plain
# HTTP_HOST: localhost:8000
# HTTP_USER_AGENT: HTTPie/0.9.4
# HTTP_ACCEPT_ENCODING: gzip, deflate
# HTTP_ACCEPT: */*
# HTTP_CONNECTION: keep-alive
# wsgi.input: <_io.BufferedReader name=4>
# wsgi.errors: <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
# wsgi.version: (1, 0)
# wsgi.run_once: False
# wsgi.url_scheme: http
# wsgi.multithread: True
# wsgi.multiprocess: False
# wsgi.file_wrapper: <class 'wsgiref.util.FileWrapper'>
#
####################################################################################################

####################################################################################################

from wsgiref.simple_server import make_server, demo_app
from wsgiref.util import setup_testing_defaults

####################################################################################################

def hello_application(environ, start_response):

    # environ is a dictionary containing CGI environment variables as well as other request
    # parameters and metadata under well-defined keys.

    # start_response is a callable taking two positional parameters, status and response_headers.
    start_response('200 OK', [('Content-Type', 'text/plain')])

    # Makes the callable into a generator.
    # The body of the response is returned as an iterable of byte strings.
    yield b'Hello, World\n'

####################################################################################################

def environ_application(environ, start_response):

    # Update environ with trivial defaults for testing purposes.
    setup_testing_defaults(environ)

    status = '200 OK'
    headers = [('Content-type', 'text/plain; charset=utf-8')]

    start_response(status, headers)

    ret = [('{}: {}\n'.format(key, value)).encode('utf-8')
           for key, value in environ.items()]
    return ret

####################################################################################################

if __name__ == '__main__':

    host = ''
    port = 8000
    app = demo_app
    # app = environ_application
    # app = hello_application

    with make_server(host, port, app) as httpd:
        print('Listening on port {} ...'.format(port))
        httpd.serve_forever()