Exemplo n.º 1
0
def start(server_settings):

    server_list = dict()

    for server_setting in server_settings:

        logger.info('Run server')

        server_id = server_setting['host'] + ':' + str(server_setting['port'])

        server = server_list.get(server_id)

        if server is None:
            server_list[server_id] = Server(
                server_setting
            )

        server_list[server_id].register(
            server_setting['server_name'],
            server_setting['app']
        )

    for server_name, server in server_list.items():
        server.run()
Exemplo n.º 2
0
    def run(self):
        if self.use_ssl:
            if hasattr(ssl, 'SSLContext'):
                if hasattr(ssl, 'PROTOCOL_TLSv1_2'):
                    # TODO: it will not work in below 2.7.9 and 3.2
                    ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
                else:
                    ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)

                if self.setting['use_http2']:
                    protocol_nego = None

                    if hasattr(ssl, 'HAS_NPN'):
                        if ssl.HAS_NPN:
                            protocol_nego = 'NPN'
                            ssl_context.set_npn_protocols(['h2'])
                    if hasattr(ssl, 'HAS_ALPN'):
                        if ssl.HAS_ALPN:
                            protocol_nego = 'ALPN'
                            ssl_context.set_alpn_protocols(['h2'])

                    if protocol_nego is None:
                        logger.info('Unsupport NPN or ALPN')

                ssl_context.load_cert_chain(
                    certfile=self.setting['crt_file'],
                    keyfile=self.setting['key_file']
                )

                self.listen_sock = ssl_context.wrap_socket(
                    socket.socket(socket.AF_INET, socket.SOCK_STREAM),
                    server_side=True
                )
            else:
                logger.info('Unsupport NPN or ALPN')
                self.listen_sock = ssl.wrap_socket(
                    socket.socket(socket.AF_INET, socket.SOCK_STREAM),
                    certfile=self.setting['crt_file'],
                    keyfile=self.setting['key_file'],
                    ssl_version=ssl.PROTOCOL_TLSv1,
                    server_side=True
                )
        else:
            self.listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        self.listen_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

        self.listen_sock.bind((self.host, self.port))

        self.listen_sock.listen(5)

        # create workers which process connections
        self.create_workers()

        while True:
            try:
                conn, addr = self.listen_sock.accept()
                if conn is None:
                    continue

                self.connection_queue.put((conn, addr))
            except ssl.SSLError:
                logger.debug('user access in tls connection without ssl cert')