|
6 | 6 |
|
7 | 7 | ''' |
8 | 8 |
|
| 9 | +import socket |
9 | 10 | import threading |
10 | 11 | import logging |
11 | | -import requests |
12 | | -import json |
13 | 12 | import base64 |
14 | 13 |
|
15 | 14 | from flask import Flask, request, jsonify |
@@ -50,7 +49,24 @@ def startAPIs(plugin_manager, model_controller, mapper_manager, hostname, port): |
50 | 49 | app = Flask('APISController') |
51 | 50 |
|
52 | 51 | _http_server = HTTPServer(WSGIContainer(app)) |
53 | | - _http_server.listen(port, address=hostname) |
| 52 | + while True: |
| 53 | + try: |
| 54 | + _http_server.listen(port, address=hostname) |
| 55 | + logger.getLogger().info( |
| 56 | + "REST API server configured on %s" % str( |
| 57 | + CONF.getApiRestfulConInfo())) |
| 58 | + break |
| 59 | + except socket.error as exception: |
| 60 | + if exception.errno == 98: |
| 61 | + # Port already in use |
| 62 | + # Let's try the next one |
| 63 | + port += 1 |
| 64 | + if port > 65535: |
| 65 | + raise Exception("No ports available!") |
| 66 | + CONF.setApiRestfulConInfoPort(port) |
| 67 | + CONF.saveConfig() |
| 68 | + else: |
| 69 | + raise exception |
54 | 70 |
|
55 | 71 | routes = [r for c in _rest_controllers for r in c.getRoutes()] |
56 | 72 |
|
@@ -325,48 +341,6 @@ def clearActivePlugins(self): |
325 | 341 | return self.ok("active plugins cleared") |
326 | 342 |
|
327 | 343 |
|
328 | | -class PluginControllerAPIClient(object): |
329 | | - def __init__(self, hostname, port): |
330 | | - self.hostname = hostname |
331 | | - self.port = port |
332 | | - self.url_input = "http://%s:%d/cmd/input" % (self.hostname, self.port) |
333 | | - self.url_output = "http://%s:%d/cmd/output" % (self.hostname, self.port) |
334 | | - self.url_active_plugins = "http://%s:%d/cmd/active-plugins" % (self.hostname, self.port) |
335 | | - self.headers = {'Content-type': 'application/json', 'Accept': 'application/json'} |
336 | | - |
337 | | - def send_cmd(self, cmd): |
338 | | - data = {"cmd": cmd} |
339 | | - new_cmd = cmd |
340 | | - output_file = None |
341 | | - try: |
342 | | - response = requests.post(self.url_input, |
343 | | - data=json.dumps(data), |
344 | | - headers=self.headers) |
345 | | - |
346 | | - if response.status_code == 200: |
347 | | - json_response = response.json() |
348 | | - if "cmd" in json_response.keys(): |
349 | | - if json_response.get("cmd") is not None: |
350 | | - new_cmd = json_response.get("cmd") |
351 | | - if "custom_output_file" in json_response.keys(): |
352 | | - output_file = json_response.get("custom_output_file") |
353 | | - except: |
354 | | - new_cmd = cmd |
355 | | - finally: |
356 | | - return new_cmd, output_file |
357 | | - |
358 | | - def send_output(self, cmd, output_file): |
359 | | - output_file = open(output_file) |
360 | | - output = base64.b64encode(output_file.read()) |
361 | | - data = {"cmd": cmd, "output": output} |
362 | | - response = requests.post(self.url_output, |
363 | | - data=json.dumps(data), |
364 | | - headers=self.headers) |
365 | | - if response.status_code != 200: |
366 | | - return False |
367 | | - return True |
368 | | - |
369 | | - |
370 | 344 | class Route(object): |
371 | 345 | """ Route class, abstracts information about: |
372 | 346 | path, handler and methods """ |
|
0 commit comments