def __init__(self, camera_id, name, camera_source=None, **kwargs): CameraBase.__init__(self, camera_id, name, type="frame", **kwargs) self._device_driver = hal.get_camera_driver(camera_source) self._device_driver.camera = self self._frame_format = self._device_driver.buffer_type protocol = "http://" if encryption.enabled(): protocol = "https://" self.ip_address = nethelper.get_ip_address() self.ip_port = nethelper.get_free_port() self.source = protocol + str(self.ip_address) + ":" + str( self.ip_port) + "/" + camera_id # + ".png" self.source = { "server": protocol + str(self.ip_address) + ":" + str(self.ip_port), "path": "/" + camera_id } self.current_frame = None self.current_frame_number = 0 from threading import Lock self.mutex = Lock() self.server = _HTTPFrameServer((self.ip_address, self.ip_port), _HTTPFrameHandler, self, self.mutex) if encryption.enabled(): cert_file, key_file = encryption.get_cert() if key_file and cert_file: import ssl self.server.socket = ssl.wrap_socket(self.server.socket, keyfile=key_file, certfile=cert_file, server_side=True) self.server_thread = threading.Thread(target=self.server.serve_forever, name="CameraStreamer server") self.server_thread.daemon = True self.server_thread.start() self.frame_thread = _CameraFrameThread(self, self.mutex)
def start(ip_address, http_port, ws_port): global SERVER, SERVER_THREAD SERVER = _HTTPServer(ip_address, http_port, ws_port, _HTTPRequestHandler) if encryption.enabled(): cert_file, key_file = encryption.get_cert() if key_file and cert_file: import ssl SERVER.socket = ssl.wrap_socket (SERVER.socket, keyfile=key_file, certfile=cert_file, server_side=True) SERVER_THREAD = threading.Thread(target=SERVER.serve_forever, name="webserver") SERVER_THREAD.daemon = True SERVER_THREAD.start()
def __init__(self, config): #coro = None self._started = False self._config = config self._spine = Spine() try: import asyncio except ImportError: ## Trollius >= 0.3 was renamed import trollius as asyncio from autobahn.asyncio.websocket import WebSocketServerFactory ssl_context = None if encryption.enabled(): self._spine.log.debug("socket using ssl") cert_file, key_file = encryption.get_cert() try: import ssl ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) ssl_context.load_cert_chain(cert_file, key_file) #self._spine.log.debug("socket ssl found") except: ssl_context = None self._spine.log.error("socket failed to use ssl") self._spine.log.debug( "start websocket on:{0}, port:{1}", self._config.network.ip, self._config.network.ws_port ) self.factory = WebSocketServerFactory() self.factory.protocol = _SpineProtocol self.loop = asyncio.get_event_loop() _SpineProtocol.loop = self.loop self.coro = self.loop.create_server( self.factory, self._config.network.ip, self._config.network.ws_port, ssl=ssl_context )
def start(self): self._server = _HTTPServer(self.plugin_config.ip_address, self.plugin_config.http_port, self.plugin_config.ws_port, _HTTPRequestHandler, self._http_docs) if encryption.enabled(): cert_file, key_file = encryption.get_cert() if key_file and cert_file: import ssl self._server.socket = ssl.wrap_socket(self._server.socket, keyfile=key_file, certfile=cert_file, server_side=True) self._use_encryption = True self._server_thread = threading.Thread( target=self._server.serve_forever, name="webserver") self._server_thread.daemon = True self._server_thread.start()
def do_GET(self): try: if self.server.do_authorize( ) and self.headers['Authorization'] == None: self.do_AUTHHEAD() #self.wfile.write('no auth header received') pass elif self.server.authorize(self.headers['Authorization']): if self.path.startswith("/cam"): path = self.path.split("/") cam_id = path[-1] spine = Spine() info = spine.send_query("getComponentInfo", cam_id) if info: conn = http.client.HTTPConnection( info["ui"]["source"]["server"], timeout=self.timeout) conn.request("GET", info["ui"]["source"]["path"]) res = conn.getresponse() self.send_response(res.status) for line in res.headers: self.send_header(line, res.headers[line]) self.end_headers() while not self.server.terminate: chunk = res.read(8192) if not chunk: break self.wfile.write(chunk) elif self.path.endswith("global.js"): self.send_response(200) self.send_header('Content-type', 'text/javascript') self.end_headers() if encryption.enabled(): response = bytes( "kerviSocketAddress='" + str(self.server.ip_address) + ":" + str(self.server.ws_port) + "';\n\rsocketProtocol='wss';", 'utf-8') else: response = bytes( "kerviSocketAddress='" + str(self.server.ip_address) + ":" + str(self.server.ws_port) + "';\n\rsocketProtocol='ws';", 'utf-8') self.wfile.write(response) elif self.path.endswith("kervitexts.js"): self.send_response(200) self.send_header('Content-type', 'text/javascript') self.end_headers() texts = json.dumps(self.server.texts) response = bytes("kerviUITexts=" + texts, 'utf-8') self.wfile.write(response) else: if self.path.startswith( "/dashboard/") or self.path.startswith("/connect"): path = self.server.docpath else: path = self.server.docpath + self.path if os.path.exists(path) and os.path.isdir(path): index_files = [ '/index.html', '/index.htm', ] for index_file in index_files: tmppath = path + index_file if os.path.exists(tmppath): path = tmppath break _, ext = os.path.splitext(path) ext = ext.lower() content_type = { '.css': 'text/css', '.gif': 'image/gif', '.htm': 'text/html', '.html': 'text/html', '.jpeg': 'image/jpeg', '.jpg': 'image/jpg', '.js': 'text/javascript', '.png': 'image/png', '.text': 'text/plain', '.txt': 'text/plain', } if ext in content_type: self.send_response(200) # OK self.send_header('Content-type', content_type[ext]) self.end_headers() with open(path, 'rb') as ifp: self.wfile.write(ifp.read()) else: self.send_response(200) # OK self.send_header('Content-type', 'text/plain') self.end_headers() with open(path, 'rb') as ifp: self.wfile.write(ifp.read()) else: self.do_AUTHHEAD() except IOError: self.send_error(404, 'file not found')