-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
52 lines (38 loc) · 1.67 KB
/
server.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
from socket import *
import sys
def webServer(port):
serverPort = port
serverSocket = socket(AF_INET, SOCK_STREAM)#server socket - TCP byte stream
serverSocket.bind(('',serverPort))
serverSocket.listen(1) #server begins listening for incoming requests
print('Server being set up...')
while True:
print ('Server is ready !')
connectionSocket, addr = serverSocket.accept() #server waits on accept new socket returned on request as this is TCP
print('Request accepted from:', addr)
try:
message = connectionSocket.recv(1024).decode()
filename = message.split()[1]
f = open(filename[1:],'r')
outputdata = f.read()
headerLine = 'HTTP/1.1 200 OK\r\n'
connectionSocket.send(headerLine.encode())
connectionSocket.send('\r\n'.encode())
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i].encode())
connectionSocket.send('\r\n'.encode())
connectionSocket.close()
except IOError:
errHeaderLine = 'HTTP/1.1 404 Not Found\r\n' #r for carriage return and n for new line
connectionSocket.send(errHeaderLine.encode())
connectionSocket.send('\r\n'.encode())
f2 = open('404page.html','r')
outputdata_err = f2.read()
for i in range(0, len(outputdata_err)):
connectionSocket.send(outputdata_err[i].encode())
connectionSocket.send('\r\n'.encode())
connectionSocket.close()
serverSocket.close()
sys.exit()
if __name__ == '__main__':
webServer(6879)