def __init__(self, uri, sites=None): with log.enter(obj=self) as tm: Site.__init__(self, None, timeout=60*60*30) self.uri = uri self.wsgi = None self.xmlrpc = None self._search_web_sites(sites)
def Service(options): consumer = QueueConsumer( consumer_queue=ns.MASTER_QUEUE, pool_size=ns.MAX_MESSAGES, spec_path=ns.SPEC_PATH, vhost=ns.RABBITMQ_VHOST, username=ns.RABBITMQ_USERNAME, password=ns.RABBITMQ_PASSWORD, host=ns.RABBITMQ_HOST, port=ns.RABBITMQ_PORT ) publisher = QueuePublisher( declare_strategy=1, spec_path=ns.SPEC_PATH, vhost=ns.RABBITMQ_VHOST, username=ns.RABBITMQ_USERNAME, password=ns.RABBITMQ_PASSWORD, host=ns.RABBITMQ_HOST, port=ns.RABBITMQ_PORT ) resource = MasterResource() resource.init(consumer, publisher) site = Site(resource) site.displayTracebacks = ns.DEBUG return internet.TCPServer(int(options['port']), site, \ interface=options['iface'])
def _testRender(self, uri, expectedURI): """ Check that a request pointing at C{uri} produce a new proxy connection, with the path of this request pointing at C{expectedURI}. """ root = Resource() reactor = MemoryReactor() resource = ReverseProxyResource("127.0.0.1", 1234, "/path", reactor) root.putChild("index", resource) site = Site(root) transport = StringTransportWithDisconnection() channel = site.buildProtocol(None) channel.makeConnection(transport) # Clear the timeout if the tests failed self.addCleanup(channel.connectionLost, None) channel.dataReceived("GET %s HTTP/1.1\r\nAccept: text/html\r\n\r\n" % (uri,)) # Check that one connection has been created, to the good host/port self.assertEquals(len(reactor.tcpClients), 1) self.assertEquals(reactor.tcpClients[0][0], "127.0.0.1") self.assertEquals(reactor.tcpClients[0][1], 1234) # Check the factory passed to the connect, and its given path factory = reactor.tcpClients[0][2] self.assertIsInstance(factory, ProxyClientFactory) self.assertEquals(factory.rest, expectedURI) self.assertEquals(factory.headers["host"], "127.0.0.1:1234")
def startup(): if not os.path.exists('data/firmware'): os.makedirs('data/firmware') if not os.path.exists('data/static'): os.makedirs('data/static') if not os.path.exists('data/cert'): os.makedirs('data/cert') # Check the certificate file host = getHost() validateCertHost('data/cert/key.pem', 'data/cert/cert.pem', 'data/static/thumb.txt', host) # Start up the HTTPS server web_port = 443 root_handler = File('./data/static/') firmware_handler = FirmwareHandler('data/firmware/') root_handler.putChild('firmware', firmware_handler) site = Site(root_handler) site.protocol = MyHttpChannel reactor.listenTCP(web_port, site) # Start up the HTTP server root_handler_http = File("./data/static/") config_handler = File("./config.html") root_handler_http.putChild('config.html', config_handler) site_http = Site(root_handler_http) reactor.listenTCP(8080, site_http) reactor.suggestThreadPoolSize(50) printStatus("Startup complete, running main loop...") # Run the main loop, this never returns: reactor.run()
def setup(args=None): """ This is the main setup function to establish the TCP listening logic for the API server. This code also takes into account development or unit test mode. """ # Setup API server api = ParadropAPIServer(reactor) api.putChild('internal', Base(apiinternal, allowNone=True)) site = Site(api, timeout=None) # Development mode if(args and args.development): thePort = settings.PDFCD_PORT + 10000 out.info('Using DEVELOPMENT variables') # Disable sending the error traceback to the client site.displayTracebacks = True elif(args and args.unittest): thePort = settings.PDFCD_PORT + 20000 out.info('Running under unittest mode') site.displayTracebacks = True else: thePort = settings.PDFCD_PORT site.displayTracebacks = False initializeSystem() # Setup the port we listen on reactor.listenTCP(thePort, site) # Never return from here reactor.run()
def startService(self): factory = WebSocketServerFactory("ws://localhost:%d" % self.port, debug=self.debug) factory.protocol = EchoServerProtocol factory.setProtocolOptions(allowHixie76=True) # needed if Hixie76 is to be supported # FIXME: Site.start/stopFactory should start/stop factories wrapped as Resources factory.startFactory() resource = WebSocketResource(factory) # we server static files under "/" .. webdir = os.path.abspath(pkg_resources.resource_filename("echows", "web")) root = File(webdir) # and our WebSocket server under "/ws" root.putChild("ws", resource) # both under one Twisted Web Site site = Site(root) site.protocol = HTTPChannelHixie76Aware # needed if Hixie76 is to be supported self.site = site self.factory = factory self.listener = reactor.listenTCP(self.port, site)
def whenRunning(cls, config: Configuration) -> None: """ Called after the reactor has started. """ async def start() -> None: await config.store.upgradeSchema() await config.store.validate() d = ensureDeferred(start()) host = config.HostName port = config.Port application = Application(config=config) cls.log.info( "Setting up web service at http://{host}:{port}/", host=host, port=port, ) patchCombinedLogFormatter() factory = Site(application.router.resource()) factory.sessionFactory = IMSSession from twisted.internet import reactor reactor.listenTCP(port, factory, interface=host) return d
def makeService(self, options): class LongSession(Session): sessionTimeout = 3600 if options['steamkey'] is None: raise ValueError('Must specify steam API key.') if options['strport'] is None: raise ValueError('Must specify strport description.') if options['dbdir'] is None: raise ValueError('Must specify database path.') steamAPI = API(key=options['steamkey']) store = Store(options['dbdir']) keyPath = FilePath(options['dbdir']).child('fernet.key') database = Database(store) loginRedirect = '/' portal = Portal(MapListRealm(database, options['bundle-path'], steamAPI, loginRedirect)) portal.registerChecker(PreauthenticatedChecker()) portal.registerChecker(AllowAnonymousAccess()) root = HTTPOpenIDAuthSessionWrapper(portal, [], loginRedirect, keyPath, database) site = Site(root) site.sessionFactory = LongSession return strports.service(options['strport'], site)
def start(self): self.loadSites() ip = Utils.getIP() #define phishing sites for key in self.websites: self.phishingsites[key] = PhishingSite(self.config, key, self.websites[key]['path'], self.logpath, self.websites[key]['logfile'], self.websites[key]['redirecturl']).getResource() site_length = 0 for key in self.phishingsites: if (len(key) > site_length): site_length = len(key) # if we are doing port based print for key in self.phishingsites: for port in range(self.MINPORT, self.MAXPORT): try: site = Site(self.phishingsites[key], logPath=self.logpath + "/" + self.websites[key]['logfile']+".access") site.logRequest = True reactor.listenTCP(port, site) print "Started website [%s] on [http://%s:%s]" % (('{:<%i}' % (site_length)).format(key), ip, port) self.websites[key]['port'] = port break except twisted.internet.error.CannotListenError, ex: continue
def run_dev_server(): global sender global ws_handler app.secret_key = "SECRET" app.debug = True ## create a Twisted Web resource for our WebSocket server wsFactory = BroadcastServerFactory(ws_url, debug=True, debugCodePaths=True) wsFactory.protocol = EchoServerProtocol wsFactory.setProtocolOptions(allowHixie76=True) wsResource = WebSocketResource(wsFactory) ## create a Twisted Web WSGI resource for our Flask server wsgiResource = WSGIResource(reactor, reactor.getThreadPool(), app) ## create a root resource serving everything via WSGI/Flask, but ## the path "/ws" served by our WebSocket stuff rootResource = WSGIRootResource(wsgiResource, {'ws': wsResource}) ## create a Twisted Web Site and run everything site = Site(rootResource) site.protocol = HTTPChannelHixie76Aware sender = MessageSender(wsFactory) wsFactory.sender = sender ws_handler = wsFactory reactor.listenTCP(port, site) reactor.run()
class WebServer(TCPApplication, _ConsumerMixin): """ Simple TCPApplication that act as a simple HTTP server """ disconnecting = False # Required by twisted connected = True disconnected = False def __init__(self): TCPApplication.__init__(self) _ConsumerMixin.__init__(self) self.app = Site(static.File(os.path.abspath("./stegano/sitetest"))).buildProtocol( "test" ) # Serve the given folder self.app.transport = ( self ) # Because we define self as transport we have to implement function normally called by twisted for a transport class def packet_received( self, packet, **kwargs ): # Overwrite TCPApplication packet_received to call the dataReceived of twisted self.lastclient = kwargs["id"] try: print ("---- Request received ----\n" + packet + "\n------------") self.app.dataReceived( packet ) # TODO: A chaque fois qu'on fait un read write s'assurer que la connection existe toujours etc.. except Exception, e: print ("Something is wrong in the request:" + str(e))
def _create_web_factory(self, config): options = config.get('options', {}) # create Twisted Web root resource if '/' in config['paths']: root_config = config['paths']['/'] root = self._create_resource(root_config, nested=False) else: root = Resource404(self._templates, b'') # create Twisted Web resources on all non-root paths configured self._add_paths(root, config.get('paths', {})) # create the actual transport factory transport_factory = Site(root) transport_factory.noisy = False # Web access logging if not options.get('access_log', False): transport_factory.log = lambda _: None # Traceback rendering transport_factory.displayTracebacks = options.get('display_tracebacks', False) # HSTS if options.get('hsts', False): if 'tls' in config['endpoint']: hsts_max_age = int(options.get('hsts_max_age', 31536000)) transport_factory.requestFactory = createHSTSRequestFactory(transport_factory.requestFactory, hsts_max_age) else: self.log.warn("Warning: HSTS requested, but running on non-TLS - skipping HSTS") return transport_factory
def __init__(self, logger_name, config, resource, *args, **kwargs): Site.__init__(self, resource, *args, **kwargs) if config.captcha_ip_origin_is_x_forwarded: self._log_formatter = proxiedLogFormatter else: self._log_formatter = combinedLogFormatter self.access_logger = logging.getLogger(logger_name)
def __init__(self, core): thread_pool = ThreadPool() thread_pool.start() reactor.addSystemEventTrigger("after", "shutdown", thread_pool.stop) application = get_flask_application(core) wsgi_resource = WSGIResource(reactor, thread_pool, application) Site.__init__(self, wsgi_resource)
def setUp(): if len(sys.argv) > 1 and sys.argv[1] == 'debug': log.startLogging(sys.stdout) debug = True else: debug = False try: import autobahn import twisted except ImportError: sys.exit("Install all dependencies") from orm.database_init import init_db, db_session from apps.web import constants init_db() from pdb import set_trace set_trace() from apps.web import main_resource root = main_resource.Root() reactor.listenTCP(8000, server.Site(main_resource.Root())) factory = BroadcastServerFactory("ws://127.0.0.1:8011", debug=debug, debugCodePaths=debug) factory.protocol = BroadcastServerProtocol factory.setProtocolOptions(allowHixie76=True) ws_resource = WebSocketResource(factory) root.putChild("ws", ws_resource) site = Site(root) site.protocol = HTTPChannelHixie76Aware listenWS(factory) reactor.run()
def setUp(): if len(sys.argv) > 1 and sys.argv[1] == 'debug': log.startLogging(sys.stdout) debug = True else: debug = False try: import autobahn import twisted except ImportError: sys.exit("Install all dependencies") root = Resource() # root.putChild(constants.WEB_DYNAMIC_BRANCH, resource) from autobahn.twisted.resource import WebSocketResource, HTTPChannelHixie76Aware from twisted.web.server import Site factory = BroadcastServerFactory("ws://127.0.0.1:8888", debug=debug, debugCodePaths=debug) #если используется proxy #factory.proxy={'host': '192.168.200.105', 'port': '8088'} factory.protocol = BroadcastServerProtocol factory.setProtocolOptions(allowHixie76=True) ws_resource = WebSocketResource(factory) root.putChild("ws", ws_resource) site = Site(root) site.protocol = HTTPChannelHixie76Aware listenWS(factory) reactor.run()
def __init__(self): root = Resource() ## downloads server -- client grabs patch info from here root.putChild('u', VirtualFile('webRoot/downloads/u')) ## MOST OF THE BELOW DONT WORK SO ARE COMMENTED OUT ## redalert3pc.sake.gamespy.com sakeStorageServer = Resource() sakeStorageServer.putChild('StorageServer.asmx', StorageServer()) #root.putChild('SakeStorageServer', sakeStorageServer) ## redalert3pc.auth.pubsvs.gamespy.com -- used to auth before reporting results authService = Resource() authService.putChild('AuthService.asmx', AuthService()) #root.putChild('AuthService', authService) ## redalert3pc.comp.pubsvs.gamespy.com -- used to report match results compSvc = Resource() compSvc.putChild('competitionservice.asmx', CompetitionService()) #compSvc.putChild('CompetitionService.asmx', CompetitionService()) root.putChild('competitionservice', compSvc) #root.putChild('CompetitionService', compSvc) ## TODO: psweb.gamespy.com -- SOAP service that serves Clan-related requests ## TODO: redalert3services.gamespy.com -- HTTP GET requests that serve rank icons ## /GetPlayerRankIcon.aspx?gp=fgErop[sap9faZeJJELRac__&pid=<pid of player> retrieves that player's rank icon ## /GetPlayerLadderRatings.aspx?gp=fgErop[sap9faZeJJELRac__ retrieves CSV of ladder ratings Site.__init__(self, root)
def run(port, sqlite_conn, index_html, static_root): #-------------------------------------------------------------------------- # log to stdout #-------------------------------------------------------------------------- from twisted.python import log import sys log.startLogging(sys.stdout) #-------------------------------------------------------------------------- # database #-------------------------------------------------------------------------- from twisted.enterprise import adbapi pool = adbapi.ConnectionPool('sqlite3', sqlite_conn, check_same_thread=False, cp_min=1, cp_max=1) data_model = DataModel(pool) #-------------------------------------------------------------------------- # url/resource mapping #-------------------------------------------------------------------------- root = Resource() root.putChild('', File(index_html)) root.putChild('static', File(static_root)) root.putChild('todos', Todos(data_model)) #-------------------------------------------------------------------------- # serve it #-------------------------------------------------------------------------- from twisted.internet import reactor from twisted.web.server import Site site = Site(root) site.displayTracebacks = False reactor.listenTCP(port, site) reactor.run()
def webServer(self): from twisted.web.server import Site from twisted.web import demo, static self.app = Site(demo.Test()).buildProtocol("lala") self.app = Site(static.File(os.path.abspath("."))).buildProtocol("lala") self.app.transport = self
def start(self): """ start websocket server """ logger.info('start websocket server at %s', self._url) self._factory = MyWebSocketServerFactory( self._url, debug=self._debug ) self._factory.protocol = MyWebSocketServerProtocol self._factory.setProtocolOptions(allowHixie76=True) self._resource = WebSocketResource(self._factory) # we server static files under "/" .. root = File('.') # and our WebSocket server under "/websocket" root.putChild('websocket', self._resource) # both under one Twisted Web Site site = Site(root) site.protocol = HTTPChannelHixie76Aware reactor.listenTCP(self._port, site) self._thread = threading.Thread(target=reactor.run, args=(False,)) self._thread.start()
def make_site(resource, site_logs_path): site = Site( resource, logPath=site_logs_path.path, logFormatter=_LogFormatter(datetime.utcnow).json_access_log, ) site.displayTracebacks = False return site
def __init__(self, logger_name, site_tag, config, resource, *args, **kwargs): Site.__init__(self, resource, *args, **kwargs) self.site_tag = site_tag proxied = config.get("x_forwarded", False) self.requestFactory = SynapseRequestFactory(self, proxied) self.access_logger = logging.getLogger(logger_name)
def run_twisted_server(ip, port): rootResource = WSGIRootResource(createDjangoEndPoint(),{}) site = Site(rootResource) site.protocol = HTTPChannelHixie76Aware from twisted.python import log as log_twisted log_twisted.startLogging(sys.stdout) reactor.listenTCP(port, site, interface=ip) reactor.run()
def __init__(self, *args, **kw): self.active_clients = 0 self.active_requests = set() self._max_requests = kw['max_requests'] del kw['max_requests'] self._request_count = 0 self._stopping_deferred = None Site.__init__(self, *args, **kw)
def site(self, displayTracebacks=True, **kwargs): """ A :twisted:`web.server.Site` that will serve me. """ site = Site(self.app.resource(), **kwargs) site.displayTracebacks = displayTracebacks return site
def get_web_service(broadcast_svc): root = RootResource(broadcast_svc) root.putChild("job", JobResource(broadcast_svc)) factory = Site(root) factory.amqp = broadcast_svc # noinspection PyUnresolvedReferences webserver = internet.TCPServer(8000, factory) return webserver
def get_site(resource, logging=False): """ :param resource: A :class:`twisted.web.resource.Resource` object. :return: a :class:`Site` that can be run """ site = Site(resource) site.displayTracebacks = False site.requestFactory = MimicLoggingRequest if logging else MimicRequest return site
def makeService(config): """ Set up the otter-api service. """ set_config_data(dict(config)) # Try to configure graylog and airbrake. if config_value('graylog'): if GraylogUDPPublisher is not None: log.addObserver( make_observer_chain( GraylogUDPPublisher(**config_value('graylog')), False)) else: warnings.warn("There is a configuration option for Graylog, but " "txgraylog is not installed.") if config_value('airbrake'): if AirbrakeLogObserver is not None: airbrake = AirbrakeLogObserver( config_value('airbrake.api_key'), config_value('environment'), use_ssl=True ) airbrake.start() else: warnings.warn("There is a configuration option for Airbrake, but " "txairbrake is not installed.") if not config_value('mock'): seed_endpoints = [ clientFromString(reactor, str(host)) for host in config_value('cassandra.seed_hosts')] cassandra_cluster = RoundRobinCassandraCluster( seed_endpoints, config_value('cassandra.keyspace')) set_store(CassScalingGroupCollection(cassandra_cluster)) s = MultiService() site = Site(root) site.displayTracebacks = False api_service = service(str(config_value('port')), site) api_service.setServiceParent(s) if config_value('scheduler'): scheduler_service = SchedulerService(int(config_value('scheduler.batchsize')), int(config_value('scheduler.interval')), cassandra_cluster) scheduler_service.setServiceParent(s) return s
def makeService(config): """ Set up the otter-api service. """ set_config_data(dict(config)) if not config_value('mock'): seed_endpoints = [ clientFromString(reactor, str(host)) for host in config_value('cassandra.seed_hosts')] cassandra_cluster = LoggingCQLClient(RoundRobinCassandraCluster( seed_endpoints, config_value('cassandra.keyspace')), log.bind(system='otter.silverberg')) set_store(CassScalingGroupCollection(cassandra_cluster)) bobby_url = config_value('bobby_url') if bobby_url is not None: set_bobby(BobbyClient(bobby_url)) cache_ttl = config_value('identity.cache_ttl') if cache_ttl is None: # FIXME: Pick an arbitrary cache ttl value based on absolutely no # science. cache_ttl = 300 authenticator = CachingAuthenticator( reactor, ImpersonatingAuthenticator( config_value('identity.username'), config_value('identity.password'), config_value('identity.url'), config_value('identity.admin_url')), cache_ttl) supervisor = Supervisor(authenticator.authenticate_tenant, coiterate) set_supervisor(supervisor) s = MultiService() site = Site(root) site.displayTracebacks = False api_service = service(str(config_value('port')), site) api_service.setServiceParent(s) if config_value('scheduler') and not config_value('mock'): scheduler_service = SchedulerService(int(config_value('scheduler.batchsize')), int(config_value('scheduler.interval')), cassandra_cluster) scheduler_service.setServiceParent(s) return s
def __init__(self, firewall): self.user = cattivo.config.get("cleaner", "user") self.passwd_file = cattivo.config.get("cleaner", "passwd-file") checker = FilePasswordDB(self.passwd_file) self.realm = CleanerRealm() self.portal = Portal(self.realm) self.portal.registerChecker(checker) self.firewall = firewall Site.__init__(self, CleanerResource(self.portal)) Loggable.__init__(self)
def main(args): register_stack_trace_dump() burninate_gc_collector() config_environ() configured_gpu_counter.set( get_gpu_count("/gpu-config/gpu-configuration.json")) # 0 means do not retire data, as missing metric will intrrupt prometheus's evaluation # of alerting rules decay_time = datetime.timedelta(seconds=0) # used to exchange gpu info between GpuCollector and ContainerCollector nvidia_info_ref = collector.AtomicRef(decay_time) # used to exchange docker stats info between ContainerCollector and ZombieCollector stats_info_ref = collector.AtomicRef(decay_time) # used to exchange zombie info between GpuCollector and ZombieCollector zombie_info_ref = collector.AtomicRef(decay_time) # used to exchange dcgm info between DCGMCollector and ContainerCollector dcgm_info_ref = collector.AtomicRef(decay_time) # used to exchange infiniband info between InfinibandCollector and # ContainerCollector infiniband_info_ref = collector.AtomicRef(decay_time) # used to exchange ipoib info between IPoIBCollector and ContainerCollector ipoib_info_ref = collector.AtomicRef(decay_time) interval = args.interval # Because all collector except container_collector will spent little time in calling # external command to get metrics, so they need to sleep 30s to align with prometheus # scrape interval. The 99th latency of container_collector loop is around 20s, so it # should only sleep 10s to adapt to scrape interval collector_args = [ ("docker_daemon_collector", interval, decay_time, collector.DockerCollector), ("gpu_collector", interval, decay_time, collector.GpuCollector, nvidia_info_ref, zombie_info_ref, args.threshold), ("container_collector", max(0, interval - 18), decay_time, collector.ContainerCollector, nvidia_info_ref, stats_info_ref, args.interface, dcgm_info_ref, infiniband_info_ref, ipoib_info_ref), ("zombie_collector", interval, decay_time, collector.ZombieCollector, stats_info_ref, zombie_info_ref), ("process_collector", interval, decay_time, collector.ProcessCollector), ("dcgm_collector", interval, decay_time, collector.DCGMCollector, dcgm_info_ref), ("nvsm_collector", 10, datetime.timedelta(seconds=1200), collector.NVSMCollector), ("infiniband_collector", interval, decay_time, collector.InfinibandCollector, infiniband_info_ref), ("ipoib_collector", interval, decay_time, collector.IPoIBCollector, ipoib_info_ref), ("nv_peer_mem_collector", interval, decay_time, collector.NvPeerMemCollector), ("lustre_collector", interval, decay_time, collector.LustreCollector), ] refs = list(map(lambda x: collector.make_collector(*x), collector_args)) REGISTRY.register(CustomCollector(refs)) root = Resource() root.putChild(b"metrics", MetricsResource()) root.putChild(b"healthz", HealthResource()) factory = Site(root) reactor.listenTCP(int(args.port), factory) reactor.run()
# Add WebSocket if config['enable_ws']: # Setup the Twisted server with Flask app log.startLogging(sys.stdout) # Set factory and begin listening to ws factory = BroadcastServerFactory(u"ws://0.0.0.0:" + str(config["ws_port"]) + "/network/local") factory.protocol = BroadcastServerProtocol listenWS(factory) wsResource = WebSocketResource(factory) # Create a Twisted WSGI resource for Flask app wsgiResource = WSGIResource(reactor, reactor.getThreadPool(), app) # Create a root resource serving everything via WSGI/Flask, but # the path "/ws" served by our WebSocket rootResource = WSGIRootResource(wsgiResource, {'ws': wsResource}) # create a Twisted Web Site and run everything site = Site(rootResource) reactor.listenTCP(config["http_port"], site) # Start the Twisted reactor # TODO: Allow for shutdown without ending/restarting python instance reactor.run() else: # Start the Flask app app.run(host='0.0.0.0', port=config["http_port"])
#!/usr/local/bin/python3 from twisted.internet import reactor from twisted.web.server import Site from twisted.web.static import File print(9000) r = File('.', ignoredExts=('.py')) factory = Site(r) reactor.listenTCP(9000, factory) reactor.run()
def MainApp(): Log("Init", "\033[37m").new_message( "Initializing Battlefield Heroes Master Server Emulator...", 0) try: ssl_key = readFromConfig("SSL", "priv_key_path") ssl_cert = readFromConfig("SSL", "cert_file_path") fesl_client_port = int(readFromConfig("connection", "fesl_client_port")) fesl_server_port = int(readFromConfig("connection", "fesl_server_port")) theater_client_port = int( readFromConfig("connection", "theater_client_port")) theater_server_port = int( readFromConfig("connection", "theater_server_port")) http_server_port = int(readFromConfig("connection", "http_server_port")) https_server_port = int( readFromConfig("connection", "https_server_port")) except: Log("Init", "\033[37;41m").new_message( "Fatal Error!\n" "Failed to load certain values in the config.ini, be sure that EVERY " "option has a valid value and try it again.") sys.exit(2) try: Database(True) except Exception as DatabaseError: Log("Database", "\033[37;1;41m").new_message( "Fatal Error! Cannot initialize database!\n\n" "Additional error info:\n" + str(DatabaseError), 0) sys.exit(3) try: SSLContext = ssl.DefaultOpenSSLContextFactory(ssl_key, ssl_cert) Log("Init", "\033[37m").new_message("Successfully created SSL Context!", 2) except Exception as SSLErr: Log("Init", "\033[37;41m").new_message( "Fatal Error!\n" "Failed to create SSL Context!\n" "Make sure that you installed all required modules using\n" "`pip install -r requirements.txt`\n" "Also check if you specified correct SSL Cert and/or key in " "`config.ini`\n " "Additional error info:\n" + str(SSLErr), 0) sys.exit(4) try: factory = Factory() factory.protocol = FeslClient.HANDLER reactor.listenSSL(fesl_client_port, factory, SSLContext) Log("FeslClient", "\033[33;1m").new_message( "Created TCP Socket (now listening on port " + str(fesl_client_port) + ")", 1) except Exception as BindError: Log("Init", "\033[33;1;41m").new_message( "Fatal Error! Cannot bind socket to port: " + str(fesl_client_port) + "\n" "Make sure that this port aren't used by another program!\n\n" "Additional error info:\n" + str(BindError), 0) sys.exit(5) try: factory = Factory() factory.protocol = FeslServer.HANDLER reactor.listenSSL(fesl_server_port, factory, SSLContext) Log("FeslServer", "\033[32;1m").new_message( "Created TCP Socket (now listening on port " + str(fesl_server_port) + ")", 1) except Exception as BindError: Log("Init", "\033[33;1;41m").new_message( "Fatal Error! Cannot bind socket to port: " + str(fesl_server_port) + "\n" "Make sure that this port aren't used by another program!\n\n" "Additional error info:\n" + str(BindError), 0) sys.exit(5) try: factoryTCP = Factory() factoryTCP.protocol = TheaterClient.TCPHandler reactor.listenTCP(theater_client_port, factoryTCP) Log("TheaterClient", "\033[35;1m").new_message( "Created TCP Socket (now listening on port " + str(theater_client_port) + ")", 1) reactor.listenUDP(theater_client_port, TheaterClient.UDPHandler()) Log("TheaterClient", "\033[35;1m").new_message( "Created UDP Socket (now listening on port " + str(theater_client_port) + ")", 1) except Exception as BindError: Log("Init", "\033[35;1;41m").new_message( "Fatal Error! Cannot bind socket to port: " + str(theater_client_port) + "\n" "Make sure that this port aren't used by another program!\n\n" "Additional error info:\n" + str(BindError), 0) sys.exit(5) try: factoryTCP = Factory() factoryTCP.protocol = TheaterServer.TCPHandler reactor.listenTCP(theater_server_port, factoryTCP) Log("TheaterServer", "\033[36;1m").new_message( "Created TCP Socket (now listening on port " + str(theater_server_port) + ")", 1) reactor.listenUDP(theater_server_port, TheaterServer.UDPHandler()) Log("TheaterServer", "\033[36;1m").new_message( "Created UDP Socket (now listening on port " + str(theater_server_port) + ")", 1) except Exception as BindError: Log("Init", "\033[35;1;41m").new_message( "Fatal Error! Cannot bind socket to port: " + str(theater_server_port) + "\n" "Make sure that this port aren't used by another program!\n\n" "Additional error info:\n" + str(BindError), 0) sys.exit(5) try: site = Site(WebServer.Handler()) reactor.listenTCP(http_server_port, site) Log("WebServer", "\033[36m").new_message( "Created TCP Socket (now listening on port " + str(http_server_port) + ")", 1) except Exception as BindError: Log("Init", "\033[35;1;41m").new_message( "Fatal Error! Cannot bind socket to port: " + str(http_server_port) + "\n" "Make sure that this port aren't used by another program!\n\n" "Additional error info:\n" + str(BindError), 0) sys.exit(5) try: site = Site(SecureWebServer.Handler()) reactor.listenSSL(https_server_port, site, SSLContext) Log("WebServer", "\033[36m").new_message( "Created TCP Socket (now listening on port " + str(https_server_port) + ")", 1) except Exception as BindError: Log("Init", "\033[35;1;41m").new_message( "Fatal Error! Cannot bind socket to port: " + str(https_server_port) + "\n" "Make sure that this port aren't used by another program!\n\n" "Additional error info:\n" + str(BindError), 0) sys.exit(5) Log("Init", "\033[37m").new_message( "Finished initialization! Ready for receiving incoming connections...", 0) reactor.run()
def __init__(self, *a, **kw): Site.__init__(self, *a, **kw) self.requestLengths = [] self._activeRequests = set()
else: action.clicked() else: response["success"] = False response["msg"] = "Invalid request" return json.dumps(response) class RemoteControlAPIServer(Resource): """ """ def __init__(self, context, workspace): self.context = context self.workspace = workspace def getChildWithDefault(self, path, request): if path == "application_state": return ApplicationStateInformation(self.context, self.workspace) elif path == "wizard_commands": return WizardCommands(self.context, self.workspace) elif path == "wizard_control": return WizardControl(self.context, self.workspace) else: return NoResource() if __name__ == '__main__': # pragma: no cover site = Site(RemoteKVSResource()) reactor.listenTCP(2048, site) reactor.run()
class DummyChannel: class TCP: port = 80 disconnected = False def __init__(self, peer=None): if peer is None: peer = IPv4Address("TCP", '192.168.1.1', 12344) self._peer = peer self.written = BytesIO() self.producers = [] def getPeer(self): return self._peer def write(self, data): if not isinstance(data, bytes): raise TypeError("Can only write bytes to a transport, not %r" % (data, )) self.written.write(data) def writeSequence(self, iovec): for data in iovec: self.write(data) def getHost(self): return IPv4Address("TCP", '10.0.0.1', self.port) def registerProducer(self, producer, streaming): self.producers.append((producer, streaming)) def unregisterProducer(self): pass def loseConnection(self): self.disconnected = True @implementer(ISSLTransport) class SSL(TCP): def abortConnection(self): # ITCPTransport.abortConnection pass def getTcpKeepAlive(self): # ITCPTransport.getTcpKeepAlive pass def getTcpNoDelay(self): # ITCPTransport.getTcpNoDelay pass def loseWriteConnection(self): # ITCPTransport.loseWriteConnection pass def setTcpKeepAlive(self, enabled): # ITCPTransport.setTcpKeepAlive pass def setTcpNoDelay(self, enabled): # ITCPTransport.setTcpNoDelay pass def getPeerCertificate(self): # ISSLTransport.getPeerCertificate pass site = Site(Resource()) def __init__(self, peer=None): self.transport = self.TCP(peer) def requestDone(self, request): pass def writeHeaders(self, version, code, reason, headers): response_line = version + b" " + code + b" " + reason + b"\r\n" headerSequence = [response_line] headerSequence.extend(name + b': ' + value + b"\r\n" for name, value in headers) headerSequence.append(b"\r\n") self.transport.writeSequence(headerSequence) def getPeer(self): return self.transport.getPeer() def getHost(self): return self.transport.getHost() def registerProducer(self, producer, streaming): self.transport.registerProducer(producer, streaming) def unregisterProducer(self): self.transport.unregisterProducer() def write(self, data): self.transport.write(data) def writeSequence(self, iovec): self.transport.writeSequence(iovec) def loseConnection(self): self.transport.loseConnection() def endRequest(self): pass def isSecure(self): return isinstance(self.transport, self.SSL) def abortConnection(self): # ITCPTransport.abortConnection pass def getTcpKeepAlive(self): # ITCPTransport.getTcpKeepAlive pass def getTcpNoDelay(self): # ITCPTransport.getTcpNoDelay pass def loseWriteConnection(self): # ITCPTransport.loseWriteConnection pass def setTcpKeepAlive(self): # ITCPTransport.setTcpKeepAlive pass def setTcpNoDelay(self): # ITCPTransport.setTcpNoDelay pass def getPeerCertificate(self): # ISSLTransport.getPeerCertificate pass
newurl = FLING_URL + '?' + data #req = urllib2.Request(FLING_URL, data) print "flinging url=%s" % newurl req = urllib2.Request(newurl) response = urllib2.urlopen(req).read() except Exception, e: print str(e) print "Creating FlingIcon" i = FlingIcon() i.show() if sys.platform == 'win32': for L in range(ord('A'), ord('Z') + 1): drive = chr(L) + ':\\' if (os.path.exists(drive)): drives.append(drive) else: drives.append('/') port_inc = 0 for root in drives: doc_root = File(root) site = Site(doc_root) reactor.listenTCP(PORT_BASE + port_inc, site) port_inc = port_inc + 1 if sys.platform == 'darwin': app.exec_() print "Starting reactor" reactor.run()
def setUpFileServer(self, port, path): resource = File(path) resource.putChild("err503", ResourceFailClass()) factory = Site(resource) self._logger.debug("Listen to port %s, factory %s", port, factory) self.file_server = reactor.listenTCP(port, factory)
sys.exit(1) baudrate = int(o.opts['baudrate']) port = int(o.opts['port']) webport = int(o.opts['webport']) wsurl = o.opts['wsurl'] ## start Twisted log system ## log.startLogging(sys.stdout) ## create Serial2Ws gateway factory ## wsMcuFactory = WsMcuFactory(wsurl) listenWS(wsMcuFactory) ## create serial port and serial port protocol ## log.msg('About to open serial port %d [%d baud] ..' % (port, baudrate)) serialPort = SerialPort(wsMcuFactory.mcuProtocol, port, reactor, baudrate = baudrate) ## create embedded web server for static files ## webdir = File(".") web = Site(webdir) reactor.listenTCP(webport, web) ## start Twisted reactor .. ## reactor.run()
def main(connection_string=DB_CONNECTION_STRING): # configure logging logging.basicConfig(level=logging.DEBUG) logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG) #logging.getLogger('sqlalchemy.engine.base.Engine').setLevel(logging.DEBUG) from spynepi.const import FILES_PATH RootService.FILES_ROOT = os.path.abspath(FILES_PATH) index_app = MyApplication([RootService, IndexService], "http://usefulinc.com/ns/doap#", in_protocol=HttpRpc(), out_protocol=HtmlTable()) rdf_app = MyApplication([RdfService], "http://usefulinc.com/ns/doap#", in_protocol=HttpRpc(), out_protocol=XmlDocument()) html_app = MyApplication([HtmlService], "http://usefulinc.com/ns/doap#", in_protocol=HttpRpc(), out_protocol=HttpRpc()) db_handle = init_database(connection_string) class UserDefinedContext(object): def __init__(self): self.session = db_handle.Session() def close(self): self.session.close() # this is called after validation def _on_method_call(ctx): ctx.udc = UserDefinedContext() # this is called once all data is sent to the client. def _on_method_return_object(ctx): ctx.udc.session.commit() def _on_wsgi_close(ctx): if ctx.udc is not None: ctx.udc.close() for app in index_app, rdf_app, html_app: app.event_manager.add_listener('method_call', _on_method_call) app.event_manager.add_listener('method_return_object', _on_method_return_object) wsgi_index = WsgiApplication(index_app) wsgi_rdf = WsgiApplication(rdf_app) wsgi_html = WsgiApplication(html_app) for a in wsgi_index, wsgi_rdf, wsgi_html: a.event_manager.add_listener('wsgi_close', _on_wsgi_close) url_map = Map([ Rule("/", endpoint=wsgi_index), Rule("/<project_name>", endpoint=wsgi_html), Rule("/<project_name>/", endpoint=wsgi_html), Rule("/<project_name>/doap.rdf", endpoint=wsgi_rdf), Rule("/<project_name>/<version>", endpoint=wsgi_html), Rule("/<project_name>/<version>/", endpoint=wsgi_html), Rule("/<project_name>/<version>/doap.rdf", endpoint=wsgi_rdf), Rule("/files/<project_name>/<version>/<download>", endpoint=wsgi_html), ]) resource = WSGIResource(reactor, reactor, TWsgiApplication(url_map)) site = Site(resource) reactor.listenTCP(PORT, site) logging.info('listening on: %s:%d' % (HOST, PORT)) logging.info('wsdl is at: http://%s:%d/?wsdl' % (HOST, PORT)) sys.exit(reactor.run())
def setUp(self): r = TestAPI() site = Site(r, timeout=None) self.port = self._listen(site) self.portno = self.port.getHost().port
#https://arachne.readthedocs.io/en/latest/ #Arachne provides a wrapper around your scrapy Spider object to run them through a flask app. # All you have to do is customize SPIDER_SETTINGS in the settings file. #pip install Arachne # settings.py file SPIDER_SETTINGS = [{ 'endpoint': 'dmoz', 'location': 'spiders.DmozSpider', 'spider': 'DmozSpider' }] #It looks very similar to a flask app but since Scrapy depends on the python twisted package, we need to run our flask app with twisted: from twisted.web.wsgi import WSGIResource from twisted.web.server import Site from twisted.internet import reactor from arachne import Arachne app = Arachne(__name__) resource = WSGIResource(reactor, reactor.getThreadPool(), app) site = Site(resource) reactor.listenTCP(8080, site) if __name__ == '__main__': reactor.run()
def _start(): reactor.listenTCP(port, Site(self.app.resource), interface=host) reactor.run()
def initial(config: Dict, callback): site = Site(OSMProtocol(callback)) endpoint = TCP4ServerEndpoint(reactor, config.get('port', 8080)) endpoint.listen(site)
transport_factory = WampWebSocketServerFactory(session_factory, args.wsurl, debug=False, debug_wamp=args.debug) transport_factory.setProtocolOptions(failByDrop=False) from twisted.web.server import Site from twisted.web.static import File from autobahn.twisted.resource import WebSocketResource # we serve static files under "/" .. root = File(".") # .. and our WebSocket server under "/ws" resource = WebSocketResource(transport_factory) root.putChild("ws", resource) # run both under one Twisted Web Site site = Site(root) site.noisy = False site.log = lambda _: None # start the WebSocket server from an endpoint ## server = serverFromString(reactor, args.websocket) server.listen(site) # now enter the Twisted reactor loop ## reactor.run()
# yield {'path': p} freezer.freeze() if options.debug: import sys, os from twisted.python import log log.startLogging(sys.stdout) from twisted.internet import reactor from twisted.web.server import Site from twisted.web.static import File resource = File(os.path.join(os.path.dirname(__file__), 'build')) resource.contentTypes.update(EXTRA_MIME_TYPES) site = Site(resource) reactor.listenTCP(int(options.port), site) reactor.run() else: if options.socketserver: print "Running Flask under standard Python SocketServer" app.run(host="0.0.0.0", port=int(options.port), debug=options.debug) else: print "Running Flask under Twisted server" import sys from twisted.python import log from twisted.internet import reactor from twisted.web.server import Site
def setup_finalize(event, application): from twisted.application import service, internet from twisted.internet import reactor, ssl from twisted.web.server import Site from twisted.python import log #from twisted.enterprise import adbapi import OpenSSL.SSL from services import ServiceEventHandler import socket_transport import http_transport #import websocket_transport import irc from stratum import settings try: import signature signing_key = signature.load_privkey_pem(settings.SIGNING_KEY) except: print "Loading of signing key '%s' failed, protocol messages cannot be signed." % settings.SIGNING_KEY signing_key = None # Attach HTTPS Poll Transport service to application try: sslContext = ssl.DefaultOpenSSLContextFactory(settings.SSL_PRIVKEY, settings.SSL_CACERT) except OpenSSL.SSL.Error: sslContext = None print "Cannot initiate SSL context, are SSL_PRIVKEY or SSL_CACERT missing?" print "This will skip all SSL-based transports." # Set up thread pool size for service threads reactor.suggestThreadPoolSize(settings.THREAD_POOL_SIZE) if settings.LISTEN_SOCKET_TRANSPORT: if settings.LISTEN_SOCKET_ADDRESS: # Attach Socket Transport service to application socket = internet.TCPServer(settings.LISTEN_SOCKET_TRANSPORT, socket_transport.SocketTransportFactory(debug=settings.DEBUG, signing_key=signing_key, signing_id=settings.SIGNING_ID, event_handler=ServiceEventHandler, tcp_proxy_protocol_enable=settings.TCP_PROXY_PROTOCOL), interface=settings.LISTEN_SOCKET_ADDRESS) else: # Attach Socket Transport service to application socket = internet.TCPServer(settings.LISTEN_SOCKET_TRANSPORT, socket_transport.SocketTransportFactory(debug=settings.DEBUG, signing_key=signing_key, signing_id=settings.SIGNING_ID, event_handler=ServiceEventHandler, tcp_proxy_protocol_enable=settings.TCP_PROXY_PROTOCOL)) socket.setServiceParent(application) # Build the HTTP interface httpsite = Site(http_transport.Root(debug=settings.DEBUG, signing_key=signing_key, signing_id=settings.SIGNING_ID, event_handler=ServiceEventHandler)) httpsite.sessionFactory = http_transport.HttpSession if settings.LISTEN_HTTP_TRANSPORT: # Attach HTTP Poll Transport service to application http = internet.TCPServer(settings.LISTEN_HTTP_TRANSPORT, httpsite) http.setServiceParent(application) if settings.LISTEN_HTTPS_TRANSPORT and sslContext: https = internet.SSLServer(settings.LISTEN_HTTPS_TRANSPORT, httpsite, contextFactory = sslContext) https.setServiceParent(application) #if settings.LISTEN_WS_TRANSPORT: # from autobahn.websocket import listenWS # log.msg("Starting WS transport on %d" % settings.LISTEN_WS_TRANSPORT) # ws = websocket_transport.WebsocketTransportFactory(settings.LISTEN_WS_TRANSPORT, # debug=settings.DEBUG, # signing_key=signing_key, # signing_id=settings.SIGNING_ID, # event_handler=ServiceEventHandler) # listenWS(ws) #if settings.LISTEN_WSS_TRANSPORT and sslContext: # from autobahn.websocket import listenWS # log.msg("Starting WSS transport on %d" % settings.LISTEN_WSS_TRANSPORT) # wss = websocket_transport.WebsocketTransportFactory(settings.LISTEN_WSS_TRANSPORT, is_secure=True, # debug=settings.DEBUG, # signing_key=signing_key, # signing_id=settings.SIGNING_ID, # event_handler=ServiceEventHandler) # listenWS(wss, contextFactory=sslContext) if settings.IRC_NICK: reactor.connectTCP(settings.IRC_SERVER, settings.IRC_PORT, irc.IrcLurkerFactory(settings.IRC_ROOM, settings.IRC_NICK, settings.IRC_HOSTNAME)) return event
#request.setHeader("Content-Type", "text/plain; charset=utf-8") #return u"<html><body>Prueba</body></html>".encode('utf-8') if __name__ == '__main__': log.startLogging(sys.stdout) logging.debug(sys.argv) logging.debug('----- PiTooth -----\n') sstate = "develop" if len(sys.argv) > 1: sstate = sys.argv[1] if sys.argv[1] in ["develop", "production" ] else "develop" logging.debug(sstate) logger = Logger(logging_delay=l_v.logging_delay, file_name=l_v.file_name, sensor_pin=l_v.sensor_pin, state=sstate) controller = Controller(intensity_threshold=b_v.intensity_threshold, connection_attempts=b_v.connection_attempts, checker_delay=b_v.checker_delay) logging.debug('Slave Node RUKA') logging.debug("Using Twisted reactor {0}".format(reactor.__class__)) web = 8000 root = Views() reactor.listenTCP(web, Site(root)) reactor.run()
def TestHttpServer(port): return reactor.listenTCP(port, Site(TestPage()))
# Email : [email protected] # Last modified : 2014-08-28 16:46:33 # Filename : tw_gearman.py # Description : from twisted.internet import reactor from twisted.web.server import Site from twisted.web.resource import Resource, ErrorPage from template import render from gearman import GearmanClient temp = render('temp/') new_client = GearmanClient(["192.168.8.116:1234"]) class PinYin(Resource): def getChild(self, name, request): return self def render_GET(self, request): return temp.pinyin() def render_POST(self, request): line = request.args["words"][0] return new_client.submit_job('pinyin', line).result site = Site(PinYin()) reactor.listenTCP(1235, site, interface='0.0.0.0') reactor.run()
def main(): parser = ArgumentParser(prog="konnectd", add_help=False, allow_abbrev=False, formatter_class=ArgumentDefaultsHelpFormatter) parser.add_argument("--name", default=node(), help="Device name") parser.add_argument("--verbose", action="store_true", default=False, help="Show debug messages") parser.add_argument("--discovery-port", default=1716, type=int, dest="discovery_port", help="Protocol discovery port") parser.add_argument("--service-port", default=1764, type=int, dest="service_port", help="Protocol service port") parser.add_argument("--admin-port", default=8080, type=int, dest="admin_port", help="Admin Rest API port") parser.add_argument("--config-dir", default="~/.config/konnect", dest="config_dir", help="Config directory") parser.add_argument("--receiver", action="store_true", default=False, help="Listen for new devices") parser.add_argument("--service", action="store_true", default=False, help="Send logs to journald") parser.add_argument("--help", action="store_true", help="This help") parser.add_argument("--version", action="store_true", help="Version information") args = parser.parse_args() if args.help: parser.print_help() exit() elif args.version: print(f"Konnectd {__version__}") exit() level = DEBUG if args.verbose else INFO if args.service is True: handler = JournalHandler(SYSLOG_IDENTIFIER="konnectd") basicConfig(format="%(levelname)s %(message)s", level=level, handlers=[handler]) else: basicConfig(format="%(asctime)s %(levelname)s %(message)s", level=level) args.config_dir = expanduser(args.config_dir) makedirs(args.config_dir, exist_ok=True) database = Database(args.config_dir) try: options = Certificate.load_options(args.config_dir) identifier = Certificate.extract_identifier(options) except (FileNotFoundError, Error): identifier = str(uuid4()).replace("-", "") Certificate.generate(identifier, args.config_dir) options = Certificate.load_options(args.config_dir) konnect = KonnectFactory(database, identifier, args.name, options) discovery = Discovery(identifier, args.name, args.discovery_port, args.service_port) info(f"Starting Konnectd {__version__} as {args.name}") reactor.listenTCP(args.service_port, konnect, interface="0.0.0.0") reactor.listenUDP(args.discovery_port if args.receiver else 0, discovery, interface="0.0.0.0") reactor.listenTCP(args.admin_port, Site(API(konnect, discovery)), interface="127.0.0.1") reactor.run()
def webServerFactory(api): return Site(webserver(api).root)
class API(Resource): isLeaf = False def __init__(self, *a, **kw): log.msg("API()") Resource.__init__(self, *a, **kw) self.putChild("", API_Root()) self.putChild("scan", ScanManager()) self.putChild("devices", DevicesManager()) self.putChild("connected", ConnectionManager()) self.putChild("configure", ConfigurationManager()) self.putChild("doconfigure", doConfiguration()) self.putChild("disconnect", DisconnectManager()) self.putChild("updates", UpdateManager()) self.putChild("switchstate", StateManager()) if __name__ == '__main__': from twisted.application.service import Application from twisted.application.internet import TCPServer from twisted.web.server import Site from twisted.internet import reactor import sys log.startLogging(sys.stdout) root = Resource() root.putChild("api", API()) reactor.listenTCP(8800, Site(root), interface="0.0.0.0") reactor.run() #!/usr/bin/env python
def setUp(self): super(SiteTest, self).setUp() factory = Site(ServerRoot()) self.proto = factory.buildProtocol(('127.0.0.1', 0)) self.tr = proto_helpers.StringTransport() self.proto.makeConnection(self.tr)
from twisted.web.server import Site from twisted.web.resource import Resource from twisted.internet import reactor from twisted.web.static import File import time import json from service import Service #from restoreService import RestoreService #from userService import UserService #from adminService import AdminService #from statusService import StatusService PORT = 9080 root = Resource() root.putChild("backup", Service()) #root.putChild("restore", RestoreService()) #root.putChild("user", UserService()) #root.putChild("admin", AdminService()) #root.putChild("status", StatusService()) factory = Site(root) #factory = Site(Service()) reactor.listenTCP(PORT, factory) reactor.run()
def main(): log.startLogging(sys.stdout) accountant = AccountantProxy( "dealer", config.get("accountant", "cashier_export"), config.getint("accountant", "cashier_export_base_port")) session = db.make_session() bitcoin_conf = config.get("cashier", "bitcoin_conf") log.msg('connecting to bitcoin client') bitcoinrpc = {'BTC': BitcoinRpc(bitcoin_conf, 1)} compropago = Compropago(config.get("cashier", "compropago_key")) cold_wallet_period = config.getint("cashier", "cold_wallet_period") sendmail = Sendmail(config.get("administrator", "email")) minimum_confirmations = config.getint("cashier", "minimum_confirmations") alerts_proxy = AlertsProxy(config.get("alerts", "export")) bitgo_config = { 'use_production': not config.getboolean("cashier", "testnet"), 'client_id': config.get("bitgo", "client_id"), 'client_secret': config.get("bitgo", "client_secret") } bitgo = BitGo(**bitgo_config) bitgo_private_key_file = config.get("cashier", "bitgo_private_key_file") cashier = Cashier( session, accountant, bitcoinrpc, compropago, cold_wallet_period=cold_wallet_period, sendmail=sendmail, minimum_confirmations=minimum_confirmations, alerts=alerts_proxy, bitgo=bitgo, bitgo_private_key_file=bitgo_private_key_file, testnet=config.getboolean("cashier", "testnet"), ) administrator_export = AdministratorExport(cashier) accountant_export = AccountantExport(cashier) webserver_export = WebserverExport(cashier) watchdog(config.get("watchdog", "cashier")) router_share_async(administrator_export, config.get("cashier", "administrator_export")) pull_share_async(accountant_export, config.get("cashier", "accountant_export")) router_share_async(webserver_export, config.get("cashier", "webserver_export")) public_server = Resource() public_server.putChild('compropago', CompropagoHook(cashier)) private_server = Resource() private_server.putChild('bitcoin', BitcoinNotify(cashier)) if config.getboolean("webserver", "ssl"): key = config.get("webserver", "ssl_key") cert = config.get("webserver", "ssl_cert") cert_chain = config.get("webserver", "ssl_cert_chain") contextFactory = ssl.ChainedOpenSSLContextFactory(key, cert_chain) reactor.listenSSL(config.getint("cashier", "public_port"), Site(public_server), contextFactory, interface=config.get("cashier", "public_interface")) else: reactor.listenTCP(config.getint("cashier", "public_port"), Site(public_server), interface=config.get("cashier", "public_interface")) reactor.listenTCP(config.getint("cashier", "private_port"), Site(private_server), interface=config.get("cashier", "private_interface")) reactor.run()
def onOpen(self): self.factory.register(self) WebSocketServerProtocol.onOpen(self) def connectionLost(self, reason): self.factory.unregister(self) WebSocketServerProtocol.connectionLost(self, reason) def onMessage(self, payload, isBinary): if not isBinary: change_doc = json.loads(payload) self.factory.onchange(self, change_doc) if __name__ == "__main__": import sys port = int(sys.argv[1]) if len(sys.argv) > 1 else 9779 factory = DBFactory() factory.protocol = DBProtocol ws_resource = WebSocketResource(factory) root = File(".") root.putChild(b"_db", ws_resource) site = Site(root) reactor.listenTCP(port, site, interface="0.0.0.0") print("http://localhost:%d" % (port)) reactor.run()
def main(): reactor.disconnectAll() log.warning("Dubaicoin Stratum proxy version: %s" % version.VERSION) # Connect to Stratum pool, main monitoring connection log.warning("Trying to connect to Stratum pool at %s:%d" % (settings.POOL_HOST, settings.POOL_PORT)) f = SocketTransportClientFactory( settings.POOL_HOST, settings.POOL_PORT, debug=settings.DEBUG, proxy=None, event_handler=client_service.ClientMiningService) f1 = None f2 = None f3 = None if settings.POOL_FAILOVER_ENABLE: log.warning( "Trying to connect to failover Stratum pool-1 at %s:%d" % (settings.POOL_HOST_FAILOVER1, settings.POOL_PORT_FAILOVER1)) f1 = SocketTransportClientFactory( settings.POOL_HOST_FAILOVER1, settings.POOL_PORT_FAILOVER1, debug=settings.DEBUG, proxy=None, event_handler=client_service.ClientMiningService) f1.is_failover = True log.warning( "Trying to connect to failover Stratum pool-2 at %s:%d" % (settings.POOL_HOST_FAILOVER2, settings.POOL_PORT_FAILOVER2)) f2 = SocketTransportClientFactory( settings.POOL_HOST_FAILOVER2, settings.POOL_PORT_FAILOVER2, debug=settings.DEBUG, proxy=None, event_handler=client_service.ClientMiningService) f2.is_failover = True log.warning( "Trying to connect to failover Stratum pool-3 at %s:%d" % (settings.POOL_HOST_FAILOVER3, settings.POOL_PORT_FAILOVER3)) f3 = SocketTransportClientFactory( settings.POOL_HOST_FAILOVER3, settings.POOL_PORT_FAILOVER3, debug=settings.DEBUG, proxy=None, event_handler=client_service.ClientMiningService) f3.is_failover = True job_registry = jobs.JobRegistry(f, f1, f2, f3) client_service.ClientMiningService.job_registry = job_registry client_service.ClientMiningService.reset_timeout() f.on_connect.addCallback(on_connect) f.on_disconnect.addCallback(on_disconnect) # Cleanup properly on shutdown reactor.addSystemEventTrigger('before', 'shutdown', on_shutdown, f) if f1: f1.on_connect.addCallback(on_connect) f1.on_disconnect.addCallback(on_disconnect) reactor.addSystemEventTrigger('before', 'shutdown', on_shutdown, f1) if f2: f2.on_connect.addCallback(on_connect) f2.on_disconnect.addCallback(on_disconnect) reactor.addSystemEventTrigger('before', 'shutdown', on_shutdown, f2) if f3: f3.on_connect.addCallback(on_connect) f3.on_disconnect.addCallback(on_disconnect) reactor.addSystemEventTrigger('before', 'shutdown', on_shutdown, f3) # Block until proxy connect to the pool try: yield f.on_connect except TransportException: log.warning("First pool server must be online first time during start") return conn = reactor.listenTCP( settings.PORT, Site(getwork_listener.Root(job_registry, settings.ENABLE_WORKER_ID)), interface=settings.HOST) try: conn.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # Enable keepalive packets conn.socket.setsockopt(socket.SOL_TCP, socket.TCP_KEEPIDLE, 60) # Seconds before sending keepalive probes conn.socket.setsockopt( socket.SOL_TCP, socket.TCP_KEEPINTVL, 1) # Interval in seconds between keepalive probes conn.socket.setsockopt( socket.SOL_TCP, socket.TCP_KEEPCNT, 5) # Failed keepalive probles before declaring other end dead except: pass # Some socket features are not available on all platforms (you can guess which one) log.warning( "-----------------------------------------------------------------------" ) if settings.HOST == '0.0.0.0': log.warning("PROXY IS LISTENING ON ALL IPs ON PORT %d" % settings.PORT) else: log.warning("LISTENING FOR MINERS ON http://%s:%d" % (settings.HOST, settings.PORT)) log.warning( "-----------------------------------------------------------------------" ) log.warning("Wallet: %s" % settings.WALLET) log.warning("Worker ID enabled: %s" % settings.ENABLE_WORKER_ID) if settings.MONITORING: log.warning("Email monitoring on %s" % settings.MONITORING_EMAIL) else: log.warning("Email monitoring disabled") log.warning("Failover enabled: %s" % settings.POOL_FAILOVER_ENABLE) log.warning( "-----------------------------------------------------------------------" )
from twisted.internet import reactor, task from twisted.web.server import Site from twisted.web import server from twisted.web.resource import Resource import time class ClockPage(Resource): isLeaf = True def __init__(self): self.presence = [] loopingCall = task.LoopingCall(self.__print_time) loopingCall.start(1, False) Resource.__init__(self) def render_GET(self, request): request.write('<b>%s</b><br>' % (time.ctime(), )) self.presence.append(request) return server.NOT_DONE_YET def __print_time(self): for p in self.presence: p.write('<b>%s</b><br>' % (time.ctime(), )) resource = ClockPage() factory = Site(resource) reactor.listenTCP(8080, factory) reactor.run()