Exemplo n.º 1
0
def main():
    handler = NewServiceHandler()
    processor = NewService.Processor(handler)
    tfactory = TTransport.TBufferedTransportFactory()
    pfactory = TJSONProtocolFactory()

    server = THttpServer(processor, ('0.0.0.0', 8888), pfactory)
    server.serve()
def main(cmdline):
    p = argparse.ArgumentParser()
    p.add_argument('-c', '--proxy', dest='proxy_bin', default='',
                   help='Run CORS proxy binary at PATH', metavar='PATH')
    p.add_argument('-p', '--port', dest='proxy_port', type=int, default=8000,
                   help='Run CORS proxy on PORT (default=%(default)d)',
                   metavar='PORT')
    p.add_argument('-v', '--verbose', action='count', dest='vb', default=0,
                   help='Increase verbosity of output to stderr')

    args = p.parse_args(cmdline)

    loglevel = [logging.WARN, logging.INFO, logging.DEBUG][min(args.vb, 2)]
    _log.setLevel(loglevel)

    use_proxy, proxy = bool(args.proxy_bin), None
    if use_proxy:
        proxy = run_cors_proxy(args.proxy_bin, args.proxy_port)

    handler = BasicTestHandler()
    processor = thrift_service.Processor(handler)
    #transport = TSocket.TServerSocket(port=PORT)
    tfactory = TTransport.TBufferedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()
    server = THttpServer(processor, ('localhost', PORT), pfactory)

    _log.info('Start server. port={:d}'.format(PORT))
    try:
        server.serve()
    except KeyboardInterrupt:
        _log.info('Interrupted')
    _log.info('Server stopped')

    # Stop the proxy, if it was running
    if proxy is not None:
        _log.info('Stopping CORS proxy. pid={}'.format(proxy.pid))
        for i in range(3):
            proxy.terminate()
            time.sleep(1)
            proxy.poll()
            if proxy.returncode is not None:
                break
        if proxy.returncode is None:
            proxy.kill()
            time.sleep(2)
            proxy.poll()
        if proxy.returncode is None:
            _log.error('Failed to stop CORS proxy. pid={}'.format(proxy.pid))
        else:
            _log.debug('CORS proxy stopped.')

    return 0