Exemplo n.º 1
0
def server():
    address = ('localhost', 0)
    server = http.server.HTTPServer(address, HTTPRequestHandler)
    thread = threading.Thread(target=server.serve_forever, daemon=True)
    thread.start()
    yield server
    server.shutdown()
Exemplo n.º 2
0
def notification_server(server_info, checker=None):
    host = None
    port = []
    if server_info is not None:
        host, *port = server_info.split(":")
        if not host or not (port and port[0]):
            raise ValueError("Notification server host:port configuration is invalid")
    else:
        raise ValueError("Notification server host:port configuration is invalid")

    with PostQueueServer(
        (host, int(port[0])), PostQueueRequestHandler, checker
    ) as server:
        server_thread = threading.Thread(target=server.serve_forever)
        server_thread.daemon = True
        server_thread.start()
        LOG.success("Notification server started")

        try:
            yield server
        finally:
            assert (
                server.check_errors() is True
            ), "Notification server caught malformed notifications"
            server.shutdown()
            server.server_close()
Exemplo n.º 3
0
    def test_post_json_request(self):
        ServerHandler = self.make_http_server_handler("HTTP/1.1")
        server = http.server.HTTPServer(("localhost", 8000), ServerHandler)
        asyncio.ensure_future(
            self.loop.run_in_executor(None, server.serve_forever))

        asyncio.ensure_future(
            self.get_coro_result(
                self.v11_client.post(
                    "http://localhost:8000/post_test/",
                    body_args={"a": "b"},
                    headers={"content-type": "application/json"})))

        self.loop.run_forever()
        server.shutdown()
        server.server_close()

        response = self.coro_result

        self.assertEqual(response.status_code, 200,
                         "Wrong Status Code")
        self.assertEqual(response.body, b"Hello, World!")

        body = self.received_body

        if hasattr(body, "getfirst"):
            self.assertEqual(body.getfirst("a"), "b")
        else:
            self.assertEqual(body.get("a"), "b")
Exemplo n.º 4
0
def mock_error_server(
        uri: str,
        err_code: int,
        err_msg: str = None,
        err_explain: str = None,
        host: str = "localhost",
        port: int = get_free_port(),
        method: HttpMethod = HttpMethod.GET,
):
    server = make_mock_error_server(
        host=host,
        port=port,
        uri=uri,
        method=method,
        err_code=err_code,
        err_msg=err_msg,
        err_explain=err_explain,
    )

    thread = threading.Thread(target=server.serve_forever)
    thread.daemon = True  # stop Python from biting ctrl-C
    thread.start()
    yield server
    server.shutdown()
    server.server_close()
Exemplo n.º 5
0
def mock_server(
    uri: str,
    body: str,
    host: str = "localhost",
    port: int = get_free_port(),
    method: HttpMethod = HttpMethod.GET,
    match_query: bool = False,
    echo: bool = False,
):
    server = make_mock_server(
        host=host,
        port=port,
        uri=uri,
        body=body,
        method=method,
        match_query=match_query,
        echo=echo,
    )

    thread = threading.Thread(target=server.serve_forever)
    thread.daemon = True  # stop Python from biting ctrl-C
    thread.start()
    yield server
    server.shutdown()
    server.server_close()
Exemplo n.º 6
0
def http_test_server():
    server = http.server.HTTPServer(('localhost', 0), TestHTTPHandler)
    child = threading.Thread(target=server.serve_forever)
    child.start()
    yield server
    server.shutdown()
    child.join()
Exemplo n.º 7
0
    def watch(self, outputFilename, port=None, localhost=False):
        import time
        outputFilename = self.fixMissingOutputFilename(outputFilename)
        if self.inputSource == "-" or outputFilename == "-":
            die("Watch mode doesn't support streaming from STDIN or to STDOUT."
                )
            return

        if port:
            # Serve the folder on an HTTP server
            import http.server
            import socketserver
            import threading

            class SilentServer(http.server.SimpleHTTPRequestHandler):
                def log_message(*args):
                    pass

            socketserver.TCPServer.allow_reuse_address = True
            server = socketserver.TCPServer(
                ("localhost" if localhost else "", port), SilentServer)

            print("Serving at port {0}".format(port))
            thread = threading.Thread(target=server.serve_forever)
            thread.daemon = True
            thread.start()
        else:
            server = None

        mdCommandLine = self.mdCommandLine

        try:
            lastInputModified = os.stat(self.inputSource).st_mtime
            self.preprocess()
            self.finish(outputFilename)
            p("==============DONE==============")
            try:
                while (True):
                    inputModified = os.stat(self.inputSource).st_mtime
                    if inputModified > lastInputModified:
                        resetSeenMessages()
                        lastInputModified = inputModified
                        formattedTime = datetime.fromtimestamp(
                            inputModified).strftime("%H:%M:%S")
                        p("Source file modified at {0}. Rebuilding...".format(
                            formattedTime))
                        self.initializeState()
                        self.mdCommandLine = mdCommandLine
                        self.preprocess()
                        self.finish(outputFilename)
                        p("==============DONE==============")
                    time.sleep(1)
            except KeyboardInterrupt:
                p("Exiting~")
                if server:
                    server.shutdown()
                    thread.join()
                sys.exit(0)
        except Exception as e:
            die("Something went wrong while watching the file:\n{0}", e)
Exemplo n.º 8
0
def test_get_file_urls(tmpdir):
    test_func = keepitfresh.get_file_urls

    tmpdir.ensure('example-0.1.0.zip', file=True)
    tmpdir.ensure('example-0.1.1.tar.gz', file=True)
    tmpdir.ensure('example-0.1.2.7z', file=True)
    tmpdir.ensure('example-0.1.3.rar', file=True)

    os.chdir(str(tmpdir))
    port = 8001
    handler = http.server.SimpleHTTPRequestHandler
    handler.log_message = lambda *a, **b: None
    http.server.HTTPServer.allow_reuse_address = True
    server = http.server.HTTPServer(("localhost", port), handler)

    thread = Thread(target=server.serve_forever, daemon=True)
    thread.start()

    test_url = 'http://localhost:{}/'.format(port)

    regex = r'example-(\d+\.\d+\.\d+)\.(?:tar\.gz|zip|rar|7z)'
    expected = {
            '{}example-0.1.1.tar.gz'.format(test_url): '0.1.1',
            '{}example-0.1.0.zip'.format(test_url): '0.1.0',
            '{}example-0.1.2.7z'.format(test_url): '0.1.2',
            '{}example-0.1.3.rar'.format(test_url): '0.1.3'}
    assert test_func(test_url, regex) == expected

    regex = r'mangledregex'
    assert test_func(test_url, regex) == {}

    server.shutdown()
Exemplo n.º 9
0
def test_is_fresh(tmpdir):
    test_func = keepitfresh.is_fresh

    tmpdir.ensure('example-0.1.0.zip', file=True)
    tmpdir.ensure('example-0.1.1.zip', file=True)
    tmpdir.ensure('example-0.1.2.zip', file=True)
    tmpdir.ensure('example-0.1.3.zip', file=True)

    os.chdir(str(tmpdir))
    port = 8000
    handler = http.server.SimpleHTTPRequestHandler
    handler.log_message = lambda *a, **b: None
    http.server.HTTPServer.allow_reuse_address = True
    server = http.server.HTTPServer(("localhost", port), handler)

    thread = Thread(target=server.serve_forever, daemon=True)
    thread.start()

    test_url = 'http://localhost:{}/'.format(port)
    regex = r'example-(\d+\.\d+\.\d+)\.(?:tar\.gz|zip|rar|7z)'

    assert not test_func(test_url, regex, '0.1.2')
    assert test_func(test_url, regex, '0.1.3')

    server.shutdown()
Exemplo n.º 10
0
def gather_token(config):
    """Create a server, run it in a thread, wait for handler, timeout."""
    # An event to signal a waiter thread that we've received the token.
    event = threading.Event()

    pr = urlparse(config.redirect_uri)
    server = HTTPServer((pr.hostname, pr.port),
                        TokenHandler,
                        config=config,
                        event=event)
    server.socket = ssl.wrap_socket(server.socket,
                                    keyfile=config.key_file,
                                    certfile=config.certificate_file,
                                    server_side=True)

    # Start server thread, wait for handler, timeout
    with server:
        thread = threading.Thread(target=server.serve_forever)
        thread.daemon = True
        thread.start()
        server.event.wait(timeout=300)
        server.shutdown()
        logging.info('Server: done')
        thread.join()

    return server.secrets
Exemplo n.º 11
0
def serve(directory, host_str, headless=False):
    host = host_str.split(":")
    if len(host) != 2:
        raise RuntimeError("Invalid host:port provided: " + host_str)
    server = None
    p = None
    wd = os.getcwd()
    try:
        os.chdir(directory)
        server = HTTPServer((host[0], int(host[1])), BaseHTTPRequestHandler)
        print("[HAROS] Serving visualisation at", host_str)
        if not headless:
            thread = threading.Thread(target=server.serve_forever)
            thread.daemon = True
            thread.start()
            _log.info("Starting web browser process.")
            cmd = ["python", "-m", "webbrowser", "-t", "http://" + host_str]
            with open(os.devnull, "wb") as devnull:
                p = subprocess.Popen(cmd,
                                     stdout=devnull,
                                     stderr=subprocess.STDOUT)
                input("[HAROS] Press enter to shutdown the viz server:")
        else:
            server.serve_forever()
        return True
    except ValueError as e:
        _log.error("Invalid port for the viz server %s", host[1])
        return False
    finally:
        if server:
            server.shutdown()
        os.chdir(wd)
        if p:
            _log.debug("Killing web browser process.")
            p.kill()
Exemplo n.º 12
0
  def test_upload_stats(self):
    stats = {'stats': {'foo': 'bar', 'baz': 42}}

    class Handler(http.server.BaseHTTPRequestHandler):
      def do_POST(handler):
        try:
          self.assertEquals('/upload', handler.path)
          self.assertEquals('application/x-www-form-urlencoded', handler.headers['Content-type'])
          length = int(handler.headers['Content-Length'])
          post_data = parse_qs(handler.rfile.read(length).decode('utf-8'))
          decoded_post_data = {k: json.loads(v[0]) for k, v in post_data.items()}
          self.assertEquals(stats, decoded_post_data)
          handler.send_response(200)
        except Exception:
          handler.send_response(400)  # Ensure the main thread knows the test failed.
          raise


    server_address = ('', 0)
    server = http.server.HTTPServer(server_address, Handler)
    host, port = server.server_address

    server_thread = threading.Thread(target=server.serve_forever)
    server_thread.daemon = True
    server_thread.start()

    self.assertTrue(RunTracker.post_stats('http://{}:{}/upload'.format(host, port), stats))

    server.shutdown()
    server.server_close()
Exemplo n.º 13
0
def test_server_and_custom_headers(responder, read_method, parquet_engine):
    if parquet_engine is not None:
        pytest.importorskip(parquet_engine)
        if parquet_engine == "fastparquet":
            pytest.importorskip("fsspec")

    custom_user_agent = "Super Cool One"
    df_true = pd.DataFrame({"header": [custom_user_agent]})

    # passing 0 for the port will let the system find an unused port
    with http.server.HTTPServer(("localhost", 0), responder) as server:
        server_thread = threading.Thread(target=server.serve_forever)
        server_thread.start()

        port = server.server_port
        if parquet_engine is None:
            df_http = read_method(
                f"http://localhost:{port}",
                storage_options={"User-Agent": custom_user_agent},
            )
        else:
            df_http = read_method(
                f"http://localhost:{port}",
                storage_options={"User-Agent": custom_user_agent},
                engine=parquet_engine,
            )
        server.shutdown()

        server.server_close()
        server_thread.join()

    tm.assert_frame_equal(df_true, df_http)
Exemplo n.º 14
0
def test_server_and_all_custom_headers(responder, read_method):
    custom_user_agent = "Super Cool One"
    custom_auth_token = "Super Secret One"
    storage_options = {
        "User-Agent": custom_user_agent,
        "Auth": custom_auth_token,
    }

    # passing 0 for the port will let the system find an unused port
    with http.server.HTTPServer(("localhost", 0), responder) as server:
        server_thread = threading.Thread(target=server.serve_forever)
        server_thread.start()

        port = server.server_port
        df_http = read_method(
            f"http://localhost:{port}",
            storage_options=storage_options,
        )
        server.shutdown()
        server.server_close()
        server_thread.join()

    df_http = df_http[df_http["0"].isin(storage_options.keys())]
    df_http = df_http.sort_values(["0"]).reset_index()
    df_http = df_http[["0", "1"]]

    keys = list(storage_options.keys())
    df_true = pd.DataFrame({
        "0": keys,
        "1": [storage_options[k] for k in keys]
    })
    df_true = df_true.sort_values(["0"])
    df_true = df_true.reset_index().drop(["index"], axis=1)

    tm.assert_frame_equal(df_true, df_http)
Exemplo n.º 15
0
def main():
    parser = argparse.ArgumentParser("Ecobee Exporter")

    parser.add_argument("--port",
                        type=int,
                        default=os.environ.get('PORT', 9756))
    parser.add_argument("--bind_address",
                        default=os.environ.get("BIND", "0.0.0.0"))
    parser.add_argument("--api_key", default=os.environ.get('APIKEY'))
    parser.add_argument("--auth_file",
                        default=os.environ.get('AUTH', 'pyecobee_db'))
    parser.add_argument("--verbose", "-v", action="count")

    args = parser.parse_args()

    if args.verbose or os.environ.get('EE_LOG_LEVEL'):
        level = logging.DEBUG
    else:
        level = logging.INFO

    logging.basicConfig(stream=sys.stdout, level=level)

    collector = ecobee_exporter.EcobeeCollector(args.api_key, args.auth_file)

    prometheus_client.REGISTRY.register(collector)

    handler = prometheus_client.MetricsHandler.factory(
        prometheus_client.REGISTRY)
    server = http.server.HTTPServer((args.bind_address, args.port), handler)
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        logging.info('Shutting down server...')
        server.shutdown()
Exemplo n.º 16
0
def gather_token(config):
    """Create a server, run it in a thread, wait for handler, timeout."""

    # An event to signal a waiter thread that we've received the token.
    event = threading.Event()

    # Create an HTTPS server.
    pr = urlparse(config.redirect_uri)
    server = HTTPServer((pr.hostname, pr.port),
                        TokenHandler,
                        config=config,
                        event=event)

    # Wrap the socket in SSL.
    context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
    context.load_cert_chain(certfile=config.certificate_file,
                            keyfile=config.key_file)
    with context.wrap_socket(server.socket, server_side=True) as ssocket:
        server.socket = ssocket

        # Start server thread, wait for handler, timeout
        with server:
            thread = threading.Thread(target=server.serve_forever)
            thread.daemon = True
            thread.start()
            server.event.wait(timeout=300)
            server.shutdown()
            logging.info('Server: done')
            thread.join()

    return server.secrets
Exemplo n.º 17
0
def http_wait_server():
    # Simple http handler which waits 10 seconds before responding
    class WaitHanlder(http.server.BaseHTTPRequestHandler):

        def do_HEAD(self):
            self.send_response(200)
            self.send_header("Content-type", "text/html")
            self.end_headers()

        def do_GET(self):
            time.sleep(10)
            try:
                self.send_response(200)
                self.send_header("Content-type", "text/html")
                self.end_headers()
                self.wfile.write(bytes("<html><head><title>Test wait handler</title></head>"
                                       "<body><p>Test Wait</p></body></html>", 'utf-8'))
                self.flush_headers()
            except BrokenPipeError:  # ignore broken pipe as socket might have been closed
                pass

        # disable logging

        def log_message(self, format, *args):
            pass

    with http.server.ThreadingHTTPServer(('', 10888), WaitHanlder) as server:
        server_thread = threading.Thread(target=server.serve_forever)
        server_thread.start()
        yield
        server.shutdown()
        server_thread.join()
Exemplo n.º 18
0
 def stop(server):
     """Stop the server after 60 seconds."""
     time.sleep(60)
     try:
         server.shutdown()
     except Exception:
         pass
Exemplo n.º 19
0
    def assert_upload_stats(self, *, response_code) -> None:
        stats = {'stats': {'foo': 'bar', 'baz': 42}}

        class Handler(http.server.BaseHTTPRequestHandler):
            def do_POST(handler):
                try:
                    if handler.path.startswith('/redirect'):
                        code = int(handler.path[-3:])
                        handler.send_response(code)
                        handler.send_header('location', mk_url('/upload'))
                        handler.end_headers()
                    else:
                        self.assertEqual('/upload', handler.path)
                        self.assertEqual('application/x-www-form-urlencoded',
                                         handler.headers['Content-type'])
                        length = int(handler.headers['Content-Length'])
                        post_data = parse_qs(
                            handler.rfile.read(length).decode())
                        decoded_post_data = {
                            k: json.loads(v[0])
                            for k, v in post_data.items()
                        }
                        self.assertEqual(stats, decoded_post_data)
                        self.assertEqual(handler.headers['User-Agent'],
                                         f"pants/v{VERSION}")
                        self.assertIn(handler.headers['X-Pants-Stats-Version'],
                                      {"1", "2"})
                        handler.send_response(response_code)
                        handler.end_headers()
                except Exception:
                    handler.send_response(
                        400)  # Ensure the main thread knows the test failed.
                    raise

        server_address = ('', 0)
        server = http.server.HTTPServer(server_address, Handler)
        host, port = server.server_address

        def mk_url(path):
            return f'http://{host}:{port}{path}'

        server_thread = threading.Thread(target=server.serve_forever)
        server_thread.daemon = True
        server_thread.start()

        self.context(for_subsystems=[Cookies])
        self.assertTrue(
            RunTracker.post_stats(mk_url('/upload'), stats, stats_version=1))
        self.assertTrue(
            RunTracker.post_stats(mk_url('/redirect307'),
                                  stats,
                                  stats_version=1))
        self.assertFalse(
            RunTracker.post_stats(mk_url('/redirect302'),
                                  stats,
                                  stats_version=2))

        server.shutdown()
        server.server_close()
Exemplo n.º 20
0
 def on_modified(self, event):
     global server
     if event.event_type != 'modified':
         return
     server.shutdown()
     print(f'{event.src_path} was modified\nRestarting...')
     os.chdir(CURR_DIR)
     os.execl(sys.executable, *([sys.executable]+sys.argv))
Exemplo n.º 21
0
 def start(self):
     server = http.server.ThreadingHTTPServer(('0.0.0.0', self.port), self.make_request_handler)
     print(f'Server starting on {self.port}, pid={os.getpid()}.')
     try:
         server.serve_forever()
     except KeyboardInterrupt:
         print('Goodbye.')
         server.shutdown()
Exemplo n.º 22
0
def update_devices(creds, args):
    if args.models:
        data = get_device_list(creds)
        args.devices = [
            x['mac'] for x in data['device_list']
            if x['product_model'] in args.models
        ]

    firmware_data = open(args.firmware, 'rb').read()
    md5 = hashlib.md5(firmware_data).hexdigest()

    server = None
    for mac in args.devices:
        logging.info("Checking device, mac=%s" % mac)
        try:
            dev_info = get_device_info(creds, mac)
        except RuntimeError as e:
            print(e)
            continue

        # TODO: Skipping devices not having valid IP address
        print()
        print('Device type:      %s (%s)' %
              (dev_info['product_type'], dev_info['product_model']))
        print('Device name:      %s' % dev_info['nickname'])
        print('Firmware version: %s' % dev_info['firmware_ver'])
        print('IP Address:       %s' % dev_info['ip'])
        print()

        answer = input("Pushing firmware to this device? [y/N]:")
        if answer.upper() != 'Y':
            logging.info("Skipping device %s (%s)..." %
                         (dev_info['nickname'], mac))
            continue

        if not server:
            if not args.addr:
                args.addr = get_host_ip(dev_info['ip'])
            url = build_url(args.addr, args.ssl, args.port)
            server = start_http_server(firmware_data, args.addr, args.port,
                                       args.ssl, args.serve_dir)
            logging.info("Serving firmware file '%s' as '%s', md5=%s" %
                         (args.firmware, url, md5))

        push_update(creds, dev_info['product_model'], mac, url, md5)
        time.sleep(3)

    if server:
        print("Press Ctrl+C when all the updates are done...")
        try:
            while True:
                time.sleep(1)
                print(".", end="", flush=True)
        except KeyboardInterrupt:
            print()

        logging.info("Stopping http server...")
        server.shutdown()
def init(port=8767):
    thread = threading.Thread(target=run, args=(port,))
    thread.daemon = True

    try:
        thread.start()
    except KeyboardInterrupt:
        server.shutdown()
        sys.exit(0)
Exemplo n.º 24
0
def webseed(data: bytes) -> Iterator[str]:
    handler = functools.partial(WebSeedRequestHandler, data)
    with http.server.HTTPServer(("127.0.0.1", 0), handler) as server:
        thread = threading.Thread(target=server.serve_forever)
        thread.daemon = True
        thread.start()
        addr, port = server.server_address
        yield f"http://{addr}:{port}/"
        server.shutdown()
def main():
    try:
        port = 8080
        server = HTTPServer(("", port), WebServerHandler)
        print("Web Server running on port %s" % port)
        server.serve_forever()
    except KeyboardInterrupt:
        print("^C entered, stopping web server...")
        server.shutdown()
Exemplo n.º 26
0
def init(port):
    # If you want cgi, set cgi to True e.g. start_server(cgi=True)
    thread = threading.Thread(target=start_server, args=(port, ))
    thread.daemon = True

    try:
        thread.start()
    except KeyboardInterrupt:
        server.shutdown()
        sys.exit(0)
Exemplo n.º 27
0
def test_cases():
    try:
        ready.get()
        f = foundry.Foundry(
            "http://*****:*****@id='content']")[0])
        print(f.alloy())
    except:
        server.shutdown()
        raise
Exemplo n.º 28
0
 def __init__(self):
     server = http.server.HTTPServer(self.server_address, self.AjaxServer)
     thread = threading.Thread(target=server.serve_forever)
     thread.daemon = True
     try:
         thread.start()
     except KeyboardInterrupt:
         server.shutdown()
         sys.exit(0)
     monad.log('portal server thread started')
Exemplo n.º 29
0
def CaptureCallback(channel):
    global WebCamPreviewActive, server

    #if GPIO.input(CapturePin) == False:
    #print("Capture: Falling-edge interrupt detected")
    if WebCamPreviewActive == True:
        print("Stopping WebCam preview...")
        camera.stop_recording()
        WebCamPreviewActive = False
        server.shutdown()
Exemplo n.º 30
0
    def run(self):
        """This is the function that needs to be started to create a thread"""
        logger.info(
            f"The imager control thread has been started in process {os.getpid()}"
        )
        # MQTT Service connection
        self.imager_client = planktoscope.mqtt.MQTT_Client(
            topic="imager/#", name="imager_client")

        self.imager_client.client.publish("status/imager",
                                          '{"status":"Starting up"}')

        if self.__camera.sensor_name == "IMX219":  # Camera v2.1
            self.imager_client.client.publish("status/imager",
                                              '{"camera_name":"Camera v2.1"}')
        elif self.__camera.sensor_name == "IMX477":  # Camera HQ
            self.imager_client.client.publish("status/imager",
                                              '{"camera_name":"HQ Camera"}')
        else:
            self.imager_client.client.publish(
                "status/imager", '{"camera_name":"Not recognized"}')

        logger.info("Starting the streaming server thread")
        address = ("", 8000)
        fps = 16
        refresh_delay = 1 / fps
        handler = functools.partial(StreamingHandler, refresh_delay)
        server = StreamingServer(address, handler)
        self.streaming_thread = threading.Thread(target=server.serve_forever,
                                                 daemon=True)
        self.streaming_thread.start()

        # Publish the status "Ready" to via MQTT to Node-RED
        self.imager_client.client.publish("status/imager",
                                          '{"status":"Ready"}')

        logger.success("Camera is READY!")

        # This is the main loop
        while not self.stop_event.is_set():
            if self.imager_client.new_message_received():
                self.treat_message()
            self.state_machine()
            time.sleep(0.001)

        logger.info("Shutting down the imager process")
        self.imager_client.client.publish("status/imager", '{"status":"Dead"}')
        logger.debug("Stopping the raspimjpeg process")
        self.__camera.close()
        logger.debug("Stopping the streaming thread")
        server.shutdown()
        logger.debug("Stopping MQTT")
        self.imager_client.shutdown()
        # self.streaming_thread.kill()
        logger.success("Imager process shut down! See you!")
Exemplo n.º 31
0
def PreviewCallback(channel):
    global WebCamPreviewActive, server
    if GPIO.input(PreviewPin) == False:
        print("Preview: Falling-edge interrupt detected")
    else:
        print("Preview: Rising-edge interrupt detected")
    if WebCamPreviewActive == True:
        print("Stopping WebCam preview...")
        camera.stop_recording()
        WebCamPreviewActive = False
        server.shutdown()
Exemplo n.º 32
0
def serve(project_name):
    b = little.Blog(project_name)
    settings = b.settings
    os.chdir(settings.OUTPUT_DIR)
    handler = http.server.SimpleHTTPRequestHandler
    server = socketserver.TCPServer(("", 8080), handler)
    click.echo("Serving on http://localhost:8080")
    try:
        server.serve_forever()
    except OSError:
        server.shutdown()
Exemplo n.º 33
0
def run_server():
    host = "0.0.0.0"
    socketserver.ThreadingMixIn.allow_reuse_address = True

    try:
        server = LocalPasteServer((host, args.port), LocalPasteHandler)
        log("Starting server... hit ctrl+c to exit")
        server.serve_forever()
    except KeyboardInterrupt as e:
        log("Stopping server...")
        server.shutdown()
        raise
Exemplo n.º 34
0
def run_server():
    socketserver.ThreadingMixIn.allow_reuse_address = True

    try:
        server = LocalPasteServer((args.listen_address, args.port), LocalPasteHandler)
        log("Starting server... hit ctrl+c to exit")
        if args.user:
            drop_privileges(args.user)
        server.serve_forever()
    except KeyboardInterrupt as e:
        log("Stopping server...")
        server.shutdown()
        raise
Exemplo n.º 35
0
 def web_loop(self):
     port = 4444
     print((self.CANEngine.ascii_logo_c))
     WebConsole.can_engine = self.CANEngine
     server = ThreadingSimpleServer(('', port), WebConsole)
     print(("CANtoolz WEB started at port: ", port))
     print("\tTo exit CTRL-C...")
     try:
         sys.stdout.flush()
         server.serve_forever()
     except KeyboardInterrupt:
         server.shutdown()
         server.server_close()
         self.CANEngine.stop_loop()
         print("gg bb")
         exit()
Exemplo n.º 36
0
 def web_loop(self, host='127.0.0.1', port=4444):
     print((self.CANEngine.ascii_logo_c))
     WebConsole.can_engine = self.CANEngine
     server = ThreadingSimpleServer((host, port), WebConsole)
     print('CANtoolz WEB started and bound to: http://{0}:{1}'.format(host, port))
     print("\tTo exit CTRL-C...")
     try:
         sys.stdout.flush()
         server.serve_forever()
     except KeyboardInterrupt:
         server.shutdown()
         server.server_close()
         print("Please wait... (do not press ctr-c again!)")
         self.CANEngine.stop_loop()
         self.CANEngine.engine_exit()
         print("gg bb")
         exit()
Exemplo n.º 37
0
    def test_v11_get_request(self):
        ServerHandler = self.make_http_server_handler("HTTP/1.1")
        server = http.server.HTTPServer(("localhost", 8000), ServerHandler)
        asyncio.ensure_future(
            self.loop.run_in_executor(None, server.serve_forever))

        asyncio.ensure_future(
            self.get_coro_result(
                self.v11_client.get("http://localhost:8000/get_test/")))

        self.loop.run_forever()
        server.shutdown()
        server.server_close()

        response = self.coro_result

        self.assertEqual(response.status_code, 200,
                         "Wrong Status Code")
        self.assertEqual(response.body, b"Hello, World!")
Exemplo n.º 38
0
  def test_upload_stats(self):
    stats = {'stats': {'foo': 'bar', 'baz': 42}}

    class Handler(http.server.BaseHTTPRequestHandler):
      def do_POST(handler):
        try:
          if handler.path.startswith('/redirect'):
            code = int(handler.path[-3:])
            handler.send_response(code)
            handler.send_header('location', mk_url('/upload'))
            handler.end_headers()
          else:
            self.assertEqual('/upload', handler.path)
            self.assertEqual('application/x-www-form-urlencoded', handler.headers['Content-type'])
            length = int(handler.headers['Content-Length'])
            post_data = parse_qs(handler.rfile.read(length).decode('utf-8'))
            decoded_post_data = {k: json.loads(v[0]) for k, v in post_data.items()}
            self.assertEqual(stats, decoded_post_data)
            handler.send_response(200)
            handler.end_headers()
        except Exception:
          handler.send_response(400)  # Ensure the main thread knows the test failed.
          raise

    server_address = ('', 0)
    server = http.server.HTTPServer(server_address, Handler)
    host, port = server.server_address

    def mk_url(path):
      return 'http://{}:{}{}'.format(host, port, path)

    server_thread = threading.Thread(target=server.serve_forever)
    server_thread.daemon = True
    server_thread.start()

    self.context(for_subsystems=[Cookies])
    self.assertTrue(RunTracker.post_stats(mk_url('/upload'), stats))
    self.assertTrue(RunTracker.post_stats(mk_url('/redirect307'), stats))
    self.assertFalse(RunTracker.post_stats(mk_url('/redirect302'), stats))

    server.shutdown()
    server.server_close()
Exemplo n.º 39
0
        def test_callback(self):
            server_ip = self.client.inspect_container(self.repour_container)["NetworkSettings"]["Gateway"]
            server_port = 8080

            server_done = threading.Event()
            callback_data = None
            class Handler(http.server.BaseHTTPRequestHandler):
                def do_POST(self):
                    nonlocal callback_data
                    size = int(self.headers.get("content-length", 0))
                    callback_data = json.loads(self.rfile.read(size).decode("utf-8"))
                    self.send_response(200)
                    self.end_headers()
                    self.flush_headers()
                    server_done.set()

                def log_request(self, format, *args):
                    pass

            server = http.server.HTTPServer((server_ip, server_port), Handler)
            server_thread = threading.Thread(target=server.serve_forever, daemon=True)
            try:
                server_thread.start()
                resp = self.requests_session.post(
                    url=self.repour_api_url + "/pull",
                    json={
                        "name": "jboss-modules",
                        "type": "git",
                        "url": "https://github.com/jboss-modules/jboss-modules.git",
                        "ref": "master",
                        "callback": {
                            "url": "http://{server_ip}:{server_port}/".format(**locals()),
                        },
                    },
                )
                ret = resp.json()
                server_done.wait(timeout=10)
            finally:
                server.shutdown()
                server_thread.join(timeout=5)
            self.assertEqual(200, callback_data["callback"]["status"])
            self.assertEqual(ret["callback"]["id"], callback_data["callback"]["id"])
Exemplo n.º 40
0
if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="A Network Block Device (NBD) server")
    parser.add_argument(
        "--size", type=int, metavar="megs", default=16,
        help="disk size in megabytes (default: 16)")
    parser.add_argument(
        "--load", type=str, metavar="file",
        help="load file as disk image")
    args = parser.parse_args()

    if args.load != None:
        TCPServer.backend = bytearray(open(args.load, "rb").read())
    else:
        TCPServer.backend = bytearray(args.size * 1024 * 1024)
    TCPServer.lock = threading.Lock()

    host, port = "", 10809
    server = TCPServer((host, port), NBDHandler)
    # monitor
    monitor = TCPServer(("", 10880), HTTPHandler)
    threading.Thread(target=monitor.serve_forever).start()
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        pass
    finally:
        server.shutdown()
        monitor.shutdown()
Exemplo n.º 41
0
def cleanup_handler():
    print('shutting down python server...')
    server.shutdown()
def stop():
    if server:
        server.shutdown()
Exemplo n.º 43
0
def main(stdscr):
    global done, downloads, position, mode, max_x, max_y, scroll, menu_status, menu_results, menu_help, max_display, screen, sub_mode, port

    server = None
    if port != 0:
        ThreadedTCPServer.allow_reuse_address = True
        server = ThreadedTCPServer((hostname, port), HttpHandler)

        server_thread = threading.Thread(target=server.serve_forever)
        server_thread.daemon = True
        server_thread.start()

    download_thread = threading.Thread(target=process_download_queue)
    download_thread.daemon = True
    download_thread.start()

    subreddit = ""
    search = ""
    screen = curses.initscr()
    curses.curs_set(0)

    (max_y, max_x) = screen.getmaxyx()
    max_display = max_y - 4
    curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)
    curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_RED)
    curses.init_pair(3, curses.COLOR_BLUE, curses.COLOR_BLACK)
    curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_RED)
    curses.init_pair(5, curses.COLOR_GREEN, curses.COLOR_BLACK)
    curses.init_pair(6, curses.COLOR_GREEN, curses.COLOR_RED)

    menu_results = curses.newpad(100, 300)
    menu_status = curses.newpad(1, 300)
    menu_help = curses.newpad(1, 300)
    restore_state()

    load_downloads()
    draw_results()
    draw_help()
    if server:
        set_status("web server started on " + gethostbyname(gethostname()) + ":" + str(port))

    curses.doupdate()
    while True:
        c = screen.getch()
        redraw = 0
        if c == curses.KEY_RESIZE:
            (max_y, max_x) = screen.getmaxyx()
            max_display = max_y - 4
            menu_results.clear()
            redraw = 1
        elif c == 6 or c == 338:
            # ^F or pagedown
            for i in range(0, max_display - 1):
                if position >= max_display - 1 and scroll < len(mode_results[mode]) - max_display:
                    scroll += 1
                elif position + scroll < len(mode_results[mode]) - 1:
                    position += 1
            redraw = 1
        elif c == 2 or c == 339:
            # ^B or pageup
            for i in range(0, max_display - 1):
                if position <= 0 and scroll > 0:
                    scroll -= 1
                elif position + scroll > 0:
                    position -= 1
            redraw = 1
        elif c == 10 or c == ord("c") or c == ord("m"):
            if mode == "youtube" and c == ord("c"):

                # get input
                channel = get_input("channel: ", screen)

                # load channel
                position = 0
                scroll = 0
                search = ""
                sub_mode = ""
                load_youtube(channel, True)
                menu_results.erase()
                redraw = 1
            else:
                open_chat = False
                if c == ord("c"):
                    open_chat = True

                movie_mode = False
                if c == ord("m"):
                    movie_mode = True

                (status, redraw) = handle_selection(open_chat, movie_mode)
                if redraw == 1:
                    menu_results.erase()
                    position = 0
                    scroll = 0
        elif c == ord("1"):
            mode = "downloads"
            load_downloads()
            menu_results.erase()
            position = 0
            scroll = 0
            redraw = 1
        elif c == ord("2"):
            mode = "reddit"
            menu_results.erase()
            position = 0
            scroll = 0
            set_status(mode_status[mode])
            redraw = 1
        elif c == ord("3"):
            mode = "youtube"
            menu_results.erase()
            position = 0
            scroll = 0
            set_status(mode_status[mode])
            redraw = 1
        elif c == ord("4"):
            mode = "twitch"
            menu_results.erase()
            position = 0
            scroll = 0

            # load games by default
            if len(mode_results[mode]) == 0:
                load_twitch_games()
            set_status(mode_status[mode])
            redraw = 1
        elif c == ord("a"):
            handle_playall(screen)
        elif c == ord("q"):
            if server:
                server.shutdown()
                server.server_close()
            break
        elif c == ord("d"):
            if mode == "downloads":
                delete_selection()
                menu_results.erase()
                load_downloads()
                update = len(mode_results[mode]) - (scroll + max_display)
                if update < 0 and len(mode_results[mode]) >= max_display:
                    scroll += update
                    position -= update
                    if scroll < 0:
                        scroll = 0
                if position >= len(mode_results[mode]):
                    position -= 1
                redraw = 1
        elif c == ord("/"):
            if mode == "reddit":

                # get input
                search = get_input("search /r/" + subreddit + ": ", screen)

                # load results
                if search != "":
                    position = 0
                    scroll = 0
                    load_subreddit(subreddit, search)
                    menu_results.erase()
                    redraw = 1
            elif mode == "youtube":

                # get input
                query = get_input("youtube: ", screen)

                # load search results
                if query != "":
                    position = 0
                    scroll = 0
                    load_youtube(query)
                    menu_results.erase()
                    redraw = 1
            elif mode == "downloads":

                # get input
                mode_query[mode] = get_input("find: ", screen)

                # find item
                if mode_query[mode] != "":
                    new_scroll = find_result(mode_query[mode], 0)
                    if new_scroll == -1:
                        set_status("not found")
                    else:
                        position = 0
                        scroll = new_scroll

                    menu_results.erase()
                    redraw = 1

        elif c == ord("t"):
            if mode == "youtube":
                show_thumbnail()
        elif c == ord("n"):
            if mode == "downloads":

                # find item
                if mode_query[mode] != "":
                    new_scroll = find_result(mode_query[mode], position + scroll + 1)
                    if new_scroll == -1:
                        set_status("not found")
                    else:
                        position = 0
                        scroll = new_scroll

                    menu_results.erase()
                    redraw = 1
        elif c == ord("s"):
            if mode == "reddit":

                # get input
                subreddit = get_input("subreddit: ", screen)

                # load new subreddit
                position = 0
                scroll = 0
                search = ""
                load_subreddit(subreddit)
                menu_results.erase()
                redraw = 1
        elif c == ord("g"):
            if mode == "twitch":

                # load twitch games
                position = 0
                scroll = 0
                search = ""
                sub_mode = ""
                load_twitch_games()
                menu_results.erase()
                redraw = 1
        elif c == ord("r"):
            if mode == "reddit":
                load_subreddit(subreddit, search, force=1)
                menu_results.erase()
                redraw = 1
            elif mode == "downloads":
                menu_results.erase()
                load_downloads()
                redraw = 1
            elif mode == "twitch":
                menu_results.erase()
                if sub_mode == "":
                    load_twitch_games()
                else:
                    load_twitch_streams()
                redraw = 1
            clamp_cursor()
        elif c == curses.KEY_UP or c == ord("k"):
            if position <= 0 and scroll > 0:
                scroll -= 1
            elif position + scroll > 0:
                position -= 1
            redraw = 1
        elif c == curses.KEY_DOWN or c == ord("j"):
            if position >= max_display - 1 and scroll < len(mode_results[mode]) - max_display:
                scroll += 1
            elif position + scroll < len(mode_results[mode]) - 1:
                position += 1
            redraw = 1

        if redraw:
            draw_results()
            draw_help()

        curses.doupdate()

    curses.endwin()
    done = 1
    download_thread.join()
Exemplo n.º 44
0
def shutDown():
	time.sleep(2)
	print("shutDown server...")
	if server is not None: 
		server.shutdown()