Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
####################################################################################################
#
# 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()