forked from ctb/cse491-webz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
73 lines (62 loc) · 2.14 KB
/
app.py
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
#! /usr/bin/env python
from wsgiref.simple_server import make_server
import urlparse
class SimpleApp(object):
def __call__(self, environ, start_response):
status = '200 OK'
path = environ['PATH_INFO']
if path == '/':
content_type = 'text/html'
data = """\
Visit:
<a href='content'>a file</a>,
<a href='error'>an error</a>,
<a href='helmet'>an image</a>,
<a href='somethingelse'>something else</a>, or
<a href='form'>a form...</a>
<p>
<img src='/helmet'>
"""
elif path == '/content':
content_type = 'text/html'
data = open('somefile.html').read()
elif path == '/error':
status = "404 Not Found"
content_type = 'text/html'
data = "Couldn't find your stuff."
elif path == '/helmet':
content_type = 'image/gif'
data = open('Spartan-helmet-Black-150-pxls.gif', 'rb').read()
elif path == '/form':
content_type = 'text/html'
data = form()
elif path == '/recv':
formdata = environ['QUERY_STRING']
results = urlparse.parse_qs(formdata)
firstname = results['firstname'][0]
lastname = results['lastname'][0]
content_type = 'text/html'
data = "First name: %s; last name: %s. <a href='./'>return to index</a>" % (firstname, lastname)
else:
content_type = 'text/plain'
data = "Hello, world; got path request %s" % environ['PATH_INFO']
headers = [('Content-type', content_type)]
start_response(status, headers)
return [data]
def form():
return """
<form action='recv'>
Your first name? <input type='text' name='firstname' size'20'>
Your last name? <input type='text' name='lastname' size='20'>
<input type='submit'>
</form>
"""
if __name__ == '__main__':
import random, socket
port = random.randint(8000, 9999)
app = SimpleApp()
httpd = make_server('', port, app)
print "Serving on port %d..." % port
print "Try using a Web browser to go to http://%s:%d/" % \
(socket.getfqdn(), port)
httpd.serve_forever()