-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp_serve4.py
More file actions
129 lines (105 loc) · 3.55 KB
/
http_serve4.py
File metadata and controls
129 lines (105 loc) · 3.55 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
#!/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)
def OK_response(entity):
"""
returns an HTTP response: header and entity in a string
"""
resp = []
resp.append('HTTP/1.1 200 OK')
resp.append(httpdate.httpdate_now())
resp.append( 'Content-Type: text/plain' )
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):
msg = ["Directory Listing:"]
for d in dir_list:
msg.append(d)
return "\n".join(msg)
def get_file(URI):
root_dir = 'web' # must be run from code dir...
URI = URI.lstrip('/') # os.path.join does not like a leading slash
filename = os.path.join( root_dir, URI)
print "path to file:", filename
if os.path.isfile(filename):
print "it's a file"
raise NotImplementedError("I can't handle a file yet")
elif os.path.isdir(filename):
print "it's a dir"
return format_dir_list(os.listdir(filename)), 'txt'
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)
except ValueError as err:
print err
response = Error_response(URI)
print "sending:"
print response[:200]
client.send(response)
client.close()