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 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)
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)
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 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()
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()
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)
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'])
def _create_server(self, host, port, fork): if fork: return HttpForkServer(host=host, port=port) else: return HttpServer(host=host, port=port)
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)
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)
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()
# 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
#!/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()
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)
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()