def initialize(self): self.router = Router() self.redis_bank = RedisBank() self.url_cacher = UrlCacher(self.redis_bank) self.atm_settings = atm_settings self.timeout_rules = {} self.init_routes() self.init_timeouts()
def main(): # Obtenemos gateway gateway = get_gateway() nodos = [gateway] # Empezamos con el gateway visited_nodes = [] connections = {} ip_to_hostname = {} t = time.time() while nodos: print("Nodo actual: ", nodos[0]) router = Router(nodos[0], USERNAME, PASSWORD) # VALIDACION if not router.is_connected: print("Error al iniciar SSH en el nodo: ", nodos[0]) nodos.pop(0) # ES POSIBLEMENTE UNA PC continue # CREAMOS NUEVO USUARIO SSH router.create_user("pirata", "pirata") # OBTENEMOS A DONDE IR visited_nodes, nodos = router.get_jump_connections( visited_nodes, nodos) # IP A HOSTNAME ip_to_hostname.update(router.interfaces_ip) #CONEXIONES QUE TIENE EL ROUTER -> IP connections[router.hostname] = router.connections print("Tiempo de ejecución: ", time.time() - t) topologia = Graph("Mi topología") for nodo in connections: topologia.node(nodo) for ip in connections[nodo]: try: topologia.edge(nodo, ip_to_hostname[ip]) except: pass topologia.render("topologia")
#!/usr/bin/env python import sys from wsgiref.simple_server import make_server from utils import Router from config import ROUTES, PORT if __name__ == '__main__': router = Router() for route in ROUTES: router.add(*route) server = make_server('', PORT, router) try: print "Serving on :%s" % (PORT) server.serve_forever() except KeyboardInterrupt: print "Shutting down" sys.exit(1)
from aws_lambda_powertools import Logger import logging from utils import Invocation from utils import Router from aws_lambda_powertools.utilities.typing import LambdaContext log: Logger = Logger() Logger("botocore").setLevel(logging.INFO) Logger("urllib3").setLevel(logging.INFO) router: Router = Router() # Handler @log.inject_lambda_context() def handler(event: dict, context: LambdaContext) -> dict: return Invocation(router, event).call() # # Query Actions # @router.direct("list_students") # Resolves for a direct invoke @router.rest("GET", "/students") # Resolves for a ReST endpoint @router.graphql("Query", "listStudents") # Resolves for graphQL endpoint def list_students(args: dict) -> list: return list(students.values()) @router.direct("get_student") # Resolves for a direct invoke @router.rest("GET", "/students/{studentId}") # Resolves for a ReST endpoint
#-*- coding: utf-8 -*- import os import sys import json from flask import Flask, request, jsonify, abort from database import DataBase from utils import Router app = Flask(__name__) app.config['JSON_AS_ASCII'] = False app.config["JSON_SORT_KEYS"] = False db = DataBase() TOKEN = os.environ["WEB_SERVER_TOKEN"] router = Router() # 最新情報の取得API @app.route("/request/now", methods=["POST"]) def request_now(): global router data = request.get_json() if type(data) != dict: data = json.loads(data) major = data["major"] try: res = router.now(major) return jsonify({ "status":"200", "response":res })
class UrlCacheHandler(tornado.web.RequestHandler): """ Default handler """ def init_routes(self): """ Putting routes into router """ # deal with default rules default_target = self.atm_settings.DEFAULT_PROXIED_HOST for url in self.atm_settings.PROXIED_URLS: self.router.add_route(url, default_target) # deal with special rules for path, host in self.atm_settings.SPECIAL_ROUTES.items(): self.router.add_route(path, host) def init_timeouts(self): self.timeout_rules = self.atm_settings.SPECIAL_TIMEOUTS def initialize(self): self.router = Router() self.redis_bank = RedisBank() self.url_cacher = UrlCacher(self.redis_bank) self.atm_settings = atm_settings self.timeout_rules = {} self.init_routes() self.init_timeouts() @tornado.web.asynchronous @tornado.gen.engine def get(self): c = tornadoredis.Client(connection_pool=CONNECTION_POOL) request = self.request url_path = request.path logger = logging.getLogger('tornado.application') logger.info(url_path) if self.router.exist_route(url_path): # determine timeout if url_path in self.timeout_rules: timeout = self.timeout_rules[url_path] else: timeout = self.atm_settings.DEFAULT_TIMEOUT query = request.query host = self.router.get_route(url_path) logger.info(host) query_url_path = "%s?%s" % (url_path, query) if query else url_path logger.info("Request: %s" % query_url_path) exists = yield tornado.gen.Task(c.exists, query_url_path) if exists: print 'exists' response = yield tornado.gen.Task(c.get, query_url_path) yield tornado.gen.Task(c.disconnect) self.write(message_decode(response)) self.finish() else: print 'not exsits' content_url = "%s%s" % (host, query_url_path) content_to_cache = send_message(content_url) content_to_cache = message_encode(content_to_cache) #cache_result = yield tornado.gen.Task(c.setex, query_url_path, content_to_cache, 3600) print query_url_path, len(content_to_cache) cache_result = yield tornado.gen.Task(c.setex, query_url_path, 3600, content_to_cache) yield tornado.gen.Task(c.disconnect) self.write('') self.finish() @tornado.web.asynchronous def post(self): request = self.request url_path = request.path if self.router.exist_route(url_path): host = self.router.get_route(url_path) print host, url_path if self.request.body: print 'body: %s' % self.request.body content = self.request.body self.write(self.url_cacher.update(host, url_path, content)) else: self.write(self.url_cacher.update(host, url_path)) self.finish()