def main():
    init_logging('metric_collector.log')
    options = get_options()

    spectator = SpectatorClient(options)
    try:
        stackdriver = StackdriverClient.make_client(options)
    except IOError as ioerror:
        logging.error(
            'Could not create stackdriver client -- Stackdriver will be unavailable\n%s',
            ioerror)
        stackdriver = None

    if options.command:
        process_command(options.command, spectator, stackdriver, options)
        return

    path_handlers = {
        '/': handlers.BaseHandler(options),
        '/clear': handlers.ClearCustomDescriptorsHandler(options, stackdriver),
        '/dump': handlers.DumpMetricsHandler(options, spectator),
        '/list': handlers.ListCustomDescriptorsHandler(options, stackdriver),
        '/explore':
        handlers.ExploreCustomDescriptorsHandler(options, spectator),
        '/show': handlers.ShowCurrentMetricsHandler(options, spectator)
    }

    logging.info('Starting HTTP server on port %d', options.port)
    httpd = HttpServer(options.port, path_handlers)
    httpd.serve_forever()
def test_http_server_not_found(mock_socket):
    sample_req = \
        (f"GET /somefile.txt HTTP/1.1\r\n"
         f"Host: localhost\r\n"
         f"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0)"
         f"Gecko/20100101 Firefox/69.0\r\n"
         f"Accept: text/html,application/xhtml+xml"
         f"application/xml;q=0.9,*/*;q=0.8\r\n"
         f"Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3\r\n"
         f"Accept-Encoding: gzip, deflate, br\r\n\r\n"
         )
    mock_socket.return_value.bind.return_value = None
    mock_socket.return_value.listen.return_value = None
    mock_socket.return_value.setblocking.return_value = None
    mock_socket.return_value.settimeout.return_value = None
    mock_socket.return_value.recv.side_effect = [bytes(sample_req, "utf-8")]
    mock_socket.return_value.sendall.side_effect = (lambda x: len(x))
    mock_socket.return_value.send.side_effect = (lambda x: len(x))
    mock_socket.return_value.fileno.return_value = 0
    mock_socket.return_value.close.return_value = None
    client_instance = mock_socket(AF_INET, SOCK_STREAM)
    mock_socket.return_value.accept.side_effect = [(client_instance, None),
                                                   mock.DEFAULT]

    http_server = HttpServer("test_config.json")
    http_server.run()
    http_server.thread_pool.tasks.join()
    http_server.thread_pool.terminate_all_workers()

    response, *_ = client_instance.sendall.call_args[0]
    str_response = response.decode('utf-8', errors='ignore')
    header = str_response.partition('\r\n')[0]

    assert header == 'HTTP/1.1 404'
def main():
    init_logging("metric_collector.log")
    options = get_options()

    spectator = SpectatorClient(options)
    try:
        stackdriver = StackdriverClient.make_client(options)
    except IOError as ioerror:
        logging.error("Could not create stackdriver client -- Stackdriver will be unavailable\n%s", ioerror)
        stackdriver = None

    if options.command:
        process_command(options.command, spectator, stackdriver, options)
        return

    path_handlers = {
        "/": handlers.BaseHandler(options),
        "/clear": handlers.ClearCustomDescriptorsHandler(options, stackdriver),
        "/dump": handlers.DumpMetricsHandler(options, spectator),
        "/list": handlers.ListCustomDescriptorsHandler(options, stackdriver),
        "/explore": handlers.ExploreCustomDescriptorsHandler(options, spectator),
        "/show": handlers.ShowCurrentMetricsHandler(options, spectator),
    }

    logging.info("Starting HTTP server on port %d", options.port)
    httpd = HttpServer(options.port, path_handlers)
    httpd.serve_forever()
def server_running(request):
    """Start the server and have it listening and ready to respond"""
    server = HttpServer()
    t = threading.Thread(target=server.listen_continuously)
    t.daemon = True
    t.start()
    yield
    server.listening = False
    server.close()
Beispiel #5
0
def main():

    if len(sys.argv) > 1 and path.isfile(sys.argv[1]):
        # 读取用户配置
        config = getconfig(sys.argv[1])
    else:
        # 使用默认配置
        config = DEFAULT_CONFIG

    signal.signal(signal.SIGINT, shutdown)

    server = HttpServer(config["root"], config["port"], config["index"],)

    server.start()
    def test_get_content_python_file(self):
        path = "/make_time.py"

        content = HttpServer.get_content(path)

        with open(os.path.join("webroot", "make_time.py"), "rb") as f:
            self.assertEqual(f.read(), content)
Beispiel #7
0
    def test_get_content_file(self):
        path = "/a_web_page.html"

        content = HttpServer.get_content(path)

        with open(os.path.join("webroot", "a_web_page.html"), "rb") as f:
            self.assertEqual(f.read(), content)
Beispiel #8
0
    def test_get_content_dir(self):
        path = "/"

        content = HttpServer.get_content(path)

        self.assertIn(b"favicon.ico", content)
        self.assertIn(b"make_time.py", content)
        self.assertIn(b"sample.txt", content)
        self.assertIn(b"a_web_page.html", content)
Beispiel #9
0
    def start(self):
        """
        Start the main loop of the application.

        Create instance of FhtListener and HttpServer and start them. Listen on the queue
        from the Listener and when receive the message, parse it and send the result to server.
        """
        self.listener_queue = Queue()
        listener = FhtListener(self.listener_queue)
        self.http_queue = Queue()
        http_server = HttpServer(self.http_queue)
        listener.start()
        http_server.start()

        while True:
            msg: FhtMessage = self.listener_queue.get()
            logger.debug("FHT message received")
            # process the message and create a message for http server
            http_msg = self.analyze_msg(msg)
            self.http_queue.put(http_msg)
Beispiel #10
0
 def test_init_from_options(self, mock_http):
     """Verify we pick up commandline args, and empty host is passed through."""
     options = {
         'port': 1234,
         'host': '',
         'server': {
             'port': 666,
             'host': 'wrong'
         }
     }
     server = HttpServer(options)
     mock_http.assert_called_with(server, ('0.0.0.0', 1234), mock.ANY)
Beispiel #11
0
 def test_init_from_server_options(self, mock_http):
     """Verify that if options arent overriden we get server config."""
     options = {
         'port': None,
         'host': None,
         'server': {
             'port': 1234,
             'host': 'testHost'
         }
     }
     server = HttpServer(options)
     mock_http.assert_called_with(server, ('testHost', 1234), mock.ANY)
Beispiel #12
0
def main():
    if len(sys.argv) != 2:
        print('Usage: python main.py <node_name>')
        sys.exit(1)

    node_name = sys.argv[1]

    configuration = Configuration()
    configuration.load()
    if node_name not in configuration.nodes.keys():
        print('Node {} is not in cluster.conf'.format(node_name))
        sys.exit(1)

    state = State()
    state.load(node_name)

    raft = Raft(node_name, configuration, state)

    raft.reset_election_timer()
    server = HttpServer(raft.parse_json_request)
    server.run(configuration.nodes[node_name]['port'])
Beispiel #13
0
    def run(self):
        # Get event loop
        # --------------------
        # Get the current event loop. If there is no current event loop set in 
        # the current OS thread and set_event_loop() has not yet been called, 
        # asyncio will create a new event loop and set it as the current one.
        self.loop = asyncio.get_event_loop()
        self.context.loop = self.loop
        self.futures = []

        # Register actor instances
        logger.info('Registering {0} actor instances'.format(
            len(self.context.config['actors'])
        ))
        
        self.context.actors = list(map(
            lambda a: a['class'](a, self.context), self.context.config['actors']
        ))

        # Message processor
        self.futures.append(asyncio.ensure_future(self.events_processor()))

        try:
            # Servers
            self.context.http_server = HttpServer()
            self.context.socket_server = SocketServer(async_mode='aiohttp')
            self.context.socket_server.configure(self.context)
            
            asyncio.ensure_future(
                self.context.http_server.configure(self.context), 
                loop=self.loop)

            # Startup event
            self.context.queue_event('homeghost', 'startup', payload={})

            # Run the event loop
            logger.info('Running core event loop')
            self.context.running = True
            self.loop.run_forever()


        finally:
            # Stop event loop
            self.context.running = False

            asyncio.wait(self.futures, loop=self.loop)

            self.loop.close()
Beispiel #14
0
    def test_make_response(self):
        code = b"200"
        reason = b"OK"
        body = b"foo"
        mimetype = b"image/bmp"

        response = HttpServer.make_response(code, reason, body, mimetype)
        str_response = response.decode()

        self.assertIn("\r\n\r\n", str_response)

        str_header, str_body = str_response.split("\r\n\r\n")

        self.assertEqual(body.decode(), str_body)
        self.assertEqual("HTTP/1.1 200 OK", str_header.splitlines()[0])
        self.assertIn("Content-Type: " + mimetype.decode(), str_header)
Beispiel #15
0
    def setUp(self):
        self.handler_a = mock.Mock()
        self.handler_b = mock.Mock()

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.bind(('localhost', 0))
        self.port = sock.getsockname()[1]
        sock.close()

        server = HttpServer({'port': self.port},
                            handlers={
                                '/path/a': self.handler_a,
                                '/path/b': self.handler_b
                            })
        self.thread = threading.Thread(target=server.handle_request)
        self.thread.daemon = True
        self.thread.start()
Beispiel #16
0
    def test_get_mimetype_file(self):
        path = "/a_web_page.html"

        mimetype = HttpServer.get_mimetype(path)

        self.assertEqual(b"text/html", mimetype)
Beispiel #17
0
def _init_http_server():
    asyncio.run(HttpServer.run())
Beispiel #18
0
def main():
    init_logging('metric_collector.log')
    options = get_options()

    spectator = spectator_client.SpectatorClient(options)
    try:
        stackdriver = stackdriver_client.StackdriverClient.make_client(options)
    except IOError as ioerror:
        logging.error(
            'Could not create stackdriver client'
            ' -- Stackdriver will be unavailable\n%s', ioerror)
        stackdriver = None

    registry = []
    registry.extend([
        CommandDefinition(handlers.BaseHandler(options, registry), '/', 'Home',
                          CommandRequest(options=options),
                          'Home page for Spinnaker metric administration.'),
        CommandDefinition(
            stackdriver_handlers.ClearCustomDescriptorsHandler(
                options,
                stackdriver), '/stackdriver/clear_descriptors', 'clear',
            CommandRequest(options=options),
            'Clear all the Stackdriver Custom Metrics'),
        CommandDefinition(
            stackdriver_handlers.ListCustomDescriptorsHandler(
                options, stackdriver), '/stackdriver/list_descriptors', 'list',
            CommandRequest(content_type='application/json', options=options),
            'Get the JSON of all the Stackdriver Custom Metric Descriptors.'),
        CommandDefinition(
            handlers.DumpMetricsHandler(options, spectator), '/dump', 'dump',
            CommandRequest(options=options),
            'Show current raw metric JSON from all the servers.'),
        CommandDefinition(
            handlers.ExploreCustomDescriptorsHandler(options, spectator),
            '/explore',
            'explore',
            CommandRequest(options=options),
            'Explore metric type usage across Spinnaker microservices.',
        ),
        CommandDefinition(
            handlers.ShowCurrentMetricsHandler(options,
                                               spectator), '/show', 'show',
            CommandRequest(options=options),
            'Show current metric JSON for all Spinnaker.'),
    ])

    if options.command:
        command_processor.process_command(options.command, registry)
        return

    if options.monitor:
        logging.info('Starting Monitor every %d s', options.period)

        # TODO: Replace this with a real service.
        metric_service = DummyMetricService()

        monitor = Monitor(spectator, metric_service, options)
        threading.Thread(target=monitor, name='monitor').start()

    logging.info('Starting HTTP server on port %d', options.port)
    url_path_to_handler = {entry.url_path: entry.handler for entry in registry}
    httpd = HttpServer(options.port, url_path_to_handler)
    httpd.serve_forever()
    sys.exit(-1)
Beispiel #19
0
 def test_init_from_hardcoded_defaults(self, mock_http):
     """Verify if options arent overriden that we get the builtin defaults."""
     options = {'port': None, 'host': None}
     server = HttpServer(options)
     mock_http.assert_called_with(server, ('localhost', 8008), mock.ANY)
#!/usr/bin/env python3
import sys
import pymysql
from os import environ
from settings import settings
from http_server import HttpServer
from photo_manager import PhotoManager

http_server = HttpServer(environ, sys.stdin)
http_server.set_header("Access-Control-Allow-Methods", "GET, PUT, OPTIONS")

def connect_to_mysql():
    conn = pymysql.connect(
       host=settings["mysql"]["host"],
       port=settings["mysql"]["port"],
       user=settings["mysql"]["user"],
       passwd=settings["mysql"]["password"],
       db=settings["mysql"]["schema"],
       charset=settings["mysql"]["charset"],
       autocommit=True
    )
    return conn

def run():
    method = http_server.get_method()
    http_server.set_status(200)
    http_server.print_headers()
    http_server.print_content(method)

    if method == "OPTIONS":
        http_server.print_headers()
Beispiel #21
0
# import mepy.others.video_capture.pygame_video_capture as PVC

from http_server import HttpServer
# from mepy.program import Program

# program = Program({})
program = {}

server = HttpServer(program, {})

server.init()

capture = True
while capture:
    input('asdf')
    capture = False
Beispiel #22
0
from http_server import HttpServer
import xml.etree.ElementTree as ET

from mg.DataStorage import DataStorage
from mg.Factory import Factory


class RequestHandler:
    def __init__(self, server):
        self.response = None
        self.server = server

    def handle(self, payload):
        request = Factory.create_command(payload)

        response = request.execute()

        root = ET.Element(response.get_type())
        response.serialize(root)
        sbuffer = ET.tostring(root)
        self.server.send(sbuffer)


if __name__ == '__main__':
    print 'run server:'
    DataStorage.shared().initialize(open('data.xml').read())
    HttpServer.start(port=8045, request_handler_class=RequestHandler)
Beispiel #23
0
    def test_get_mimetype_dir(self):
        path = "/foo/"

        mimetype = HttpServer.get_mimetype(path)

        self.assertIn(mimetype, [b"text/html", b"text/plain"])
Beispiel #24
0
                          cookies=cookies)
    except Exception as e:
        # 失败
        return ResponseObj('error', 'call unisound api failed').format()
    # 成功
    return ResponseObj('success', json.loads(r.text)).format()


if __name__ == "__main__":
    # 服务端口
    try:
        port = int(sys.argv[1])
    except:
        print("USAGE: service port")
        sys.exit(-1)
    # 加载配置文件
    config_file = sys.argv[2]
    config.read(config_file)
    # 注册服务
    HOST, PORT = "", port
    server = HttpServer(HOST, PORT)
    server.Register("/tuling", tuling_api)
    server.Register("/tencent", tencent_api)
    server.Register("/ifly", ifly_api)
    server.Register("/unisound", unisound_api)
    # TODO
    server.Register("/tuling/", tuling_api)
    server.Register("/tencent/", tencent_api)
    server.Register("/ifly/", ifly_api)
    server.Register("/unisound/", unisound_api)
    server.Start()
Beispiel #25
0
    parser.add_argument('--test', dest='test', action='store_true')
    parser.add_argument('--timezone', help="Set timezone of Sonos system", default='UTC')
    parser.add_argument("-v", "--verbose", help="Enable verbose logging", action="store_const", dest="loglevel", const=logging.INFO, default=logging.WARNING)
    parser.add_argument('-d', '--debug', help="Enable debug logging", action="store_const", dest="loglevel", const=logging.DEBUG)
    parser.add_argument('imap_host', metavar='IMAP_HOST', type=str, help='')
    parser.add_argument('imap_user', metavar='IMAP_USER', type=str, help='')
    parser.add_argument('imap_pass', metavar='IMAP_PASS', type=str, help='')
    parser.add_argument('imap_label', metavar='IMAP_LABEL', type=str, help='')

    args = parser.parse_args()

    logging.basicConfig(level=args.loglevel, format='%(relativeCreated)6d - %(threadName)-17s - %(name)s - %(levelname)s - %(message)s')

    sonos_alarm = SonosAlarm(args.port, args.timezone)

    http_server = HttpServer(args.port)
    mail_fetcher = MailFetcher(args.imap_host, args.imap_user, args.imap_pass, args.imap_label)
    mail_fetcher.add_new_mail_observer(sonos_alarm.alarm)

    logging.info("Starting server on port %s" % args.port)

    http_server_thread = threading.Thread(name="HTTPServerThread", target=http_server.serve_forever)
    http_server_thread.daemon = True
    http_server_thread.start()

    mail_fetcher_thread = threading.Thread(name="MailFetcherThread", target=mail_fetcher.fetch_forever)
    mail_fetcher_thread.daemon = True
    mail_fetcher_thread.start()

    if args.test:
        sonos_alarm.alarm()
Beispiel #26
0
def main():
    init_logging('metric_collector.log')
    options = vars(get_options())

    spectator = spectator_client.SpectatorClient(options)
    try:
        stackdriver = StackdriverMetricsService.make_service(options)

    except IOError as ioerror:
        logging.error(
            'Could not create stackdriver client'
            ' -- Stackdriver will be unavailable\n%s', ioerror)
        stackdriver = None

    registry = []
    registry.extend([
        CommandDefinition(handlers.BaseHandler(options, registry), '/', 'Home',
                          CommandRequest(options=options),
                          'Home page for Spinnaker metric administration.'),
        CommandDefinition(
            stackdriver_handlers.ClearCustomDescriptorsHandler(
                options,
                stackdriver), '/stackdriver/clear_descriptors', 'clear',
            CommandRequest(options=options),
            'Clear all the Stackdriver Custom Metrics'),
        CommandDefinition(
            stackdriver_handlers.ListCustomDescriptorsHandler(
                options, stackdriver), '/stackdriver/list_descriptors', 'list',
            CommandRequest(content_type='application/json', options=options),
            'Get the JSON of all the Stackdriver Custom Metric Descriptors.'),
        CommandDefinition(
            stackdriver_handlers.UpsertCustomDescriptorsHandler(
                options, stackdriver), None, 'upsert_descriptors',
            CommandRequest(options=options),
            'Given a file of Stackdriver Custom Metric Desciptors,'
            ' update the existing ones and add the new ones.'
            ' WARNING: Historic time-series data may be lost on update.'),
        CommandDefinition(
            handlers.DumpMetricsHandler(options, spectator), '/dump', 'dump',
            CommandRequest(options=options),
            'Show current raw metric JSON from all the servers.'),
        CommandDefinition(
            handlers.ExploreCustomDescriptorsHandler(options, spectator),
            '/explore',
            'explore',
            CommandRequest(options=options),
            'Explore metric type usage across Spinnaker microservices.',
        ),
        CommandDefinition(
            handlers.ShowCurrentMetricsHandler(options,
                                               spectator), '/show', 'show',
            CommandRequest(options=options),
            'Show current metric JSON for all Spinnaker.'),
    ])

    if options.get('command', None):
        command_processor.process_command(options['command'], registry)
        return

    if options.get('monitor', None):
        logging.info('Starting Monitor every %d s', options['period'])

        metric_service = stackdriver

        monitor = Monitor(spectator, metric_service, options)
        threading.Thread(target=monitor, name='monitor').start()

    logging.info('Starting HTTP server on port %d', options['port'])
    url_path_to_handler = {entry.url_path: entry.handler for entry in registry}
    httpd = HttpServer(options['port'], url_path_to_handler)
    httpd.serve_forever()
    sys.exit(-1)
Beispiel #27
0
import sys
from http_server import HttpServer

HOST = '0.0.0.0'

if len(sys.argv) > 1:
    PORT = int(sys.argv[1])
else:
    PORT = 8000

app = HttpServer(host=HOST, port=PORT)

if __name__ == "__main__":
    app.run()
    type_, auth = headers['ignored']['Authorization'].split(" ", 1)
    if type_ != "Basic":
        with open("www/error/501.html") as error:
            return ({
                "Status-code": 501,
                "Reason-phrase": "Not Implemented"
            }, error.read())
    uname, pword = base64.b64decode(auth.encode()).split(b":")
    if uname == pword:
        with open("www/index.html") as index:
            return ({}, index.read())
    else:
        with open("www/error/403.html") as error:
            return ({
                "Status-code": 403,
                "Reason-phrase": "Forbidden",
            }, error.read())


if __name__ != "__main__":
    raise SystemExit

http_server = HttpServer("/home/dev/python/webserver/www",
                         "www/error",
                         port=randint(49152, 65535),
                         logger_folder="logs")  # , logger_file=sys.stdout)
print(f"hosting HTTP server on {http_server.host}:{http_server.port}")
http_server.add_route("/", {"GET": index})
http_server.add_route("/index", {"GET": index})
http_server.handle_http_requests()
def main():
  init_logging('metric_collector.log')
  options = get_options()

  spectator = spectator_client.SpectatorClient(options)
  try:
    stackdriver = stackdriver_client.StackdriverClient.make_client(options)
  except IOError as ioerror:
    logging.error('Could not create stackdriver client'
                  ' -- Stackdriver will be unavailable\n%s',
                  ioerror)
    stackdriver = None

  registry = []
  registry.extend([
      CommandDefinition(
          handlers.BaseHandler(options, registry),
          '/',
          'Home',
          CommandRequest(options=options),
          'Home page for Spinnaker metric administration.'),
      CommandDefinition(
          stackdriver_handlers.ClearCustomDescriptorsHandler(
              options, stackdriver),
          '/stackdriver/clear_descriptors',
          'clear',
          CommandRequest(options=options),
          'Clear all the Stackdriver Custom Metrics'),
      CommandDefinition(
          stackdriver_handlers.ListCustomDescriptorsHandler(
              options, stackdriver),
          '/stackdriver/list_descriptors',
          'list',
          CommandRequest(content_type='application/json', options=options),
          'Get the JSON of all the Stackdriver Custom Metric Descriptors.'
          ),

      CommandDefinition(
          handlers.DumpMetricsHandler(options, spectator),
          '/dump',
          'dump',
          CommandRequest(options=options),
          'Show current raw metric JSON from all the servers.'),
      CommandDefinition(
          handlers.ExploreCustomDescriptorsHandler(options, spectator),
          '/explore',
          'explore',
          CommandRequest(options=options),
          'Explore metric type usage across Spinnaker microservices.',
          ),
      CommandDefinition(
          handlers.ShowCurrentMetricsHandler(options, spectator),
          '/show',
          'show',
          CommandRequest(options=options),
          'Show current metric JSON for all Spinnaker.'
          ),
      ])

  if options.command:
    command_processor.process_command(options.command, registry)
    return

  if options.monitor:
    logging.info('Starting Monitor every %d s', options.period)

    # TODO: Replace this with a real service.
    metric_service = DummyMetricService()

    monitor = Monitor(spectator, metric_service, options)
    threading.Thread(target=monitor, name='monitor').start()

  logging.info('Starting HTTP server on port %d', options.port)
  url_path_to_handler = {entry.url_path: entry.handler for entry in registry}
  httpd = HttpServer(options.port, url_path_to_handler)
  httpd.serve_forever()
  sys.exit(-1)
Beispiel #30
0
    def test_get_content_not_found(self):
        path = "/foo/bar/baz/doesnt/exist"

        with self.assertRaises(FileNotFoundError):
            HttpServer.get_content(path)
 def _create_server(self, host, port, fork):
     if fork:
         return HttpForkServer(host=host, port=port)
     else:
         return HttpServer(host=host, port=port)
Beispiel #32
0
    def test_get_path(self):
        path = "/foo"
        request_head = "GET {} HTTP/1.1".format(path)

        self.assertEqual(path, HttpServer.get_path(request_head))