|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import socket |
| 4 | +import os |
| 5 | + |
| 6 | +import httpdate |
| 7 | + |
| 8 | +host = '' # listen on all connections (WiFi, etc) |
| 9 | +port = 50000 |
| 10 | +backlog = 5 # how many connections can we stack up |
| 11 | +size = 1024 # number of bytes to receive at once |
| 12 | + |
| 13 | +root_dir = 'web' # must be run from code dir... |
| 14 | + |
| 15 | +print "point your browser to http://localhost:%i"%port |
| 16 | + |
| 17 | +## create the socket |
| 18 | +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 19 | +# set an option to tell the OS to re-use the socket |
| 20 | +s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 21 | + |
| 22 | +# the bind makes it a server |
| 23 | +s.bind( (host,port) ) |
| 24 | +s.listen(backlog) |
| 25 | + |
| 26 | +html = open("tiny_html.html").read() |
| 27 | + |
| 28 | +mime_types={} |
| 29 | +mime_types['html'] = "text/html" |
| 30 | +mime_types['htm'] = "text/html" |
| 31 | +mime_types['txt'] = "text/plain" |
| 32 | +mime_types['png'] = "image/png" |
| 33 | +mime_types['jpeg'] = "image/jpg" |
| 34 | +mime_types['jpg'] = "image/jpg" |
| 35 | + |
| 36 | +def OK_response(entity, extension='html'): |
| 37 | + """ |
| 38 | + returns an HTTP response: header and entity in a string |
| 39 | + """ |
| 40 | + resp = [] |
| 41 | + resp.append('HTTP/1.1 200 OK') |
| 42 | + resp.append(httpdate.httpdate_now()) |
| 43 | + type = mime_types.get(extension, 'text/plain') |
| 44 | + resp.append( 'Content-Type: %s'%type ) |
| 45 | + resp.append('Content-Length: %i'%len(entity)) |
| 46 | + resp.append('') |
| 47 | + resp.append(entity) |
| 48 | + |
| 49 | + return "\r\n".join(resp) |
| 50 | + |
| 51 | +def Error_response(URI): |
| 52 | + """ |
| 53 | + returns an HTTP 404 Not Found Error response: |
| 54 | + |
| 55 | + URI is the name of the entity not found |
| 56 | + """ |
| 57 | + resp = [] |
| 58 | + resp.append('HTTP/1.1 404 Not Found') |
| 59 | + resp.append(httpdate.httpdate_now()) |
| 60 | + resp.append('Content-Type: text/plain') |
| 61 | + |
| 62 | + msg = "404 Error:\n %s \n not found"%( URI ) |
| 63 | + |
| 64 | + resp.append('Content-Length: %i'%( len(msg) ) ) |
| 65 | + resp.append('') |
| 66 | + resp.append(msg) |
| 67 | + |
| 68 | + return "\r\n".join(resp) |
| 69 | + |
| 70 | + |
| 71 | +def parse_request(request): |
| 72 | + """ |
| 73 | + parse an HTTP request |
| 74 | + |
| 75 | + returns the URI asked for |
| 76 | + |
| 77 | + note: minimal parsing -- only supprt GET |
| 78 | + |
| 79 | + example: |
| 80 | + GET / HTTP/1.1 |
| 81 | + Host: localhost:50000 |
| 82 | + User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:12.0) Gecko/20100101 Firefox/12.0 |
| 83 | + Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 |
| 84 | + Accept-Language: en-us,en;q=0.5 |
| 85 | + Accept-Encoding: gzip, deflate |
| 86 | + Connection: keep-alive |
| 87 | + Cache-Control: max-age=0 |
| 88 | + """ |
| 89 | + # first line should be the method line: |
| 90 | + lines = request.split("\r\n") |
| 91 | + |
| 92 | + method, URI, protocol = lines[0].split() |
| 93 | + |
| 94 | + # a bit of checking: |
| 95 | + if method.strip() != "GET": |
| 96 | + raise ValueError("I can only process a GET request") |
| 97 | + if protocol.split('/')[0] != "HTTP": |
| 98 | + raise ValueError("I can only process an HTTP request") |
| 99 | + |
| 100 | + return URI |
| 101 | + |
| 102 | +def format_dir_list(URI): |
| 103 | + """ |
| 104 | + format the contests of dir as HTML with links |
| 105 | + """ |
| 106 | + dir = os.path.join(root_dir, URI) |
| 107 | + names = os.listdir(dir) |
| 108 | + |
| 109 | + dirs = [d for d in names if os.path.isdir(os.path.join(dir,d))] |
| 110 | + files = [d for d in names if os.path.isfile(os.path.join(dir,d))] |
| 111 | + |
| 112 | + html =[] |
| 113 | + html.append("<http> <body>") |
| 114 | + html.append("<h2>%s</h2>"%URI) |
| 115 | + print "URI:", URI |
| 116 | + if URI: # don't need the parent dir at the root |
| 117 | + html.append('<a href="..">Parent</a>' ) |
| 118 | + html.append("<h3>Directories:</h3>") |
| 119 | + html.append(" <ul>") |
| 120 | + for d in dirs: |
| 121 | + html.append(' <li> <a href="%s">%s </a></li>'%(os.path.join(URI,d), d)) |
| 122 | + html.append(" </ul>") |
| 123 | + html.append("<h3>Files:</h3>") |
| 124 | + html.append(" <ul>") |
| 125 | + for f in files: |
| 126 | + html.append(' <li> <a href="%s"> %s </a> </li>'%(os.path.join(URI,f), f) ) |
| 127 | + html.append(" </ul>") |
| 128 | + html.append("</body> </http>") |
| 129 | + return "\n".join(html) |
| 130 | + |
| 131 | +def get_time_page(): |
| 132 | + """ |
| 133 | + returns and html page with the current time in it |
| 134 | + """ |
| 135 | + time = httpdate.httpdate_now() |
| 136 | + html = "<html> <body> <h1> %s </h1> </body> </html>"%time |
| 137 | + return html |
| 138 | + |
| 139 | +def get_file(URI): |
| 140 | + |
| 141 | + URI = URI.strip('/') #weird-- os.path.join does not like a leading slash |
| 142 | + # check if this is the time server option |
| 143 | + if URI.lower() == "get_time": |
| 144 | + return get_time_page(), 'html' |
| 145 | + else: |
| 146 | + filename = os.path.join( root_dir, URI) |
| 147 | + if os.path.isfile(filename): |
| 148 | + contents = open(filename, 'rb').read() |
| 149 | + ext = os.path.splitext(filename)[1].strip('.') |
| 150 | + return contents, ext |
| 151 | + elif os.path.isdir(filename): |
| 152 | + return format_dir_list(URI), 'htm' |
| 153 | + else: |
| 154 | + raise ValueError("there is nothing by that name") |
| 155 | + |
| 156 | + |
| 157 | +while True: # keep looking for new connections forever |
| 158 | + client, address = s.accept() # look for a connection |
| 159 | + request = client.recv(size) |
| 160 | + if request: # if the connection was closed there would be no data |
| 161 | + print "received:", request |
| 162 | + URI = parse_request(request) |
| 163 | + try: |
| 164 | + file_data, ext = get_file(URI) |
| 165 | + response = OK_response(file_data, ext) |
| 166 | + except ValueError: |
| 167 | + response = Error_response(URI) |
| 168 | + client.send(response) |
| 169 | + client.close() |
| 170 | + |
0 commit comments