-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_server.py
164 lines (123 loc) · 4.8 KB
/
test_server.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/python
# -*- coding: utf-8 -*-
import cgi
import argparse
from random import randint
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from multiprocessing import Value
parser = argparse.ArgumentParser()
parser.add_argument("--port", type=int, default=65123)
parser.add_argument("--stream-url", dest="stream_url", help="Stream url", required = True)
parser.add_argument("--test-url", dest="test_url", help="Testing stream url", required = True)
options = parser.parse_args()
#This class will handles any incoming request from
#the browser
class Handler(BaseHTTPRequestHandler):
started = Value('b', False)
started_test = Value('b', False)
index = """
<html>
<body>
<video controls>
<source src="STREAM_URL" type='video/webm'/>
<source src="TEST_URL" type='video/webm'/>
</video>
<img src="/slide"></img>
<br/>
STREAM_START_STOP
TEST_START_STOP
</body>
</html>"""
#Handler for the GET requests
def do_GET(self):
index = self.index.replace("STREAM_URL", options.stream_url + "?nocache=%d" %randint(1,9999))
index = index.replace("TEST_URL", options.test_url + "?nocache=%d" %randint(1,9999))
if self.path=="/":
mimetype='text/html'
self.send_response(200)
self.send_header('Content-type', mimetype)
self.send_header("Cache-Control", "no-cache")
self.end_headers()
replacement = ("""<a href="/stop">Stop stream</a>"""
if self.started.value else
"""<a href="/start">Start stream</a>""")
index = index.replace("STREAM_START_STOP", replacement)
replacement = ("""<a href="/stop_test">Stop test stream</a>"""
if self.started_test.value else
"""<a href="/start_test">Start test stream</a>""")
self.wfile.write(index.replace("TEST_START_STOP", replacement))
return
elif self.path=="/start":
mimetype='text/html'
self.started.value = True
self.send_response(301)
self.send_header('Location','/')
elif self.path=="/stop":
mimetype='text/html'
self.started.value = False
self.send_response(301)
self.send_header('Location','/')
elif self.path=="/start_test":
mimetype='text/html'
self.started_test.value = True
self.send_response(301)
self.send_header('Location','/')
elif self.path=="/stop_test":
mimetype='text/html'
self.started_test.value = False
self.send_response(301)
self.send_header('Location','/')
elif self.path=="/started":
mimetype='text/html'
self.send_response(200)
self.send_header('Content-type', mimetype)
self.send_header("Cache-Control", "no-cache")
self.end_headers()
self.wfile.write("True" if self.started.value else "False")
return
elif self.path=="/started_test":
mimetype='text/html'
self.send_response(200)
self.send_header('Content-type', mimetype)
self.send_header("Cache-Control", "no-cache")
self.end_headers()
self.wfile.write("True" if self.started_test.value else "False")
return
elif self.path=="/slide":
try: fd = open('.slide', "rb")
except IOError:
self.send_error(404,'Slide Not Found!')
return
mimetype = 'image/jpg'
self.send_response(200)
self.send_header('Content-type', mimetype)
self.send_header("Cache-Control", "no-cache")
self.end_headers()
self.wfile.write(fd.read())
fd.close()
return
self.send_header("Cache-Control", "no-cache")
self.end_headers()
def do_POST(self):
form = cgi.FieldStorage(fp=self.rfile, headers=self.headers,
environ = {'REQUEST_METHOD':'POST',
'CONTENT_TYPE': self.headers['Content-Type'],
}
)
upfile = form['slide']
fd = open(".slide", "wb")
fd.write(upfile.file.read())
fd.close()
self.send_response(301)
self.send_header('Location','/')
self.end_headers()
try:
#Create a web server and define the handler to manage the
#incoming request
server = HTTPServer(('', options.port), Handler)
print 'Started httpserver on port ' , options.port
#Wait forever for incoming htto requests
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down the web server'
server.socket.close()