-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathwebserver.py
More file actions
96 lines (77 loc) · 2.69 KB
/
Copy pathwebserver.py
File metadata and controls
96 lines (77 loc) · 2.69 KB
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
# Example program from Part 01 of the python-csp tutorial
# Copyright (C) Sarah Mount, 2010.
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# 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 General Public License for more details.
# You should have rceeived a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import time
from csp.cspprocess import *
import socket
def response(code, reason, page):
"""Construct and return a single HTTP response.
FIXME: Should read and return files from disk, not a static page.
FIXME: Should handle other MIME types.
"""
html = """
<html>
<head><title>%i %s</title></head>
<body>
%s
<hr/>
<p><strong>Date:</strong> %s</p>
</body>
</html>
""" % (code, reason, page, time.ctime())
template = """HTTP/1.0 %i %s
Content-Type: text/html
Content-Length: %i
%s
""" % (code, reason, len(html), html)
return template
@process
def server(host, port):
"""Simple CSP based web server.
"""
print('Running tutorial web-server on port {0}...'.format(port))
print('Interrupt with CTRL-C')
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((host, port))
sock.listen(5)
while True:
conn_sock, conn_addr = sock.accept()
request = conn_sock.recv(4096).strip()
if request.startswith('GET'):
handler_ok(request, conn_sock).start()
else:
handler_not_found(request, conn_sock).start()
@process
def handler_ok(request, conn_sock):
"""Handle a single HTTP 200 OK request.
"""
page = '<h1>My python-csp web server!</h1>'
page += '<p><strong>You asked for:</strong><pre>%s</pre></p>' % request
conn_sock.send(response(200, 'OK', page))
conn_sock.shutdown(socket.SHUT_RDWR)
conn_sock.close()
return
@process
def handler_not_found(request, conn_sock):
"""Handle a single HTTP 404 Not Found request.
"""
page = '<h1>Cannot find your file</h1>'
page += '<p><strong>You asked for:</strong><pre>%s</pre></p>' % request
conn_sock.send(response(404, 'Not Found', page))
conn_sock.shutdown(socket.SHUT_RDWR)
conn_sock.close()
return
if __name__ == '__main__':
host = ''
port = 8888
server(host, port).start()