Skip to content

Commit 7be7d8c

Browse files
committed
* add in answer files for the two in-class labs
1 parent 98ff121 commit 7be7d8c

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/python
2+
import cgi
3+
import cgitb
4+
cgitb.enable()
5+
import os
6+
import datetime
7+
8+
9+
print "Content-Type: text/html"
10+
print
11+
12+
body = """<html>
13+
<head>
14+
<title>Lab 1 - CGI experiments</title>
15+
</head>
16+
<body>
17+
The server name is %s. (if an IP address, then a DNS problem) <br>
18+
<br>
19+
The server address is %s:%s.<br>
20+
<br>
21+
Your hostname is %s. <br>
22+
<br>
23+
You are coming from %s:%s.<br>
24+
<br>
25+
The currenly executing script is %s<br>
26+
<br>
27+
The request arrived at %s<br>
28+
29+
</body>
30+
</html>""" % (
31+
os.environ.get('SERVER_NAME', 'Unset'), # Server Hostname
32+
os.environ.get('SERVER_ADDR', 'Unset'), # server IP
33+
os.environ.get('SERVER_PORT', 'Unset'), # server port
34+
os.environ.get('REMOTE_HOST', 'Unset'), # client hostname
35+
os.environ.get('REMOTE_ADDR', 'Unset'), # client IP
36+
os.environ.get('REMOTE_PORT', 'Unset'), # client port
37+
os.environ.get('SCRIPT_NAME', 'Unset'), # this script name
38+
datetime.datetime.now().isoformat(), # time
39+
)
40+
41+
print body,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/python
2+
import datetime
3+
4+
body = """<html>
5+
<head>
6+
<title>Lab A - CGI experiments</title>
7+
</head>
8+
<body>
9+
10+
The server name is %s. (if an IP address, then a DNS problem) <br>
11+
<br>
12+
The server address is %s:%s.<br>
13+
<br>
14+
You are coming from %s:%s.<br>
15+
<br>
16+
The URI we are serving is %s.<br>
17+
<br>
18+
The request arrived at %s<br>
19+
20+
</body>
21+
</html>"""
22+
23+
def application(environ, start_response):
24+
response_body = body % (
25+
environ.get('SERVER_NAME', 'Unset'), # server name
26+
environ.get('SERVER_ADDR', 'Unset'), # server IP
27+
environ.get('SERVER_PORT', 'Unset'), # server port
28+
environ.get('REMOTE_ADDR', 'Unset'), # client IP
29+
environ.get('REMOTE_PORT', 'Unset'), # client port
30+
environ.get('SCRIPT_NAME', 'Unset'), # this script name
31+
datetime.datetime.now().isoformat(), # time
32+
)
33+
status = '200 OK'
34+
35+
response_headers = [('Content-Type', 'text/html'),
36+
('Content-Length', str(len(response_body)))]
37+
start_response(status, response_headers)
38+
39+
return [response_body]
40+
41+
if __name__ == '__main__':
42+
from wsgiref.simple_server import make_server
43+
srv = make_server('localhost', 8080, application)
44+
srv.serve_forever()

0 commit comments

Comments
 (0)