-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp_serve6.py
More file actions
162 lines (131 loc) · 4.45 KB
/
http_serve6.py
File metadata and controls
162 lines (131 loc) · 4.45 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
import socket
import os
import httpdate
host = '' # listen on all connections (WiFi, etc)
port = 50000
backlog = 5 # how many connections can we stack up
size = 1024 # number of bytes to receive at once
print "point your browser to http://localhost:%i"%port
## create the socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# set an option to tell the OS to re-use the socket
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# the bind makes it a server
s.bind( (host,port) )
s.listen(backlog)
html = open("tiny_html.html").read()
mime_types={}
mime_types['html'] = "text/html"
mime_types['htm'] = "text/html"
mime_types['txt'] = "text/plain"
mime_types['png'] = "image/png"
mime_types['jpeg'] = "image/jpg"
mime_types['jpg'] = "image/jpg"
def OK_response(entity, extension='html'):
"""
returns an HTTP response: header and entity in a string
"""
resp = []
resp.append('HTTP/1.1 200 OK')
resp.append(httpdate.httpdate_now())
type = mime_types.get(extension, 'text/plain')
resp.append( 'Content-Type: %s'%type )
resp.append('Content-Length: %i'%len(entity))
resp.append('')
resp.append(entity)
return "\r\n".join(resp)
def Error_response(URI):
"""
returns an HTTP 404 Not Found Error response:
URI is the name of the entity not found
"""
resp = []
resp.append('HTTP/1.1 404 Not Found')
resp.append(httpdate.httpdate_now())
resp.append('Content-Type: text/plain')
msg = "404 Error:\n %s \n not found"%( URI )
resp.append('Content-Length: %i'%( len(msg) ) )
resp.append('')
resp.append(msg)
return "\r\n".join(resp)
def parse_request(request):
"""
parse an HTTP request
returns the URI asked for
note: minimal parsing -- only supprt GET
example:
GET / HTTP/1.1
Host: localhost:50000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:12.0) Gecko/20100101 Firefox/12.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Cache-Control: max-age=0
"""
# first line should be the method line:
lines = request.split("\r\n")
print lines
method, URI, protocol = lines[0].split()
print method
print URI
print protocol
# a bit of checking:
if method.strip() != "GET":
raise ValueError("I can only process a GET request")
if protocol.split('/')[0] != "HTTP":
raise ValueError("I can only process an HTTP request")
return URI
def format_dir_list(dir_list):
"""
format the dir list as HTML
"""
html = ["<html> <body>"]
html.append(" <h1>Directory Listing</h1>")
for d in dir_list:
html.append( "<p> %s </p>"%d )
html.append( "</body> </html>" )
return "\n".join(html)
def get_time_page():
"""
returns and html page with the current time in it
"""
time = httpdate.httpdate_now()
html = "<html> <body> <h1> %s </h1> </body> </html>"%time
return html
def get_file(URI):
root_dir = 'web' # must be run from code dir...
URI = URI.strip('/') #weird-- os.path.join does not like a leading slash
# check if this is the time server option
if URI.lower() == "get_time":
return get_time_page(), 'html'
else:
filename = os.path.join( root_dir, URI)
print "path to file:", filename
if os.path.isfile(filename):
print "it's a file"
contents = open(filename, 'rb').read()
ext = os.path.splitext(filename)[1].strip('.')
return contents, ext
elif os.path.isdir(filename):
print "it's a dir"
return format_dir_list(os.listdir(filename)), 'htm'
else:
raise ValueError("there is nothing by that name")
while True: # keep looking for new connections forever
client, address = s.accept() # look for a connection
request = client.recv(size)
if request: # if the connection was closed there would be no data
print "received:", request
URI = parse_request(request)
print "URI requested is:", URI
try:
file_data, ext = get_file(URI)
response = OK_response(file_data, ext)
except ValueError:
response = Error_response(URI)
print "sending:"
print response[:200]
client.send(response)
client.close()