-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathserver.py
More file actions
80 lines (66 loc) · 2.31 KB
/
Copy pathserver.py
File metadata and controls
80 lines (66 loc) · 2.31 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
import threading
import time
import os
import posixpath
import urllib
from socket import socket
try:
from SimpleHTTPServer import SimpleHTTPRequestHandler
from BaseHTTPServer import HTTPServer
from urllib import unquote
except ImportError:
from http.server import SimpleHTTPRequestHandler
from http.server import HTTPServer
from urllib.parse import unquote
class SilentRequestHandler(SimpleHTTPRequestHandler): # pragma: no cover
silent = True
def translate_path(self, path):
"""Use the file's location instead of cwd"""
# abandon query parameters
self.silent = SilentRequestHandler.silent
path = path.split('?', 1)[0]
path = path.split('#', 1)[0]
path = posixpath.normpath(unquote(path))
words = path.split('/')
words = filter(None, words)
path = os.path.dirname(__file__)
for word in words:
drive, word = os.path.splitdrive(word)
head, word = os.path.split(word)
if word in (os.curdir, os.pardir):
continue
path = os.path.join(path, word)
return path
def log_message(self, *args):
"""Don't log anything, unless you say so"""
if not self.silent:
SimpleHTTPRequestHandler.log_message(self, *args)
def do_POST(self):
self.do_GET()
class TestServer(threading.Thread): # pragma: no cover
def __init__(self, daemonise=True, silent=True):
super(TestServer, self).__init__()
self.daemon = daemonise
self.silent = silent
self.http = None
# Try and get a free port number
sock = socket()
sock.bind(('', 0))
self.port = sock.getsockname()[1]
sock.close()
def run(self):
protocol = "HTTP/1.0"
server_address = ('', self.port)
SilentRequestHandler.protocol_version = protocol
SilentRequestHandler.silent = self.silent
# if not self.silent:
# print "Starting", protocol, "server on port", self.port
self.http = HTTPServer(server_address, SilentRequestHandler)
self.http.serve_forever()
def shutdown(self):
self.join()
if __name__ == '__main__': # pragma: no cover
server = TestServer(silent=False)
server.start()
for number in range(1, 20):
time.sleep(2)