Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/scopes/scope_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/training.python_web.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1,141 changes: 1,141 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

22 changes: 17 additions & 5 deletions assignments/session01/echo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ def client(msg, log_buffer=sys.stderr):
server_address = ('localhost', 10000)
# TODO: Replace the following line with your code which will instantiate
# a TCP socket with IPv4 Addressing, call the socket you make 'sock'
sock = None
sock = socket.socket(
socket.AF_INET,
socket.SOCK_STREAM,
socket.IPPROTO_IP)

print >>log_buffer, 'connecting to {0} port {1}'.format(*server_address)
# TODO: connect your socket to the server here.

sock.connect(server_address)
# this try/finally block exists purely to allow us to close the socket
# when we are finished with it
try:
print >>log_buffer, 'sending "{0}"'.format(msg)
# TODO: send your message to the server here.

sock.sendall(msg)
# TODO: the server should be sending you back your message as a series
# of 16-byte chunks. You will want to log them as you receive
# each one. You will also need to check to make sure that
Expand All @@ -24,12 +28,20 @@ def client(msg, log_buffer=sys.stderr):
#
# Make sure that you log each chunk you receive. Use the print
# statement below to do it. (The tests expect this log format)
chunk = ''
print >>log_buffer, 'received "{0}"'.format(chunk)
amount_received = 0
amount_expected = len(msg)
while amount_received < amount_expected:
chunk = sock.recv(16)
amount_received += len(chunk)
print >>log_buffer, 'received "{0}"'.format(chunk)



finally:
# TODO: after you break out of the loop receiving echoed chunks from
# the server you will want to close your client socket.
print >>log_buffer, 'closing socket'
sock.close()


if __name__ == '__main__':
Expand Down
28 changes: 20 additions & 8 deletions assignments/session01/echo_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ def server(log_buffer=sys.stderr):
address = ('127.0.0.1', 10000)
# TODO: Replace the following line with your code which will instantiate
# a TCP socket with IPv4 Addressing, call the socket you make 'sock'
sock = None
sock = socket.socket(
socket.AF_INET,
socket.SOCK_STREAM,
socket.IPPROTO_TCP
)
# TODO: Set an option to allow the socket address to be reused immediately
# see the end of http://docs.python.org/2/library/socket.html

sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# log that we are building a server
print >>log_buffer, "making a server on {0}:{1}".format(*address)

# TODO: bind your new sock 'sock' to the address above and begin to listen
# for incoming connections

sock.bind(address)
sock.listen(1)
try:
# the outer loop controls the creation of new connection sockets. The
# server will handle each incoming connection one at a time.
Expand All @@ -28,9 +33,10 @@ def server(log_buffer=sys.stderr):
# the client so we can report it below. Replace the
# following line with your code. It is only here to prevent
# syntax errors
addr = ('bar', 'baz')
#addr = ('bar', 'baz')
conn, client_address = sock.accept()
try:
print >>log_buffer, 'connection - {0}:{1}'.format(*addr)
print >>log_buffer, 'connection - {0}:{1}'.format(*client_address)

# the inner loop will receive messages sent by the client in
# buffers. When a complete message has been received, the
Expand All @@ -41,27 +47,33 @@ def server(log_buffer=sys.stderr):
# following line with your code. It's only here as
# a placeholder to prevent an error in string
# formatting
data = ''
data = conn.recv(16)
print >>log_buffer, 'received "{0}"'.format(data)
# TODO: you will need to check here to see if any data was
# received. If so, send the data you got back to
# the client. If not, exit the inner loop and wait
# for a new connection from a client
if not data:
break
conn.send(data)



finally:
# TODO: When the inner loop exits, this 'finally' clause will
# be hit. Use that opportunity to close the socket you
# created above when a client connected. Replace the
# call to `pass` below, which is only there to prevent
# syntax problems
pass
conn.close()

except KeyboardInterrupt:
# TODO: Use the python KeyboardIntterupt exception as a signal to
# close the server socket and exit from the server function.
# Replace the call to `pass` below, which is only there to
# prevent syntax problems
pass
sock.close()
sys.exit()


if __name__ == '__main__':
Expand Down
17 changes: 14 additions & 3 deletions assignments/session02/http_server.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import socket
import sys
import mimetypes


def response_ok():
def response_ok(body, mimetyp):
"""returns a basic HTTP response"""
if not args:
body = "this is a pretty minimal response"
mimetype = "text/plain"
else:
body = args[0]
mimetype = args[1]
resp = []
resp.append("HTTP/1.1 200 OK")
resp.append("Content-Type: text/plain")
resp.append("Content-Type:" + mimetype)
resp.append("")
resp.append("this is a pretty minimal response")
resp.append(body)
return "\r\n".join(resp)


Expand All @@ -26,6 +33,10 @@ def parse_request(request):
if method != "GET":
raise NotImplementedError("We only accept GET")
print >>sys.stderr, 'request is okay'
return uri

def resolve_uri(uri):
mime_res = (mimetypes.guess_type(uri))[0]


def server():
Expand Down
74 changes: 74 additions & 0 deletions assignments/session02/http_server_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import socket
import sys


def response_ok():
"""returns a basic HTTP response"""
resp = []
resp.append("HTTP/1.1 200 OK")
resp.append("Content-Type: text/plain")
resp.append("")
resp.append("this is a pretty minimal response")
return "\r\n".join(resp)


def response_method_not_allowed():
"""returns a 405 Method Not Allowed response"""
resp = []
resp.append("HTTP/1.1 405 Method Not Allowed")
resp.append("")
return "\r\n".join(resp)


def parse_request(request):
first_line = request.split("\r\n", 1)[0]
method, uri, protocol = first_line.split()
if method != "GET":
raise NotImplementedError("We only accept GET")
print >>sys.stderr, 'request is okay'
return uri

def resolve_uri(uri):


def server():
address = ('127.0.0.1', 10000)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print >>sys.stderr, "making a server on %s:%s" % address
sock.bind(address)
sock.listen(1)

try:
while True:
print >>sys.stderr, 'waiting for a connection'
conn, addr = sock.accept() # blocking
try:
print >>sys.stderr, 'connection - %s:%s' % addr
request = ""
while True:
data = conn.recv(1024)
request += data
if len(data) < 1024 or not data:
break

try:
parse_request(request)
except NotImplementedError:
response = response_method_not_allowed()
else:
response = response_ok()

print >>sys.stderr, 'sending response'
conn.sendall(response)
finally:
conn.close()

except KeyboardInterrupt:
sock.close()
return


if __name__ == '__main__':
server()
sys.exit(0)
1 change: 1 addition & 0 deletions assignments/session02/tasks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ Optional Tasks:
If you choose to take on any of these optional tasks, try start by writing
tests in tests.py that demostrate what the task should accomplish. Then write
code that makes the tests pass.

Loading