Skip to content

Commit 9ad73cf

Browse files
committed
2 parents 0fc59a2 + b7ee810 commit 9ad73cf

18 files changed

Lines changed: 1615 additions & 11 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import socket
2+
import sys
3+
4+
# Create a TCP/IP socket
5+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
6+
7+
# Connect the socket to the port where the server is listening
8+
server_address = ('localhost', 10000)
9+
print >>sys.stderr, 'connecting to %s port %s' % server_address
10+
sock.connect(server_address)
11+
12+
try:
13+
14+
# Send data
15+
message = 'This is the message. It will be repeated.'
16+
print >>sys.stderr, 'sending "%s"' % message
17+
sock.sendall(message)
18+
19+
# Look for the response
20+
amount_received = 0
21+
amount_expected = len(message)
22+
23+
while amount_received < amount_expected:
24+
data = sock.recv(16)
25+
amount_received += len(data)
26+
print >>sys.stderr, 'received "%s"' % data
27+
28+
finally:
29+
print >>sys.stderr, 'closing socket'
30+
sock.close()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import socket
2+
import sys
3+
4+
# Create a TCP/IP socket
5+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
6+
7+
# Bind the socket to the port
8+
server_address = ('localhost', 10000)
9+
print >>sys.stderr, 'starting up on %s port %s' % server_address
10+
sock.bind(server_address)
11+
12+
# Listen for incoming connections
13+
sock.listen(1)
14+
15+
try:
16+
17+
while True:
18+
# Wait for a connection
19+
print >>sys.stderr, 'waiting for a connection'
20+
connection, client_address = sock.accept()
21+
22+
try:
23+
print >>sys.stderr, 'connection from', client_address
24+
25+
# Receive the data in small chunks and retransmit it
26+
while True:
27+
data = connection.recv(16)
28+
print >>sys.stderr, 'received "%s"' % data
29+
if data:
30+
print >>sys.stderr, 'sending data back to the client'
31+
connection.sendall(data)
32+
else:
33+
print >>sys.stderr, 'no more data from', client_address
34+
break
35+
36+
finally:
37+
# Clean up the connection
38+
connection.close()
39+
40+
except KeyboardInterrupt:
41+
sock.close()
42+
sys.exit(0)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Complete your HTTP Web Server. Accomplish as many of the following goals as
2+
you are able:
3+
4+
* If you were unable to complete the first five steps in class, circle back
5+
and finish them
6+
7+
* Complete the 'Bonus point' parts from the first five steps, if you haven't
8+
already done so
9+
10+
* Format your directory listing as HTML
11+
12+
* In the HTML directory listing, make the files clickable links
13+
14+
* Add a new, dynamic endpoint. If the URI /time-page is requested, return an
15+
HTML page with the current time displayed.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
## create the socket
11+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
12+
# set an option to tell the OS to re-use the socket
13+
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
14+
15+
# the bind makes it a server
16+
s.bind( (host,port) )
17+
s.listen(backlog)
18+
19+
while True: # keep looking for new connections forever
20+
client, address = s.accept() # look for a connection
21+
data = client.recv(size)
22+
if data: # if the connection was closed there would be no data
23+
print "received: %s, sending it back"%data
24+
client.send(data)
25+
client.close()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<html>
2+
<body>
3+
<h1>This is a header</h1>
4+
<p>
5+
and this is some regular text
6+
</p>
7+
<p>
8+
and some more
9+
</p>
10+
</body>
11+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<body>
4+
5+
<h1>My First Heading</h1>
6+
7+
<p>My first paragraph.</p>
8+
9+
</body>
10+
</html>
11+
14.8 KB
Loading
143 KB
Loading
8.55 KB
Loading
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
make_time.py
5+
6+
simple script that returns and HTML page with the current time
7+
"""
8+
9+
import datetime
10+
11+
time_str = datetime.datetime.now().isoformat()
12+
13+
html = """
14+
<http>
15+
<body>
16+
<h2> The time is: </h2>
17+
<p> %s <p>
18+
</body>
19+
</http>
20+
"""% time_str
21+
22+
print html
23+
24+
25+

0 commit comments

Comments
 (0)