Skip to content

Commit 6fd49b5

Browse files
Merge pull request avinashkranjan#208 from PrajjwalDatir/master
HTTP-Server-in-Python3-By-Prajjwal-Datir
2 parents c18e565 + 9bfddff commit 6fd49b5

File tree

10 files changed

+1227
-0
lines changed

10 files changed

+1227
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# HTTP Webserver From Scratch
2+
3+
## Implemented RFC of HTTP using SOCKET programming in python3
4+
5+
### Download this and store it into your home directory such that, This should be your path
6+
7+
/home/maniac/HTTP-Prajjwal/
8+
9+
### HOW TO RUN
10+
11+
main file cotaining the code => httpserver.py
12+
13+
#### To run main file with restart,start, quit functionality running in SEPARATE window
14+
15+
bash master.sh
16+
17+
#### To run main file with restart,start, quit functionality running in SAME window
18+
19+
python3 master.py port_number
20+
21+
#### To Test the program Run
22+
23+
first replace all getpath variable values with your local paths.
24+
25+
I didn't uploadded my test files to keep this project lightweight.
26+
27+
`python3 testing/autoTest.py 5562`
28+
29+
## HTTP-Web-Server
30+
31+
This is the project based on http protocol that has been used in day to day life. All dimensions are considered according to rfc 2616
32+
33+
It has following features:-
34+
35+
HTTP: GET, POST, PUT, HEAD, DELETE, Cookies, Headers, non-persistent connections, Multiple clients at the same time
36+
(with a sepearate program to test this), logging with levels of logging, handling file permissions; Server configuration
37+
config file with DocumentRoot, log file name, max simulateneous connections ; way to stop and restart the server;
38+
39+
It is developed using socket programming and very basic level of python.
40+
Project Developer has only focused on the quality of the server and not the features of the python language.
41+
42+
Those having beginner level of knowledge about socket Programming and Python language are requested to see and understand
43+
the code as the developer also has the beginner level of knowledge in both
44+
45+
### Here's PseudoCode I refered to while building HTTP server.
46+
47+
```python
48+
webserver {
49+
open a socket, bind to port 90
50+
listen
51+
t = accept();
52+
create a thread for t
53+
thread {
54+
s -> socket to exchange data for this particular connection
55+
loop {
56+
d = recvdata();
57+
interprete the data in d; // headers are to be deciphered
58+
string processing, tokenizing, making sense of data
59+
loop {
60+
l = next line();
61+
t = ':' separated list of tokens from l;
62+
swithch(t) {
63+
"Host": do something;
64+
carry some action on the server side;
65+
change some headers in the output (sent to browser);
66+
"User-Agent": do something;
67+
"GET":
68+
f = get the next part of "GET" // filename;
69+
open f;
70+
write HTTP headers into the socket;
71+
dump file f() into the socket;
72+
"if-modified-since":
73+
ht = get the 'time' given in header;
74+
ct = get current time;
75+
ft = get the time when file was modified;
76+
if(ft - ct < ht)
77+
just send headers in socket
78+
else
79+
send headers + file in socket;
80+
}
81+
}
82+
}
83+
}
84+
}
85+
86+
```
87+
88+
- Special Thanks to Abhijit Sir ( COEP CS Faculty)
89+
- RFC
90+
- Mozilla HTTP Docs
91+
- TutorialsPoint
92+
- Seniors and peers for helping me out throughout the project.
Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
# RUN CONFIG.PY TO CONFIGURE
2+
3+
import os
4+
5+
'''Max buffer size to accept from a client'''
6+
SIZE = 8192 # 8*1024 = 8MB
7+
8+
'''Gets the Current Working directory (.)'''
9+
ROOT = os.getcwd()
10+
11+
#dictionary to convert month to its decimal
12+
MONTH = { 'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6,
13+
'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12 }
14+
15+
'''
16+
fav icon which is displayed in title bar of the browser is requested by client
17+
so we define the path of favicon.ico here
18+
'''
19+
favicon = '/images/favicon.ico'
20+
FAVICON = ROOT + favicon # to get absolute path
21+
22+
'''
23+
only HTTP version no. 1.1 is supported for this server.
24+
It depends on the implementation of the dev but I am only supporting 1.1
25+
'''
26+
RUNNING_VERSION = '1.1'
27+
28+
'''
29+
Number of Thread Requests handled by the server at one time
30+
'''
31+
MAX_REQUEST = 20
32+
33+
'''
34+
Maximum URL length supported by the server at the time of establishing new connection
35+
'''
36+
MAX_URL = 250
37+
38+
'''
39+
log file path so that we can write into it
40+
'''
41+
LOG = ROOT + '/server.log'
42+
w = open(LOG, "a") # using a mode so we can only append and not overrite etc bad stuff
43+
w.close()
44+
45+
'''
46+
workfile.html has the response saved msg.
47+
It is not part of the Server program I am using it show client that it's response is saved
48+
'''
49+
WORKFILE = ROOT + '/workfile.html' # path
50+
51+
'''
52+
this creates a basic html workfile
53+
'''
54+
w = open(WORKFILE, "w")
55+
d = '''<html lang="en">
56+
<head>
57+
<meta charset="UTF-8">
58+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
59+
<title>Response Recieved</title>
60+
<body>
61+
<h1>Yeah We got the response!</h1>
62+
</br>
63+
<h1>Your Response was Saved Succesfully!</h1>
64+
</body>
65+
</html>'''
66+
w.write(d)
67+
w.close()
68+
69+
'''
70+
All data entered by the client is stored here for checking Purpose.
71+
'''
72+
CSVFILE = ROOT + '/output.csv'
73+
w = open(CSVFILE, "a") # only appending not writing
74+
w.close()
75+
76+
'''
77+
all the files which are deleted using DELETE are getting moved here.
78+
'''
79+
DELETE = ROOT + '/deleted'
80+
'''
81+
the /deleted folder mentioned above is created here.
82+
For the DELETE req purpose
83+
'''
84+
try:
85+
os.mkdir(DELETE)
86+
except:
87+
pass
88+
89+
'''
90+
username and password for approval of delete request method
91+
'''
92+
USERNAME = 'http' # delete can only be done after checking Auth
93+
PASSWORD = 'sudo' # Keep this secret folks
94+
95+
96+
'''
97+
cookie details
98+
'''
99+
COOKIE = 'Set-Cookie: id=' # id will be given in the program
100+
MAXAGE = '; max-age=3600' # 3600 sec is 60min
101+
102+
'''Following is the file formats supported by the server'''
103+
FORMAT = {
104+
".aac" : "audio/aac",
105+
".abw" : "application/x-abiword",
106+
".arc" : "application/x-freearc",
107+
".avi" : "video/x-msvideo",
108+
".azw" : "application/vnd.amazon.ebook",
109+
".bin" : "application/octet-stream",
110+
".bmp" : "image/bmp",
111+
".bz" : "application/x-bzip",
112+
".bz2" : "application/x-bzip2",
113+
".csh" : "application/x-csh",
114+
".css" : "text/css",
115+
".csv" : "text/csv",
116+
".doc" : "application/msword",
117+
".docx" : "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
118+
".eot" : "application/vnd.ms-fontobject",
119+
".epub" : "application/epub+zip",
120+
".gz" : "application/gzip",
121+
".gif" : "image/gif",
122+
".htm" : "text/html",
123+
".html" : "text/html",
124+
".ico" : "image/vnd.microsoft.icon",
125+
".ics" : "text/calendar",
126+
".jar" : "application/java-archive",
127+
".jpeg" : "image/jpeg",
128+
".jpg" : "image/jpeg",
129+
".js" : "text/javascript",
130+
".json" : "application/json",
131+
".jsonld": "application/ld+json",
132+
".mid" : "audio/midi",
133+
" .midi": "audio/midi",
134+
".mjs" : "text/javascript",
135+
".mp3" : "audio.mpeg",
136+
".mpeg" : "video/mpeg",
137+
".mpkg" : "application/vnd.apple.installer+xml",
138+
".odp" : "application/vnd.oasis.opendocument.presentation",
139+
".ods" : "application/vnd.oasis.opendocument.spreadsheet",
140+
".oga" : "audio/ogg",
141+
".ogv" : "video/ogg",
142+
".ogx" : "application/ogg",
143+
".otf" : "font/otf",
144+
".png" : "image/png",
145+
".pdf" : "application/pdf",
146+
".php" : "appliction/php",
147+
".ppt" : "application/vnd.ms-powerpoint",
148+
".pptx" : "application/vnd.openxmlformats-officedocument.presentationml.presentation",
149+
".rar" : "application/x-rar-compressed",
150+
".rtf" : "application/rtf",
151+
".sh" : "application/x-sh",
152+
".svg" : "image/svg+xml",
153+
".swf" : "application/x-shockwave-flash",
154+
".tar" : "application/x-tar",
155+
".tif" : "image/tiff",
156+
" .tiff": "image/tiff",
157+
".ts" : "video/mp2t",
158+
".ttf" : "font/ttf",
159+
".txt" : "text/html",
160+
".vsd" : "application/vnd.visio",
161+
".wav" : "audio/wav",
162+
".weba" : "audio/webm",
163+
".webm" : "video/webm",
164+
".webp" : ".webp",
165+
".woff" : "font/woff",
166+
".woff2": "font/woff2",
167+
".xhtml": "application/xhtml+xml",
168+
".xls" : "application/vnd.ms-excel",
169+
".xlsx" : "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
170+
".xml" : "application/xml",
171+
".xul" : "application/vnd.mozilla.xul+xml",
172+
".zip" : "application/zip",
173+
".3gp" : "video/3gpp",
174+
".3g2" : "video/3gpp2",
175+
".7z" : "application/x-7z-compressed",
176+
}
177+
178+
'''Following is the file formats supported by the server'''
179+
FORMAT2 = {
180+
"audio/aac" : ".aac" ,
181+
"application/x-abiword" : ".abw" ,
182+
"application/x-freearc" : ".arc" ,
183+
"video/x-msvideo" : ".avi" ,
184+
"application/vnd.amazon.ebook" : ".azw" ,
185+
"application/octet-stream" : ".bin" ,
186+
"image/bmp" : ".bmp" ,
187+
"application/x-bzip" : ".bz" ,
188+
"application/x-bzip2" : ".bz2" ,
189+
"application/x-csh" : ".csh" ,
190+
"text/css" : ".css" ,
191+
"text/csv" : ".csv" ,
192+
"application/msword" : ".doc" ,
193+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document":".docx" ,
194+
"application/vnd.ms-fontobject" : ".eot" ,
195+
"application/epub+zip" : ".epub" ,
196+
"application/gzip" : ".gz" ,
197+
"image/gif" : ".gif" ,
198+
"text/html" : ".html" ,
199+
"image/vnd.microsoft.icon" : ".ico" ,
200+
"text/calendar" : ".ics" ,
201+
"application/java-archive" : ".jar" ,
202+
"image/jpeg" : ".jpeg" ,
203+
"text/javascript" : ".js" ,
204+
"application/json" : ".json" ,
205+
"application/ld+json" : ".jsonld" ,
206+
"audio/midi" : ".mid" ,
207+
"audio.mpeg" : ".mp3" ,
208+
"video/mpeg" : ".mpeg" ,
209+
"application/vnd.apple.installer+xml":".mpkg" ,
210+
"application/vnd.oasis.opendocument.presentation" : ".odp" ,
211+
"application/vnd.oasis.opendocument.spreadsheet" : ".ods" ,
212+
"audio/ogg" : ".oga" ,
213+
"video/ogg" : ".ogv" ,
214+
"application/ogg" : ".ogx" ,
215+
"font/otf" : ".otf" ,
216+
"image/png" : ".png" ,
217+
"application/pdf" : ".pdf" ,
218+
"appliction/php" : ".php" ,
219+
"application/vnd.ms-powerpoint" : ".ppt" ,
220+
"application/vnd.openxmlformats-officedocument.presentationml.presentation":".pptx" ,
221+
"application/x-rar-compressed" : ".rar" ,
222+
"application/rtf" : ".rtf" ,
223+
"application/x-sh" : ".sh" ,
224+
"image/svg+xml" : ".svg" ,
225+
"application/x-shockwave-flash" : ".swf" ,
226+
"application/x-tar" : ".tar" ,
227+
"image/tiff" : ".tif" ,
228+
"video/mp2t" : ".ts" ,
229+
"font/ttf" : ".ttf" ,
230+
"application/vnd.visio" : ".vsd" ,
231+
"audio/wav" : ".wav" ,
232+
"audio/webm" : ".weba" ,
233+
"video/webm" : ".webm" ,
234+
".webp" : ".webp" ,
235+
"font/woff" : ".woff" ,
236+
"font/woff2" : ".woff2" ,
237+
"application/xhtml+xml" : ".xhtml" ,
238+
"application/vnd.ms-excel" : ".xls" ,
239+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":".xlsx" ,
240+
"application/xml" : ".xml" ,
241+
"application/vnd.mozilla.xul+xml":".xul" ,
242+
"application/zip" : ".zip" ,
243+
"video/3gpp" : ".3gp" ,
244+
"video/3gpp2" : ".3g2" ,
245+
"application/x-7z-compressed" : ".7z" ,
246+
}
247+
248+
'''Response status codes'''
249+
status_codes = {
250+
200 : "Ok",
251+
201 : "Created",
252+
202 : "Accepted",
253+
204 : "No Content",
254+
304 : "Not Modified",
255+
400 : "Bad Request",
256+
401 : "Unauthorized",
257+
403 : "Forbidden",
258+
404 : "Not Found",
259+
411 : "Length required",
260+
412 : "Precondition Failed",
261+
414 : "URI too long",
262+
415 : "Unsupported media Type",
263+
500 : "Internal Server Error",
264+
501 : "Not Implemented",
265+
503 : "Server Unavailable",
266+
505 : "HTTP version not supported",
267+
}
268+
269+
'''Methods supported by the server'''
270+
methods = ["GET", "POST", "HEAD", "PUT", "DELETE", "TRACE", "OPTIONS"]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<html>
2+
<head>
3+
<title>Hi there</title>
4+
</head>
5+
<body>
6+
<h1>This indidcates that your server is running! Congratulations!!</h1>
7+
<form method="POST">
8+
<label for="fname">First name:</label><br>
9+
<input type="text" id="fname" name="fname"><br>
10+
<label for="lname">Last name:</label><br>
11+
<input type="text" id="lname" name="lname">
12+
<input type="submit" value="Submit">
13+
</form>
14+
</body>
15+
</html>

0 commit comments

Comments
 (0)