19 import multiprocessing.pool
21 from wsgiref.simple_server
import WSGIServer, WSGIRequestHandler
28 app = bottle.Bottle(autojson=
False)
29 app.install(bottle.JSONPlugin(
31 config_key =
'traffexam.{}'.format
34 @app.route(
'/address', methpd=
'GET')
40 @app.route(
'/address', method=
'POST')
44 payload = bottle.request.json
45 payload.pop(
'idnr',
None)
48 vlan_tag = payload.pop(
'vlan', 0)
51 vlan = context.service.vlan.lookup(vlan_tag)
54 context.service.vlan.create(vlan)
62 except TypeError
as e:
63 return bottle.HTTPError(400, str(e))
66 context.service.address.create(entity)
68 return bottle.HTTPError(400, str(e))
70 bottle.response.status = 201
74 @app.route(
'/address/<idnr>', method=
'GET')
79 address = context.service.address.lookup(idnr)
81 return bottle.HTTPError(404,
'Address does not exist.')
85 @app.route(
'/address/<idnr>', method=
'DELETE')
90 context.service.address.delete(idnr)
92 return bottle.HTTPError(404,
'Address does not exist.')
93 bottle.response.status = 204
100 @app.route(
'/endpoint', method=
'GET')
106 @app.route(
'/endpoint', method=
'POST')
109 payload = bottle.request.json
110 payload.pop(
'idnr',
None)
115 klass = model.endpoint_klass_map[klass]
116 except (TypeError, KeyError):
117 return bottle.HTTPError(400,
'Invalid endpoint type {!r}'.
format(kind))
120 bind_address = payload.pop(
'bind_address',
None)
123 bind_address = context.service.address.lookup(bind_address)
126 if bind_address
is None:
127 raise bottle.HTTPError(400,
"bind_address field is missing")
128 entity = klass(bind_address, **payload)
131 payload,
'remote_address')[0]
135 bind_address=bind_address, **payload)
137 raise bottle.HTTPError(500,
'Unreachable point have been reached!')
139 return bottle.HTTPError(400,
'Invalid resource reference: {}'.
format(e))
140 except TypeError
as e:
141 return bottle.HTTPError(400, str(e))
143 context.service.endpoint.create(entity)
145 bottle.response.status = 201
149 @app.route(
'/endpoint/<idnr>', method=
'GET')
154 entity = context.service.endpoint.lookup(idnr)
156 return bottle.HTTPError(404,
'Endpoint does not exist.')
160 @app.route(
'/endpoint/<idnr>', method=
'DELETE')
165 context.service.endpoint.delete(idnr)
167 return bottle.HTTPError(404,
'Endpoint does not exist.')
168 bottle.response.status = 204
171 @app.route(
'/endpoint/<idnr>/report', method=
'GET')
176 output = context.service.endpoint.get_report(idnr)
178 report, error = output
180 report = error =
None 181 entity = context.service.endpoint.lookup(idnr)
183 return bottle.HTTPError(404,
'Endpoint does not exist')
188 'status': entity.proc.returncode}
197 if isinstance(payload, collections.Sequence):
199 return {key: payload}
204 app.run(server=MTServer, host=bind.address, port=bind.port, thread_count=5)
209 idnr = uuid.UUID(idnr)
211 raise bottle.HTTPError(400,
'Invalid resource id')
216 return bottle.request.app.config[
config_key(
'context')]
225 found.append(payload.pop(name))
230 raise bottle.HTTPError(400,
'Payload is lack of fields: "{}"'.
format(
231 '", "'.join(sorted(missing))))
244 '''WSGI-compliant HTTP server. Dispatches requests to a pool of threads.''' 246 def __init__(self, thread_count=None, *args, **kwargs):
247 '''If 'thread_count' == None, we'll use multiprocessing.cpu_count() threads.''' 248 WSGIServer.__init__(self, *args, **kwargs)
255 self.finish_request(request, client_address)
256 self.shutdown_request(request)
258 self.handle_error(request, client_address)
259 self.shutdown_request(request)
263 args=(request, client_address))
268 thread_count = self.options.pop(
'thread_count',
None)
270 self.host, self.port, handler, thread_count, **self.options)
271 server.serve_forever()
274 def make_server(host, port, app, thread_count=None,
275 handler_class=WSGIRequestHandler):
276 '''Create a new WSGI server listening on `host` and `port` for `app`'''
def format_response(payload, single, multiple)
def make_server(host, port, app, thread_count=None, handler_class=WSGIRequestHandler)
def extract_payload_fields(payload, fields)
def __init__(self, thread_count=None, args, kwargs)
def endpoint_delete(idnr)
def process_request_thread(self, request, client_address)
def endpoint_do_report(idnr)
def address_response(payload)
def process_request(self, request, client_address)
def endpoint_response(payload)