-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_webserver.py
executable file
·155 lines (118 loc) · 4.41 KB
/
admin_webserver.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
#!/bin/env dls-python
import os
import mimetypes
import sys
import time
sys.path.append(
"/dls_sw/work/tools/RHEL6-x86_64/ws4py/prefix/lib/python2.7/site-packages/ws4py-0.3.4-py2.7.egg")
from ws4py.websocket import WebSocket
from ws4py.manager import WebSocketManager
from ws4py.server.wsgiutils import WebSocketWSGIApplication
from wsgiref.simple_server import WSGIServer as _WSGIServer
from ws4py.compat import get_connection
# This disables hop-by-hop headers
from wsgiref import util
util._hoppish = {}.__contains__
# Number of bytes to send in each block
BLOCK_SIZE = 4096
class WSGIServer(_WSGIServer):
def shutdown_request(self, request):
"""
The base class would close our socket
if we didn't override it.
"""
pass
def server_close(self):
"""
Properly initiate closing handshakes on
all websockets when the WSGI server terminates.
"""
print "closing sockets"
_WSGIServer.server_close(self)
class EchoWebSocket(WebSocket):
commands = ["append_ssh", "replace_ssh", "list_zpkg", "install_zpkg"]
def received_message(self, message):
"""Do one of the commands"""
message = str(message)
self.send("<SOF>")
if message in self.commands:
self.send("Process started")
time.sleep(2)
getattr(self, message)()
else:
self.send("Unrecognized command %s" % message)
self.send("<EOF>")
def append_ssh(self):
self.send("SSH key file appended")
def replace_ssh(self):
self.send("SSH key file replaced")
def list_zpkg(self):
self.send("a.zpkg\nb.zpkg")
def install_zpkg(self):
self.send("packages installed")
class FileServer(object):
""" Serves static files from a directory.
"""
def __init__(self, path):
""" path is directory where static files are stored
"""
self.path = path
self.wsapp = WebSocketWSGIApplication(handler_cls=EchoWebSocket)
self.manager = WebSocketManager()
self.manager.start()
def __call__(self, environ, start_response):
""" WSGI entry point
"""
# Upgrade header means websockets...
upgrade_header = environ.get('HTTP_UPGRADE', '').lower()
if upgrade_header:
environ['ws4py.socket'] = get_connection(environ['wsgi.input'])
# This will make a websocket, hopefully!
ret = self.wsapp(environ, start_response)
if 'ws4py.websocket' in environ:
self.manager.add(environ.pop('ws4py.websocket'))
return ret
# Find path to file to server
path_info = environ["PATH_INFO"]
if not path_info or path_info == "/":
path_info = "/index.html"
file_path = os.path.join(self.path, path_info[1:])
# If file does not exist, return 404
if not os.path.exists(file_path):
return self._not_found(start_response)
# Guess mimetype of file based on file extension
mimetype = mimetypes.guess_type(file_path)[0]
# If we can't guess mimetype, return a 403 Forbidden
if mimetype is None:
return self._forbidden(start_response)
# Get size of file
size = os.path.getsize(file_path)
# Create headers and start response
headers = [
("Content-type", mimetype),
("Content-length", str(size)),
]
start_response("200 OK", headers)
# Send file
return self._send_file(file_path, size)
def _send_file(self, file_path, size):
""" A generator function which returns the blocks in a file, one at
a time.
"""
with open(file_path) as f:
block = f.read(BLOCK_SIZE)
while block:
yield block
block = f.read(BLOCK_SIZE)
def _not_found(self, start_response):
start_response("404 NOT FOUND", [("Content-type", "text/plain")])
return ["Not found", ]
def _forbidden(self, start_response):
start_response("403 FORBIDDEN", [("Content-type", "text/plain")])
return ["Forbidden", ]
if __name__ == '__main__':
from wsgiref.simple_server import make_server
application = FileServer(
os.path.join(os.path.dirname(__file__), "htstatic"))
server = make_server('0.0.0.0', 8081, application, server_class=WSGIServer)
server.serve_forever()