|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import socket |
| 4 | +import httplib |
| 5 | +import os |
| 6 | + |
| 7 | + |
| 8 | +class EchoServer(object): |
| 9 | + """Interacts with Client, recieving and sending data.""" |
| 10 | + |
| 11 | + def __init__(self, host='', port=50001, backlog=5): |
| 12 | + """Constructs the socket connection, configures the server.""" |
| 13 | + |
| 14 | + # Create a TCP/IP socket |
| 15 | + self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 16 | + # Bind the socket to the port |
| 17 | + self.server.bind((host, port)) |
| 18 | + print 'Server started on port: ', port |
| 19 | + # Listen for incoming connections |
| 20 | + self.server.listen(backlog) |
| 21 | + print("Server listening\n") |
| 22 | + ## create the socket |
| 23 | + # set an option to tell the OS to re-use the socket |
| 24 | + self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 25 | + |
| 26 | + self.size = 1024 # number of bytes to receive at once |
| 27 | + |
| 28 | + def ProcessRequest(self, size): |
| 29 | + # Wait for a connection |
| 30 | + self.con, self.cli = self.server.accept() |
| 31 | + print 'New connection from ', self.cli |
| 32 | + self.data = self.con.recv(size) |
| 33 | + return self.data |
| 34 | + |
| 35 | + def ProcessResponse(self, data): |
| 36 | + # Send data to client |
| 37 | + self.con.send(data) |
| 38 | + |
| 39 | + def CloseSocket(self): |
| 40 | + self.con.close() |
| 41 | + |
| 42 | + def ParseRequest(self, request): |
| 43 | + i = request.split() |
| 44 | + print i |
| 45 | + (verb, path, http) = i[0], i[1], i[2] |
| 46 | + return (verb, path, http) |
| 47 | + |
| 48 | + def Payload(self, header, body): |
| 49 | + body = """ |
| 50 | + <html> <body> \ |
| 51 | + <h1>""" + header + """</h1> \ |
| 52 | + <p>""" + body + """</p> \ |
| 53 | + </body> </html> """ |
| 54 | + return body |
| 55 | + |
| 56 | + def ClientErrorResponse(self): |
| 57 | + bad = "HTTP/1.1 400 Bad Request" |
| 58 | + body = self.Payload(bad) |
| 59 | + empty = "" |
| 60 | + resp = "\r\n".join([bad, empty, body]) |
| 61 | + return resp |
| 62 | + |
| 63 | + def NotFoundResponse(self): |
| 64 | + notfound = "HTTP/1.1 404 Not Found" |
| 65 | + body = self.Payload(notfound) |
| 66 | + empty = "" |
| 67 | + resp = "\r\n".join([notfound, empty, body]) |
| 68 | + return resp |
| 69 | + |
| 70 | + def Response(self, data=''): |
| 71 | + ok = "HTTP/1.1 200 OK" |
| 72 | + body = self.Payload(ok, data) |
| 73 | + empty = "" |
| 74 | + resp = "\r\n".join([ok, empty, body]) |
| 75 | + return resp |
| 76 | + |
| 77 | + def ResolveUri(self, path): |
| 78 | + if os.path.realpath.exists(path): |
| 79 | + content = os.listdir(path) |
| 80 | + return content |
| 81 | + |
| 82 | +def main(): |
| 83 | + echo_server = EchoServer() |
| 84 | + while True: |
| 85 | + data = echo_server.ProcessRequest(echo_server.size) |
| 86 | + try: |
| 87 | + if data: # if the connection was closed there would be no data |
| 88 | + (verb, path, http) = echo_server.ParseRequest(data) |
| 89 | + resolve_uri = echo_server.ResolveUri(path) |
| 90 | + if verb == 'GET' and http == "HTTP/1.1": |
| 91 | + print echo_server.ResolveUri(path) |
| 92 | + echo_server.ProcessResponse(echo_server.Response()) |
| 93 | +# return echo_server.NotFoundResponse() |
| 94 | +# raise ValueError('%s was not found' % (path)) |
| 95 | + else: |
| 96 | + echo_server.ProcessResponse(echo_server.ClientErrorResponse()) |
| 97 | + raise ValueError('%s %s is a bad request' % (verb, http)) |
| 98 | + finally: |
| 99 | + print "Closing Socket" |
| 100 | + echo_server.CloseSocket() |
| 101 | + |
| 102 | +main() |
0 commit comments