-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleSocketServer.py
More file actions
executable file
·114 lines (86 loc) · 3.28 KB
/
SimpleSocketServer.py
File metadata and controls
executable file
·114 lines (86 loc) · 3.28 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
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
111
112
113
114
#!/usr/local/bin/python
import os
import socket
class SimpleSocketServer:
def __init__(self, server_address, RequestHandlerClass):
self.server_address = server_address
self.RequestHandlerClass = RequestHandlerClass
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(self.server_address)
self.server_address = self.socket.getsockname()
self.server_activate()
def server_activate(self):
""" Listen to only 1 clients """
self.socket.listen(1)
def serve_forever(self):
while True:
client, address = self.socket.accept()
self.handle_request(client, address)
def handle_request(self, client, client_address):
self.RequestHandlerClass(client, client_address)
class TreeWalkingRequestHandler:
def __init__(self, client, client_address):
self.client = client
self.client_address = client_address
self.setup()
try:
self.handle()
finally:
self.finish()
def setup(self):
pass
def handle(self):
data = self.client.recv(1024)
if data:
data = data.split('\r\n')[0]
words = data.split()
if len(words) == 3:
command, path, version = words
elif len(words) == 2:
command, path = words
else:
self.handle_error_404()
return;
if command != 'GET':
self.handle_error_501()
else:
self.do_get(path)
self.client.close()
def do_get(self, path):
self.client.send("HTTP/1.1 200 OK\r\n");
base_path = "." + os.path.normpath(path);
if os.path.isdir(base_path):
self.client.send("\r\n\r\n");
self.client.send("<html>");
if path != "/":
self.client.send("<a href=\"/" + base_path + "/..\">..</a><br/>");
for file in os.listdir(base_path):
current_file = os.path.join(base_path, file);
if os.path.isdir(current_file):
self.client.send("<a href=\"/" + current_file + "\">" + file + "/</a><br/>");
else:
self.client.send("<a href=\"/" + current_file + "\">" + file + "</a><br/>");
self.client.send("</html>");
else:
name, ext = os.path.splitext(base_path);
if ext.lower() == ".jpg":
self.client.send("Content-type: image/jpeg");
file = open(base_path, 'rb');
else:
file = open(base_path);
self.client.send("\r\n\r\n");
self.client.send(file.read());
file.close();
def handle_error_404(self):
self.client.send("HTTP/1.1 404 Bad Request\r\n");
def handle_error_501(self):
self.client.send("HTTP/1.1 501 Not Implemented\r\n");
def finish(self):
pass
if __name__ == '__main__':
server_address = ('127.0.0.1', 8000)
echo = SimpleSocketServer(server_address, TreeWalkingRequestHandler)
sa = echo.socket.getsockname()
print "Serving on", sa[0], "port", sa[1], "..."
echo.serve_forever()