Skip to content

Commit 03c1122

Browse files
committed
2 parents 9ad73cf + 3b20be2 commit 03c1122

48 files changed

Lines changed: 4366 additions & 130 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
3+
import socket
4+
5+
host = '' # listen on all connections (WiFi, etc)
6+
port = 50000
7+
backlog = 5 # how many connections can we stack up
8+
size = 1024 # number of bytes to receive at once
9+
10+
print "point your browser to http://localhost:%i"%port
11+
12+
## create the socket
13+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
14+
# set an option to tell the OS to re-use the socket
15+
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
16+
17+
# the bind makes it a server
18+
s.bind( (host,port) )
19+
s.listen(backlog)
20+
21+
html = open("tiny_html.html").read()
22+
23+
while True: # keep looking for new connections forever
24+
client, address = s.accept() # look for a connection
25+
request = client.recv(size)
26+
if request: # if the connection was closed there would be no data
27+
print "received:", request
28+
client.send(html)
29+
client.close()
30+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python
2+
3+
import socket
4+
5+
import httpdate
6+
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+
print "point your browser to http://localhost:%i"%port
14+
15+
## create the socket
16+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
17+
# set an option to tell the OS to re-use the socket
18+
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
19+
20+
# the bind makes it a server
21+
s.bind( (host,port) )
22+
s.listen(backlog)
23+
24+
html = open("tiny_html.html").read()
25+
26+
def OK_response(entity):
27+
"""
28+
returns an HTTP response: header and entity in a string
29+
"""
30+
resp = []
31+
resp.append('HTTP/1.1 200 OK')
32+
resp.append(httpdate.httpdate_now())
33+
resp.append('Content-Type: text/html')
34+
resp.append('Content-Length: %i'%len(entity))
35+
resp.append('')
36+
resp.append(entity)
37+
38+
return "\r\n".join(resp)
39+
40+
while True: # keep looking for new connections forever
41+
client, address = s.accept() # look for a connection
42+
request = client.recv(size)
43+
if request: # if the connection was closed there would be no data
44+
print "received:"
45+
print request
46+
response = OK_response(html)
47+
print "sending:"
48+
print response[:120]
49+
client.send(response)
50+
client.close()
51+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env python
2+
3+
import socket
4+
5+
import httpdate
6+
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+
print "point your browser to http://localhost:%i"%port
14+
15+
## create the socket
16+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
17+
# set an option to tell the OS to re-use the socket
18+
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
19+
20+
# the bind makes it a server
21+
s.bind( (host,port) )
22+
s.listen(backlog)
23+
24+
html = open("tiny_html.html").read()
25+
26+
def OK_response(entity):
27+
"""
28+
returns an HTTP response: header and entity in a string
29+
"""
30+
resp = []
31+
resp.append('HTTP/1.1 200 OK')
32+
resp.append(httpdate.httpdate_now())
33+
resp.append('Content-Type: text/html')
34+
resp.append('Content-Length: %i'%len(entity))
35+
resp.append('')
36+
resp.append(entity)
37+
38+
return "\r\n".join(resp)
39+
40+
def parse_request(request):
41+
"""
42+
parse an HTTP request
43+
44+
returns the URI asked for
45+
46+
note: minimal parsing -- only supprt GET
47+
48+
example:
49+
GET / HTTP/1.1
50+
Host: localhost:50000
51+
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:12.0) Gecko/20100101 Firefox/12.0
52+
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
53+
Accept-Language: en-us,en;q=0.5
54+
Accept-Encoding: gzip, deflate
55+
Connection: keep-alive
56+
Cache-Control: max-age=0
57+
"""
58+
# first line should be the method line:
59+
lines = request.split("\r\n")
60+
print lines
61+
62+
method, URI, protocol = lines[0].split()
63+
print method
64+
print URI
65+
print protocol
66+
# a bit of checking:
67+
if method.strip() != "GET":
68+
raise ValueError("I can only process a GET request")
69+
if protocol.split('/')[0] != "HTTP":
70+
raise ValueError("I can only process an HTTP request")
71+
72+
return URI
73+
74+
while True: # keep looking for new connections forever
75+
client, address = s.accept() # look for a connection
76+
request = client.recv(size)
77+
if request: # if the connection was closed there would be no data
78+
print "received:", request
79+
URI = parse_request(request)
80+
print "URI requested is:", URI
81+
response = OK_response(html)
82+
print "sending:"
83+
print response[:120]
84+
client.send(response)
85+
client.close()
86+
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
print "point your browser to http://localhost:%i"%port
14+
15+
## create the socket
16+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
17+
# set an option to tell the OS to re-use the socket
18+
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
19+
20+
# the bind makes it a server
21+
s.bind( (host,port) )
22+
s.listen(backlog)
23+
24+
def OK_response(entity):
25+
"""
26+
returns an HTTP response: header and entity in a string
27+
"""
28+
resp = []
29+
resp.append('HTTP/1.1 200 OK')
30+
resp.append(httpdate.httpdate_now())
31+
resp.append( 'Content-Type: text/plain' )
32+
resp.append('Content-Length: %i'%len(entity))
33+
resp.append('')
34+
resp.append(entity)
35+
36+
return "\r\n".join(resp)
37+
38+
def Error_response(URI):
39+
"""
40+
returns an HTTP 404 Not Found Error response:
41+
42+
URI is the name of the entity not found
43+
"""
44+
resp = []
45+
resp.append('HTTP/1.1 404 Not Found')
46+
resp.append(httpdate.httpdate_now())
47+
resp.append('Content-Type: text/plain')
48+
49+
msg = "404 Error:\n %s \n not found"%( URI )
50+
51+
resp.append('Content-Length: %i'%( len(msg) ) )
52+
resp.append('')
53+
resp.append(msg)
54+
55+
return "\r\n".join(resp)
56+
57+
58+
def parse_request(request):
59+
"""
60+
parse an HTTP request
61+
62+
returns the URI asked for
63+
64+
note: minimal parsing -- only supprt GET
65+
66+
example:
67+
GET / HTTP/1.1
68+
Host: localhost:50000
69+
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:12.0) Gecko/20100101 Firefox/12.0
70+
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
71+
Accept-Language: en-us,en;q=0.5
72+
Accept-Encoding: gzip, deflate
73+
Connection: keep-alive
74+
Cache-Control: max-age=0
75+
"""
76+
# first line should be the method line:
77+
lines = request.split("\r\n")
78+
print lines
79+
80+
method, URI, protocol = lines[0].split()
81+
print method
82+
print URI
83+
print protocol
84+
# a bit of checking:
85+
if method.strip() != "GET":
86+
raise ValueError("I can only process a GET request")
87+
if protocol.split('/')[0] != "HTTP":
88+
raise ValueError("I can only process an HTTP request")
89+
90+
return URI
91+
92+
def format_dir_list(dir_list):
93+
msg = ["Directory Listing:"]
94+
for d in dir_list:
95+
msg.append(d)
96+
return "\n".join(msg)
97+
98+
def get_file(URI):
99+
root_dir = 'web' # must be run from code dir...
100+
URI = URI.lstrip('/') # os.path.join does not like a leading slash
101+
filename = os.path.join( root_dir, URI)
102+
print "path to file:", filename
103+
if os.path.isfile(filename):
104+
print "it's a file"
105+
raise NotImplementedError("I can't handle a file yet")
106+
elif os.path.isdir(filename):
107+
print "it's a dir"
108+
return format_dir_list(os.listdir(filename)), 'txt'
109+
else:
110+
raise ValueError("there is nothing by that name")
111+
112+
while True: # keep looking for new connections forever
113+
client, address = s.accept() # look for a connection
114+
request = client.recv(size)
115+
if request: # if the connection was closed there would be no data
116+
print "received:", request
117+
URI = parse_request(request)
118+
print "URI requested is:", URI
119+
try:
120+
file_data, ext = get_file(URI)
121+
response = OK_response(file_data)
122+
except ValueError as err:
123+
print err
124+
response = Error_response(URI)
125+
print "sending:"
126+
print response[:200]
127+
client.send(response)
128+
client.close()
129+

0 commit comments

Comments
 (0)