Skip to content

Commit c90bbea

Browse files
committed
added files for week 5
1 parent b455830 commit c90bbea

9 files changed

Lines changed: 1256 additions & 883 deletions

File tree

week-04/presentation-week04.tex

Lines changed: 1040 additions & 883 deletions
Large diffs are not rendered by default.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import socket
2+
3+
host = 'localhost'
4+
port = 50000
5+
size = 1024
6+
s = socket.socket(socket.AF_INET,
7+
socket.SOCK_STREAM)
8+
s.connect((host,port))
9+
s.send('Hello, world')
10+
data = s.recv(size)
11+
s.close()
12+
print 'Received:', data
13+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import socket
2+
3+
host = ''
4+
port = 50000
5+
backlog = 5
6+
size = 1024
7+
s = socket.socket(socket.AF_INET,
8+
socket.SOCK_STREAM)
9+
s.bind((host,port))
10+
s.listen(backlog)
11+
while True:
12+
client, address = s.accept()
13+
data = client.recv(size)
14+
if data:
15+
client.send(data)
16+
client.close()
17+

week-05/code/Brian's/favicon.ico

1.14 KB
Binary file not shown.

week-05/code/Brian's/print_time.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import datetime
2+
print datetime.datetime.now().isoformat()
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#
2+
# ws30 -- the thirty minute web server
3+
# author: Wilhelm Fitzpatrick (rafial@well.com)
4+
# date: August 3rd, 2002
5+
#
6+
# Written after attending a Dave Thomas talk at PNSS and hearing about
7+
# his "write a web server in Ruby in one hour" challenge.
8+
#
9+
# Actual time spent:
10+
# 30 minutes reading socket man page
11+
# 30 minutes coding to first page fetched
12+
# 3 hours making it prettier & more pythonic
13+
#
14+
# updated by Brian Dorsey
15+
#
16+
17+
import os
18+
import socket
19+
20+
HOST = ""
21+
PORT = 8080
22+
MIME_TYPES = {'.jpg': 'image/jpg',
23+
'.gif': 'image/gif',
24+
'.png': 'image/png',
25+
'.html': 'text/html',
26+
'.pdf': 'application/pdf'}
27+
28+
29+
RESPONSE_HEADERS = {}
30+
31+
RESPONSE_HEADERS[200] =\
32+
"""HTTP/1.0 200 Okay
33+
Server: ws30
34+
Content-type: %s
35+
36+
%s
37+
"""
38+
39+
RESPONSE_HEADERS[301] =\
40+
"""HTTP/1.0 301 Moved
41+
Server: ws30
42+
Content-type: text/plain
43+
Location: %s
44+
45+
moved
46+
"""
47+
48+
RESPONSE_HEADERS[404] =\
49+
"""HTTP/1.0 404 Not Found
50+
Server: ws30
51+
Content-type: text/plain
52+
53+
%s not found
54+
"""
55+
56+
DIRECTORY_LISTING =\
57+
"""<html>
58+
<head><title>%s</title></head>
59+
<body>
60+
<a href="%s..">..</a><br>
61+
%s
62+
</body>
63+
</html>
64+
"""
65+
66+
DIRECTORY_LINE = '<a href="%s">%s</a><br>'
67+
68+
69+
def server_socket(host, port):
70+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
71+
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
72+
s.bind((host, port))
73+
s.listen(1)
74+
return s
75+
76+
77+
def parse_request(sock):
78+
data = sock.recv(4096)
79+
if not data:
80+
print "Bad request: no data"
81+
return ''
82+
line = data[0:data.find("\r")]
83+
print line
84+
method, uri, protocol = line.split()
85+
#headers = data[0:data.find("\r\n\r\n")]
86+
#print headers
87+
return uri
88+
89+
90+
def list_directory(uri):
91+
entries = os.listdir('.' + uri)
92+
entries.sort()
93+
return DIRECTORY_LISTING % (uri, uri, '\n'.join(
94+
[DIRECTORY_LINE % (e, e) for e in entries]))
95+
96+
97+
def get_file(path):
98+
f = open(path)
99+
try:
100+
return f.read()
101+
finally:
102+
f.close()
103+
104+
105+
def get_content(uri):
106+
try:
107+
path = '.' + uri
108+
if os.path.isfile(path):
109+
return (200, get_mime(uri), get_file(path))
110+
if os.path.isdir(path):
111+
if(uri.endswith('/')):
112+
return (200, 'text/html', list_directory(uri))
113+
else:
114+
return (301, uri + '/')
115+
else:
116+
return (404, uri)
117+
except IOError, e:
118+
return (404, e)
119+
120+
121+
def get_mime(uri):
122+
return MIME_TYPES.get(os.path.splitext(uri)[1], 'text/plain')
123+
124+
125+
def send_response(sock, content):
126+
template = RESPONSE_HEADERS[content[0]]
127+
data = template % content[1:]
128+
sock.sendall(data)
129+
130+
131+
if __name__ == '__main__':
132+
server = server_socket(HOST, int(PORT))
133+
print 'starting %s on %s...' % (HOST, PORT)
134+
try:
135+
while True:
136+
sock, client_address = server.accept()
137+
uri = parse_request(sock)
138+
if uri:
139+
content = get_content(uri)
140+
send_response(sock, content)
141+
sock.close()
142+
except KeyboardInterrupt:
143+
print 'shutting down...'
144+
server.close()
9.07 MB
Binary file not shown.
14.4 MB
Binary file not shown.

week-05/code/socket_serve1.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Example from the HowTo:
5+
6+
http://docs.python.org/howto/sockets.html
7+
8+
Then edited by Chris Barker
9+
"""
10+
11+
import socket
12+
13+
#create an INET, STREAMing socket
14+
serversocket = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
15+
16+
#bind the socket to localhost, high port
17+
serversocket.bind(('localhost', 55559),)
18+
#become a server socket
19+
serversocket.listen(5)
20+
21+
# accept a single request
22+
#while True:
23+
if True:
24+
#accept connections from outside
25+
print "calling accept"
26+
(clientsocket, address) = serversocket.accept()
27+
print "accept returned"
28+
#now do something with the clientsocket
29+
#in this case, we'll pretend this is a threaded server
30+
#ct = client_thread(clientsocket)
31+
#ct.run()
32+
print "got something:", clientsocket
33+
print "from address", address
34+
print clientsocket.recv(1024)
35+
36+
# now lets send something:
37+
clientsocket.send("This is some text")
38+
39+
## put this in your browser while this is running:
40+
## http://localhost:55557/a_file

0 commit comments

Comments
 (0)