|
| 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