-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrsu.py
65 lines (52 loc) · 1.94 KB
/
rsu.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from http.server import BaseHTTPRequestHandler, HTTPServer
from socketserver import ThreadingMixIn
import os, threading, socket
from queue import Queue
from datetime import datetime
class ThreadPoolMixIn(ThreadingMixIn):
numThreads = 100
allow_reuse_address = True
def serve_forever(self):
self.requests = Queue(self.numThreads)
for x in range(self.numThreads):
t = threading.Thread(target=self.process_request_thread)
t.setDaemon(1)
t.start()
while True:
self.handle_request()
self.server_close()
def process_request_thread(self):
while True:
ThreadingMixIn.process_request_thread(self, *self.requests.get())
def handle_request(self):
try:
request, client_address = self.get_request()
except socket.error:
return
if self.verify_request(request, client_address):
self.requests.put((request, client_address))
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
try:
size = int((self.path.decode()).split('/')[1])
self.send_response(200)
self.end_headers()
payload = os.urandom(size)
self.wfile.write(payload)
with open('rsu.log', 'a') as f:
f.write(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + ' GET ' + self.path + '\n')
except IOError:
self.send_error(404, 'File Not Found: %s' % self.path)
def do_POST(self):
try:
self._set_headers()
with open('rsu.log', 'a') as f:
f.write(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + ' POST ' + self.path + '\n')
except:
pass
class ThreadedHTTPServer(ThreadPoolMixIn, HTTPServer):
pass
if __name__ == '__main__':
server = ThreadedHTTPServer(('', 6666), Handler)
print('Starting Road Site Unit, use <Ctrl-C> to stop')
server.serve_forever()