/
3600http.py
110 lines (84 loc) · 3.08 KB
/
3600http.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Team Laget
# Scott Surette
# Zane Whitney
# Rani Aljondi
import os
import socket
import sys
import thread
import datetime
import mimetypes
def buildpath(socket, basedir, httpid):
"""Builds a directory path to a file from an HTTP request."""
request = socket.recv(4096)
print str(httpid) + " REQUEST"
print "Request recieved: " + request
reqlist = request.split()
print "Request in list form: " + str(reqlist)
print "Length of reqlist: " + str(len(reqlist))
if (len(reqlist) > 1):
if (reqlist[0] == "GET"):
endpath = reqlist[1]
abspath = basedir + str(endpath)
print "Full path constructed: " + str(abspath)
print (endpath)
else:
socket.sendall("HTTP/1.0 400 Bad Request\r\n")
socket.close
abspath = None
else:
socket.sendall("HTTP/1.0 400 Bad Request\r\n")
socket.close
abspath = None
return abspath
def servicerequest(path, socket, httpid):
"""Services an HTTP request based on the provided file path"""
if (os.path.exists(path)):
print str(httpid) + " DELIVERED"
fd = open(path, "r")
file = fd.read()
okmsg = "HTTP/1.0 200 OK\r\n"
now = datetime.datetime.now()
datestr = now.strftime("%a %d %B %Y %H:%M:%S %z") + "\r\n"
print "Date and time: " + datestr
server = "Server: Var http-server 1.0\r\n"
connection = "Connection: close\r\n"
content_length = "Content length: " + str(sys.getsizeof(file)) + "\r\n"
content_type = "Content type: " + mimetypes.guess_type(path)[0] + "\r\n\r\n"
print content_type
print content_length
header = datestr + server + connection + content_length + content_type
socket.sendall(okmsg + header + file)
socket.close()
else:
print str(httpid) + " NOTFOUND"
socket.sendall("HTTP/1.0 404 Not Found\r\n")
socket.close()
print str(httpid) + " CLOSE"
return None
def main (directory, port=8080):
httpid = 0
# Building socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(("", port))
print "Waiting for requests on port " + str(port)
while 1:
print "going..."
server_socket.listen(5)
client_socket, address = server_socket.accept()
httpid += 1
print str(httpid) + " CONNECT"
print "Client socket: " + str(client_socket)
print "Address: " + str(address)
searchpath = buildpath(client_socket, directory, httpid)
if (searchpath == None):
print str(httpid) + " ERROR"
server_socket.close
else:
thread.start_new_thread(servicerequest, (searchpath, client_socket, httpid))
if __name__ == "__main__":
if (len(sys.argv) < 4):
main (sys.argv[1])
else:
main (sys.argv[3], int(sys.argv[2]))