-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhenlinWebServerv1.py
More file actions
42 lines (42 loc) · 1.73 KB
/
henlinWebServerv1.py
File metadata and controls
42 lines (42 loc) · 1.73 KB
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
from socket import *
import sys
import threading
from multiprocessing import connection
#Prepare a server socket
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('192.168.2.55', 11000))
serverSocket.listen(7)
# this array will hold threads
threads = []
print ('Ready to receive...')
#function for threading
def TCPThread(connectionSocket,addr):
try:
message = connectionSocket.recv(1024).decode()
filename = message.split()[1]
f = open(filename[1:])
outputdata = f.read()
#Send one HTTP header line into socket
connectionSocket.send('HTTP/1.1 200 OK\r\n'.encode('utf-8'))
connectionSocket.send("Content-Type: text/html \r\n\r\n".encode('utf-8'))
connectionSocket.send(outputdata.encode('utf-8'))
connectionSocket.close()
except IOError:
#Send response message for file not found
connectionSocket.send('HTTP/1.1 404 Not Found'.encode('utf-8'))
#Close client socket
connectionSocket.close()
return
try:
while True:
connectionSocket, addr = serverSocket.accept()
#creating the thread
thread_ = threading.Thread(target=TCPThread, args=(connectionSocket,addr))
#adding a thread to array
threads.append(thread_)
#start
thread_.start()
print(str(len(threads)) + " Thread(s)")
finally:
serverSocket.close()
sys.exit()#Terminate the program after sending the corresponding data