Exemple #1
0
def setup_servers(config, servers, is_new):
    for server in servers.values():
        if is_new:
            conf = config._get('server.%s' % server.name)
        else:
            conf = config._get('%s' % server.name)
        if conf.is_active is False:
            continue
        context = MicroContext(
            conf.http_max_content_length if hasattr(conf, 'http_max_content_length') else None,
            conf.http_max_line_length if hasattr(conf, 'http_max_line_length') else 10000,
            conf.http_max_header_count if hasattr(conf, 'http_max_header_count') else 100,
        )
        mapper = RESTMapper(context)
        for route in server.routes:
            methods = {}
            for method, path in route.methods.items():
                methods[method] = _import(path)
            mapper.add(route.pattern, silent=route.silent, **methods)
        handler = _import(server.handler) if server.handler else MicroRESTHandler
        SERVER.add_server(
            conf.port,
            handler,
            mapper,
            conf.ssl.is_active,
            conf.ssl.certfile,
            conf.ssl.keyfile,
        )
        log.info('listening on %s port %d', server.name, conf.port)
Exemple #2
0
def load_server(filename, config=None):
    p = _load(filename)
    if config:
        p.config._load(file_util.normalize_path(config))
    sys.modules[__name__].config = p.config
    SERVER.close()
    setup_servers(p.config, p.servers, p.is_new)
    return p
Exemple #3
0
def run(sleep=100, max_iterations=100):
    while True:
        try:
            SERVER.service(delay=sleep/1000.0, max_iterations=max_iterations)
            TIMERS.service()
        except KeyboardInterrupt:
            log.info('Received shutdown command from keyboard')
            break
        except Exception:
            log.exception('exception encountered')
Exemple #4
0
 def done(self):
     if self.is_active:
         if hasattr(self, "route"):
             self.mapper.add(self.route.pattern, **self.route.method)
         try:
             SERVER.add_server(self.port, self.handler, self.mapper, ssl=self.ssl)
         except Exception:
             log.error("unable to add %s server on port %d", self.name, self.port)
             raise
         log.info("listening on %s port %d", self.name, self.port)
Exemple #5
0
def connect_parsed(
            callback,
            url,
            host,
            address,
            port,
            path,
            query,
            is_ssl,
            method,
            headers,
            body,
            is_json,
            is_form,
            timeout,
            wrapper,
            handler,
            evaluate,
            debug,
            trace,
            **kwargs
        ):
    c = ConnectContext(callback, url, method, path, query, host, headers, body,
                       is_json, is_form, timeout, wrapper, evaluate, debug,
                       trace, kwargs)
    return SERVER.add_connection((address, port), handler or ConnectHandler,
                                 context=c, ssl=is_ssl)
Exemple #6
0
def _connect(callback, url, host, address, port, path, is_ssl, method, body,
             headers, is_json, is_debug, timeout, wrapper, setup, handler,
             trace, kwargs):
    c = ConnectContext(callback, url, method, path, host, headers, body,
                       is_json, is_debug, timeout, wrapper, setup, kwargs,
                       trace)
    return SERVER.add_connection(
        (address, port),
        ConnectHandler if handler is None else handler,
        c,
        ssl=is_ssl)
Exemple #7
0
def request(url, callback, content='', headers=None, method='GET', timeout=5.0, close=True, ssl_args=None, compress=False, recv_len=None, event=None):
    ''' make an async http request

        When operating a tcpserver.SERVER, use this method to make async HTTP requests that eventually
        finish with a success or error call to a RequestCallback instance. The timeout feature will not
        work unless TIMERS.service is being called with appropriate frequency.

        Parameters:
            url     : resource url
            callback: a type of RequestCallback to which asynchronous results are reported
            content : http content to send
            headers : dictionary of http headers
            method  : http method
            timeout : max time, in seconds, allowed for network inactivity
            close   : close socket after request complete, boolean
            ssl_args: dict of kwargs for add_connection
            recv_len: read buffer size (default = BasicHandler.RECV_LEN)
            event   : dictionary of Handler event callback routines

                      on_init(handler)
                      on_open(handler)
                      on_close(handler)
                      on_handshake(handler, cert): bool, True means keep going
                      on_ready(handler)
                      on_http_headers(handler): (rc, result), (0, None) means keep going
                      on_http_send(handler, headers, content)
                      on_data(handler, data)

    '''
    url = _URLParser(url)
    context = _Context(host=url.host, resource=url.resource, callback=callback, content=content, headers=headers, method=method, timeout=timeout, close=close, compress=compress, recv_len=recv_len, event=event)
    if url.is_ssl:
        ssl = {'ssl': True}
        if ssl_args:
            ssl.update(ssl_args)
    else:
        ssl = {'ssl': False}
    SERVER.add_connection((url.address, url.port), _Handler, context, **ssl)
Exemple #8
0
def request(url, callback, content='', headers=None, method='GET', timeout=5.0, close=True, ssl_args=None, compress=False, recv_len=None, event=None):
    ''' make an async http request

        When operating a tcpserver.SERVER, use this method to make async HTTP requests that eventually
        finish with a success or error call to a RequestCallback instance. The timeout feature will not
        work unless TIMERS.service is being called with appropriate frequency.

        Parameters:
            url     : resource url
            callback: a type of RequestCallback to which asynchronous results are reported
            content : http content to send
            headers : dictionary of http headers
            method  : http method
            timeout : max time, in seconds, allowed for network inactivity
            close   : close socket after request complete, boolean
            ssl_args: dict of kwargs for add_connection
            recv_len: read buffer size (default = BasicHandler.RECV_LEN)
            event   : dictionary of Handler event callback routines

                      on_init(handler)
                      on_open(handler)
                      on_close(handler)
                      on_handshake(handler, cert): bool, True means keep going
                      on_ready(handler)
                      on_http_headers(handler): (rc, result), (0, None) means keep going
                      on_http_send(handler, headers, content)
                      on_data(handler, data)

    '''
    url = _URLParser(url)
    context = _Context(host=url.host, resource=url.resource, callback=callback, content=content, headers=headers, method=method, timeout=timeout, close=close, compress=compress, recv_len=recv_len, event=event)
    if url.is_ssl:
        ssl = {'ssl': True}
        if ssl_args:
            ssl.update(ssl_args)
    else:
        ssl = {'ssl': False}
    SERVER.add_connection((url.address, url.port), _Handler, context, **ssl)
Exemple #9
0
def run(command, delay=.01, loop=0):
    ''' helper function: loop through SERVER/TIMER until command.is_done is True '''

    while not command.is_done:
        SERVER.service(delay=delay, max_iterations=loop)
        TIMERS.service()
Exemple #10
0
def _connect(callback, url, host, address, port, path, is_ssl, method, body, headers, is_json, is_debug, timeout, wrapper, setup, handler, trace, kwargs):
    c = ConnectContext(callback, url, method, path, host, headers, body, is_json, is_debug, timeout, wrapper, setup, kwargs, trace)
    return SERVER.add_connection((address, port), ConnectHandler if handler is None else handler, c, ssl=is_ssl)
Exemple #11
0
def run(command, delay=.01, loop=0):
    ''' helper function: loop through SERVER/TIMER until command.is_done is True '''

    while not command.is_done:
        SERVER.service(delay=delay, max_iterations=loop)
        TIMERS.service()
Exemple #12
0
def re_start(p):
    SERVER.close()
    setup_servers(p.config, p.servers, p.is_new)
Exemple #13
0
def run(command, delay=.01, loop=0):
    """ service SERVER/TIMER until command.is_done is True """
    while not command.is_done:
        SERVER.service(delay=delay, max_iterations=loop)
        TIMERS.service()
Exemple #14
0
def run(command, delay=.01, loop=0):
    """ service SERVER/TIMER until command.is_done is True """
    while not command.is_done:
        SERVER.service(delay=delay, max_iterations=loop)
        TIMERS.service()