-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebServer.py
More file actions
43 lines (33 loc) · 1.26 KB
/
Copy pathWebServer.py
File metadata and controls
43 lines (33 loc) · 1.26 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
43
from socket import *
import sys
# Create a TCP server socket
# (AF_INET is used for IPv4 protocols)
# (SOCK_STREAM is used for TCP)
serverSocket = socket(AF_INET, SOCK_STREAM)
# Prepare a server socket
serverPort = 6789
serverSocket.bind(('192.168.1.20', serverPort)) #my IP address
serverSocket.listen(1)
while True:
# Establish the connection
print('Ready to serve...')
connectionSocket, addr = serverSocket.accept()
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\r\n".encode())
# Send the content of the requested file to the client
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i].encode())
connectionSocket.send("\r\n".encode())
connectionSocket.close()
except IOError:
# Send response message for file not found
connectionSocket.send("HTTP/1.1 404 Not Found\r\n\r\n".encode())
# Close client socket
connectionSocket.close()
serverSocket.close()
sys.exit()