diff options
Diffstat (limited to 'admin/Sources/Clients/ogagent/src/test_rest_server.py')
-rw-r--r-- | admin/Sources/Clients/ogagent/src/test_rest_server.py | 210 |
1 files changed, 0 insertions, 210 deletions
diff --git a/admin/Sources/Clients/ogagent/src/test_rest_server.py b/admin/Sources/Clients/ogagent/src/test_rest_server.py deleted file mode 100644 index ad1aede6..00000000 --- a/admin/Sources/Clients/ogagent/src/test_rest_server.py +++ /dev/null @@ -1,210 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright (c) 2015 Virtual Cable S.L. -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without modification, -# are permitted provided that the following conditions are met: -# -# * Redistributions of source code must retain the above copyright notice, -# this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above copyright notice, -# this list of conditions and the following disclaimer in the documentation -# and/or other materials provided with the distribution. -# * Neither the name of Virtual Cable S.L. nor the names of its contributors -# may be used to endorse or promote products derived from this software -# without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -''' -@author: Adolfo Gómez, dkmaster at dkmon dot com -''' -# pylint: disable=unused-wildcard-import,wildcard-import -from __future__ import unicode_literals, print_function - -# Pydev can't parse "six.moves.xxxx" because it is loaded lazy -from six.moves.socketserver import ThreadingMixIn # @UnresolvedImport -from six.moves.BaseHTTPServer import BaseHTTPRequestHandler # @UnresolvedImport -from six.moves.BaseHTTPServer import HTTPServer # @UnresolvedImport -from six.moves.urllib.parse import unquote # @UnresolvedImport - -import json -import threading -import ssl - -import logging -from tempfile import gettempdir -from os.path import exists, join - -logger = logging.getLogger(__name__) - - -CERTFILE = 'OGTestServer.pem' - - -def createSelfSignedCert(force=False): - - certFile = join(gettempdir(), CERTFILE) - - if exists(certFile) and not force: - return certFile - - certData = '''-----BEGIN PRIVATE KEY----- -MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCb50K3mIznNklz -yVAD7xSQOSJQ6+NPXj7U9/4zLZ+TvmbQ7RqUUsxbfxHbeRnoYTWV2nKk4+tHqmvz -ujLSS/loFhTSMqtrLn7rowSYJoQhKOUkAiQlWkqCfItWgL5pJopDpNHFul9Rn3ds -PMWQTiGeUNR4Y3RnBhr1Q1BsqAzf4m6zFUmgLPPmVLdF4uJ3Tuz8TSy2gWLs5aSr -5do4WamwUfYjRSVMJECmwjUM4rQ8SQgg0sHBeBuDUGNBvBQFac1G7qUcMReeu8Zr -DUtMsXma/l4rA8NB5CRmTrQbTBF4l+jb2BDFebDqDUK1Oqs9X35yOQfDOAFYHiix -PX0IsXOZAgMBAAECggEBAJi3000RrIUZUp6Ph0gzPMuCjDEEwWiQA7CPNX1gpb8O -dp0WhkDhUroWIaICYPSXtOwUTtVjRqivMoxPy1Thg3EIoGC/rdeSdlXRHMEGicwJ -yVyalFnatr5Xzg5wkxVh4XMd0zeDt7e3JD7s0QLo5lm1CEzd77qz6lhzFic5/1KX -bzdULtTlq60dazg2hEbcS4OmM1UMCtRVDAsOIUIZPL0M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7APoq0P/M0DBhQpiJKjrD6ydUevC+Y/ -43ZOJPhMlNw0o6TdQxOkX6FDwQanLLs7sfvJvqtVzYn3nuRFKT3dvl7Zg44QMw2M -ay42q59fAcpB4LaDx/i7gOYSS5eca3lYW7j7YSr/+ozXK2KlgUkuCUHN95lOq+dF -trmV9mjzM4CNPZqKSE7kpHRywgrXGPCO000NvEGSYf82AtgRSFKiU8NWLQSEPdcB -k//2dsQZw2cRZ8DrC2B6Tb3M+3+CA6wVyqfqZh1SZva3LfGvq/C+u+ItguzPqNpI -xtvM ------END CERTIFICATE-----''' - with open(certFile, "wt") as f: - f.write(certData) - - return certFile - -class HTTPServerHandler(BaseHTTPRequestHandler): - service = None - protocol_version = 'HTTP/1.0' - server_version = 'OpenGnsys Test REST Server' - sys_version = '' - - def sendJsonError(self, code, message): - self.send_response(code) - self.send_header('Content-type', 'application/json') - self.end_headers() - self.wfile.write(json.dumps({'error': message})) - return - - def sendJsonResponse(self, data): - self.send_response(200) - data = json.dumps(data) - self.send_header('Content-type', 'application/json') - self.send_header('Content-Length', len(data)) - self.end_headers() - # Send the html message - self.wfile.write(data) - - - # parseURL - def parseUrl(self): - # Very simple path & params splitter - path = self.path.split('?')[0][1:].split('/') - - try: - params = dict((v[0], unquote(v[1])) for v in (v.split('=') for v in self.path.split('?')[1].split('&'))) - except Exception: - params = {} - - return (path, params) - - - def do_GET(self): - path, params = self.parseUrl() - - self.sendJsonResponse({'path': path, 'params': params}) - - def do_POST(self): - path, getParams = self.parseUrl() - - # Now post parameters, that are in JSON format - self.sendJsonResponse({'path': path, 'params': getParams}) - - def log_error(self, fmt, *args): - logger.error('HTTP ' + fmt % args) - - def log_message(self, fmt, *args): - logger.info('HTTP ' + fmt % args) - - -class HTTPThreadingServer(ThreadingMixIn, HTTPServer): - pass - -class HTTPServerThread(threading.Thread): - def __init__(self, address, service): - super(self.__class__, self).__init__() - - HTTPServerHandler.service = service - - self.certFile = createSelfSignedCert() - self.server = HTTPThreadingServer(address, HTTPServerHandler) - self.server.socket = ssl.wrap_socket(self.server.socket, certfile=self.certFile, server_side=True) - - logger.info('Initialized HTTPS Server thread on {}'.format(address)) - - def getServerUrl(self): - return 'https://{}:{}/{}'.format(self.server.server_address[0], self.server.server_address[1], HTTPServerHandler.uuid) - - def stop(self): - self.server.shutdown() - - def run(self): - self.server.serve_forever() - - - -if __name__ == '__main__': - logging.basicConfig( - filename='/tmp/restserver.log', - filemode='w', - format='%(levelname)s %(asctime)s %(message)s', - level=logging.DEBUG - ) - - thr = HTTPServerThread(('0.0.0.0', 9999), None) - print('Server started: {}'.format(thr)) - thr.run() -
\ No newline at end of file |