def __init__(self, pdfpath, cacheroot): self.pdf = Pdf(pdfpath) self.cacheroot = cacheroot if not os.path.exists(cacheroot): os.makedirs(cacheroot) File.__init__(self, pdfpath)
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 requestAvatar(self, avatarId, mind, *interfaces): for iface in interfaces: if iface is resource.IResource: resc = File("/tmp") resc.realm = self return (resource.IResource, resc, lambda: None) raise NotImplementedError("Can't support that interface.")
def setUp(self): from twisted.python import threadpool reactor.threadpool = threadpool.ThreadPool(0, 10) reactor.threadpool.start() class Hello(resource.Resource): isLeaf = True def render_GET(self, request): return "<html>Hello, world!</html>" self.src_path = tempfile.mkdtemp(suffix=".src") self.cache_path = tempfile.mkdtemp(suffix=".cache") self._resource = None self.createSrcFile("a", "content of a") src = File(self.src_path) src.putChild("hello", Hello()) factory = Site(src) self.httpserver = reactor.listenTCP(0, factory) p = self.httpserver.getHost().port plugProps = {"properties": {"cache-size": CACHE_SIZE, "cache-dir": self.cache_path, "virtual-hostname": "localhost", "virtual-port": p}} self.plug = \ file_provider.FileProviderHTTPCachedPlug(plugProps) self.stats = DummyStats() self.plug.startStatsUpdates(self.stats) return self.plug.start(None)
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 startService(self): factory = WebSocketServerFactory(u"ws://127.0.0.1:%d" % self.port, debug=self.debug) factory.protocol = DispatcherProtocol factory.protocol.core = self._core # 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("leap.bitmask_core", "web")) root = File(webdir) # and our WebSocket server under "/ws" root.putChild(u"bitmask", resource) # both under one Twisted Web Site site = Site(root) self.site = site self.factory = factory self.listener = reactor.listenTCP(self.port, site)
def startWebServer(options, config): # Extract properties from config log_dir = str(config["configuration"]["log_dir"]) content = str(config["configuration"]["content"]) endpoint = str(config["configuration"]["endpoint"]) host = str(config["configuration"]["host"]) port = int(config["configuration"]["port"]) # Setup logging logFileName = log_dir + os.sep + "launcherLog.log" formatting = '%(asctime)s:%(levelname)s:%(name)s:%(message)s' logging.basicConfig(level=logging.DEBUG, filename=logFileName, filemode='w', format=formatting) observer = log.PythonLoggingObserver() observer.start() if options.debug: console = logging.StreamHandler(sys.stdout) console.setLevel(logging.INFO) formatter = logging.Formatter(formatting) console.setFormatter(formatter) logging.getLogger('').addHandler(console) # Initialize web resource web_resource = File(content) if (len(content) > 0) else resource.Resource() # Attach launcher web_resource.putChild(endpoint, LauncherResource(options, config)) site = server.Site(web_resource) reactor.listenTCP(port, site, interface=host) reactor.run()
def start_web(port=8080): root = File('./enocean_site') root.putChild('radio', RadioResource()) site = Site(root) reactor.listenTCP(port, site) logging.info('HTTP server running on port '+str(port)) return site
def main(): config = ConfigParser.ConfigParser() config.read(os.path.expanduser('~/.b07')) api = b07.api.API(reactor, config.get('ingress', 'email'), config.get('ingress', 'password')) http_root = Redirect('https://localhost:{}/'.format(config.get('server', 'https_port'))) http_factory = Site(http_root) http_endpoint = endpoints.serverFromString(reactor, 'tcp:{}'.format(config.get('server', 'http_port'))) http_endpoint.listen(http_factory) https_root = File(os.path.expanduser(config.get('server', 'web_root'))) https_root.indexNames = ['index.html'] https_factory = Site(https_root) https_endpoint = endpoints.serverFromString(reactor, 'ssl:{}:privateKey={}:certKey={}'.format(config.get('server', 'https_port'), os.path.expanduser(config.get('server', 'ssl_key')), os.path.expanduser(config.get('server', 'ssl_cert')))) https_endpoint.listen(https_factory) wsfactory = Factory() wsfactory.protocol = WebSocketProtocol wss_endpoint = endpoints.serverFromString(reactor, 'ssl:{}:privateKey={}:certKey={}'.format(config.get('server', 'wss_port'), os.path.expanduser(config.get('server', 'ssl_key')), os.path.expanduser(config.get('server', 'ssl_cert')))) wss_endpoint.listen(txws.WebSocketFactory(wsfactory)) reactor.run()
def download_zip(self, request, request_id): download = yield self.download_database.get_download( self.logger, request_id=request_id ) assert download.completed self.logger.bind( request_id=request_id, file_number=download.file_number, ).emit("download") document_types = yield self.document_types.wait() path = download.build_zip(self.jinja_env, self.fernet, document_types) request.setHeader( "Content-Disposition", "attachment; filename={}-eFolder.zip".format(download.file_number) ) resource = File(path, defaultType="application/zip") resource.isLeaf = True request.notifyFinish().addBoth(lambda *args, **kwargs: os.remove(path)) returnValue(resource)
def startService(self): factory = WebSocketServerFactory(u"ws://127.0.0.1:%d" % self.port) factory.protocol = EchoServerProtocol # 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" (note that Twisted uses # bytes for URIs) root.putChild(b"ws", resource) # both under one Twisted Web Site site = Site(root) self.site = site self.factory = factory self.listener = reactor.listenTCP(self.port, site)
def makeService(self, options): if not options["noisy-logging"]: protocol.Factory.noisy = False endpoint = twitterEndpoint(options["endpoint"]) tickeryService = service.MultiService() cache = TickeryCache(options["cache-dir"], options["restore-add-queue"], int(options["queue-width"]), endpoint) cache.setServiceParent(tickeryService) root = File("tickery/www/output") root.putChild("tickery", RegularService(cache, endpoint)) root.putChild(defaults.TICKERY_CALLBACK_CHILD, callback.Callback(cache)) adminRoot = File("tickery/admin/output") adminRoot.putChild("tickery", AdminService(cache)) root.putChild("admin", adminRoot) factory = server.Site(root) if options["promiscuous"]: kw = {} else: kw = {"interface": "localhost"} tickeryServer = internet.TCPServer(int(options["port"]), factory, **kw) tickeryServer.setServiceParent(tickeryService) return tickeryService
def main(argv): from twisted.python import log from twisted.web.server import Site from twisted.web.static import File from twisted.internet import reactor from twisted.python import log observer = log.PythonLoggingObserver('twisted') log.startLoggingWithObserver(observer.emit, setStdout=False) static_dir = os.path.abspath('.') logging.info("registering static folder %r on /" % static_dir) root = File(static_dir) wr = TwistedWebResource(soap11_application) logging.info("registering %r on /%s" % (wr, url)) root.putChild(url, wr) site = Site(root) if port[0] == 0: port[0] = get_open_port() reactor.listenTCP(port[0], site) logging.info("listening on: %s:%d" % (host,port)) return reactor.run()
class Insist(Resource): isLeaf = True def __init__(self, cacheloc): self.cacheloc = cacheloc self.cachefile = None if os.path.exists(cacheloc): self.cachefile = File(cacheloc) self.reqs_waiting = [] self.started = False Resource.__init__(self) def render_GET(self, req): # Check if someone else has created the file somehow if self.cachefile is None and os.path.exists(self.cacheloc): self.cachefile = File(self.cacheloc) # Check if someone else has *deleted* the file elif self.cachefile is not None and not os.path.exists(self.cacheloc): self.cachefile = None if self.cachefile is not None: return self.cachefile.render_GET(req) else: self.reqs_waiting.append(req) req.notifyFinish().addErrback( self._nevermind, req) if not self.started: self.started = True reactor.callInThread(self.desist) return NOT_DONE_YET def _nevermind(self, _err, req): self.reqs_waiting.remove(req) def desist(self): self.serialize_computation(self.cacheloc) reactor.callFromThread(self.resist) def _get_mime(self): return mimetypes.guess_type(self.cacheloc)[0] def resist(self): if not os.path.exists(self.cacheloc): # Error! print "%s does not exist - rendering fail!" % (self.cacheloc) for req in self.reqs_waiting: req.headers["Content-Type"] = "text/plain" req.write("cyst error") req.finish() return self.cachefile = File(self.cacheloc) # Send content to all interested parties for req in self.reqs_waiting: self.cachefile.render(req) def serialize_computation(self, outpath): raise NotImplemented
def getChild(self, path, request): resource = File.getChild(self, path, request) for pat, repl in self._virtualLinks.iteritems(): newPath = pat.sub(repl, path) if newPath != path: resource = File.getChild(self, newPath, request) break return resource
def __init__(self, feederName, lockCamera, lockSensor): threading.Thread.__init__(self) feederName = feederName res = File('/home/pi/PiFeed/src/') res.putChild('', res) factory = Site(res) reactor.listenTCP(8000, factory) print('%s: twisted web server started' % (feederName))
def setup(scope, port=21000): MainHandler.scope = scope MainHandler.setup() global root root = File(".") root.putChild('_pyreg',File(os.path.dirname(__file__)+'/_pyreg')) site = WebSocketSite(root) site.addHandler("/ws/websocket", MainHandler) reactor.listenTCP(port, site)
def __init__(self, service): pagedir = os.getcwd() + "/www" timeout = 60*60*12 root = File(pagedir) dbres = Uploader() root.putChild("upload", dbres) root.childNotFound = File(pagedir+"/404.html") server.Site.__init__(self, root, logPath=None, timeout=timeout) self.service = service
def getChild(self, uid, req): out_dir = self.transcriber.out_dir(uid) trans_ctrl = File(out_dir) # Add a Status endpoint to the file trans_status = TranscriptionStatus(self.transcriber.get_status(uid)) trans_ctrl.putChild("status.json", trans_status) return trans_ctrl
def getChild(self, name, request): #make source media available via oshash if request.path.startswith('/get/'): oshash = request.path.split('/')[-1] for path in self.client.path(oshash): if os.path.exists(path): f = File(path, 'application/octet-stream') f.isLeaf = True return f return self
def runatomix(): log.startLogging(stdout) factory = WebSocketServerFactory(u"ws://127.0.0.1:8080") factory.protocol = WebServerProtocol resource = WebSocketResource(factory) root = File(".") root.putChild(u"ws", resource) site = Site(root) reactor.listenTCP(8080, site) reactor.run()
def getChild(self, name, request): versionString = 'phoenix/%s' % self.core.VERSION request.setHeader('Server', versionString) if request.method == 'POST' and request.path == '/': return self else: docroot = os.path.join(self.core.basedir, 'www') root = File(self.core.config.get('web', 'root', str, docroot)) root.processors = {'.rpy': script.ResourceScript} return root.getChild(name, request)
def setup(): log_handler, log_buffer = setupLogging() status = monitor.status.Status() # Create our global shared status. Sort of a hard coded file adapter. config_file = os.path.join(BASE_DIR, 'server.json') with open(config_file, 'r') as f: status.set('status://server', json.load(f)) # Setup the normal adapters. setupAdapters(status) # Create the manager for performing actions. action_manager = monitor.actions.ActionManager(status) # Instantiating the engine sets up the deferreds needed to keep it running. monitor.rules_engine.RulesEngine(status, action_manager) # Assemble the factory for our web server. # Serve the standard static web content, overlaid with our dynamic content root = File("./static") root.putChild("button", monitor.web_resources.Button(status)) root.putChild("log", monitor.web_resources.Log(log_handler, log_buffer)) root.putChild("restart", monitor.web_resources.Restart(status)) root.putChild("status", monitor.web_resources.Status(status)) reactor.listenTCP(status.get('status://server/port', 8080), Site(root))
def run(self): task.LoopingCall(self.status_check).start(0.5) root = File('www') root.putChild('st', StatusHandler(self)) root.putChild('upd', self.updateHandler) root.putChild('cfg', ConfigHandler(self)) root.putChild('upt', UptimeHandler(self)) if self.config['config']['use_auth']: clk = ClickHandler(self) args={self.config['site']['username']:self.config['site']['password']} checker = checkers.InMemoryUsernamePasswordDatabaseDontUse(**args) realm = HttpPasswordRealm(clk) p = portal.Portal(realm, [checker]) credentialFactory = BasicCredentialFactory("Garage Door Controller") protected_resource = HTTPAuthSessionWrapper(p, [credentialFactory]) root.putChild('clk', protected_resource) else: root.putChild('clk', ClickHandler(self)) site = server.Site(root) if not self.get_config_with_default(self.config['config'], 'use_https', False): reactor.listenTCP(self.config['site']['port'], site) # @UndefinedVariable reactor.run() # @UndefinedVariable else: sslContext = ssl.DefaultOpenSSLContextFactory(self.config['site']['ssl_key'], self.config['site']['ssl_cert']) reactor.listenSSL(self.config['site']['port_secure'], site, sslContext) # @UndefinedVariable reactor.run() # @UndefinedVariable
def __init__(self, bucket, path, defaultType="text/html", ignoredExts=(), registry=None, allowExt=0, pwdic=None): # print "Instantiating WebDAV instance:", path File.__init__(self, path, defaultType=defaultType, ignoredExts=ignoredExts, registry=registry, allowExt=allowExt) self.path = path self.pwdic = pwdic self.s3 = boto.connect_s3() self.bucket = self.s3.get_bucket(bucket) self.mapper = S3ObjectMapper()
def secureSite(_environ=environ): """Builds the secure (HTTPS, port 443) site. """ root = File(_environ["STATIC_PATH"]) root.putChild("subscribe", SubscribeResource("*****@*****.**")) site = Site(root) site.displayTracebacks = False site.requestFactory = _withHSTS(site.requestFactory) return site
def render_GET(self, request): since = request.getHeader('if-modified-since') if since: tsince = time.strptime(since.replace(" GMT", "")) tfrom = time.strptime(time.ctime(os.path.getmtime(self.path))) if tfrom > tsince: return File.render_GET(self, request) else: request.setResponseCode(304) return "" return File.render_GET(self, request)
def run(wf, port=9090): # web-control of a workflow datapath = os.path.join(os.path.dirname(__file__), "data") root = File(datapath) workflow_api = WorkflowAPI(wf) root.putChild("wf", workflow_api) site = Site(root) reactor.listenTCP(port, site, interface="0.0.0.0") print 'listening on port '+str(port) reactor.run()
def resolve(self, path): if path == []: path = ['index.html'] relative_path = os.path.join(*(['../..'] + path)) filename = pkg_resources.resource_filename(__name__, relative_path) resource = File(filename) if not resource.exists(): return False return ONCView(resource)
def _file(self, path, type, encoding=None): """ Construct a `twisted.web.static.File` customized to serve Yarrharr static assets. :param path: `twisted.internet.filepath.FilePath` instance :returns: `twisted.web.resource.IResource` """ f = File(path.path) f.type = type f.encoding = encoding return f
def root(request): return File(os.path.dirname(__file__))
def _configure_named_resource(self, name, compress=False): """Build a resource map for a named resource Args: name (str): named resource: one of "client", "federation", etc compress (bool): whether to enable gzip compression for this resource Returns: dict[str, Resource]: map from path to HTTP resource """ resources = {} if name == "client": client_resource = ClientRestResource(self) if compress: client_resource = gz_wrap(client_resource) resources.update( { "/_matrix/client/api/v1": client_resource, "/_matrix/client/r0": client_resource, "/_matrix/client/unstable": client_resource, "/_matrix/client/v2_alpha": client_resource, "/_matrix/client/versions": client_resource, "/.well-known/matrix/client": WellKnownResource(self), "/_synapse/admin": AdminRestResource(self), } ) if self.get_config().oidc_enabled: from synapse.rest.oidc import OIDCResource resources["/_synapse/oidc"] = OIDCResource(self) if self.get_config().saml2_enabled: from synapse.rest.saml2 import SAML2Resource resources["/_matrix/saml2"] = SAML2Resource(self) if self.get_config().threepid_behaviour_email == ThreepidBehaviour.LOCAL: from synapse.rest.synapse.client.password_reset import ( PasswordResetSubmitTokenResource, ) resources[ "/_synapse/client/password_reset/email/submit_token" ] = PasswordResetSubmitTokenResource(self) if name == "consent": from synapse.rest.consent.consent_resource import ConsentResource consent_resource = ConsentResource(self) if compress: consent_resource = gz_wrap(consent_resource) resources.update({"/_matrix/consent": consent_resource}) if name == "federation": resources.update({FEDERATION_PREFIX: TransportLayerServer(self)}) if name == "openid": resources.update( { FEDERATION_PREFIX: TransportLayerServer( self, servlet_groups=["openid"] ) } ) if name in ["static", "client"]: resources.update( { STATIC_PREFIX: StaticResource( os.path.join(os.path.dirname(synapse.__file__), "static") ) } ) if name in ["media", "federation", "client"]: if self.get_config().enable_media_repo: media_repo = self.get_media_repository_resource() resources.update( {MEDIA_PREFIX: media_repo, LEGACY_MEDIA_PREFIX: media_repo} ) elif name == "media": raise ConfigError( "'media' resource conflicts with enable_media_repo=False" ) if name in ["keys", "federation"]: resources[SERVER_KEY_V2_PREFIX] = KeyApiV2Resource(self) if name == "webclient": webclient_loc = self.get_config().web_client_location if webclient_loc is None: logger.warning( "Not enabling webclient resource, as web_client_location is unset." ) elif webclient_loc.startswith("http://") or webclient_loc.startswith( "https://" ): resources[WEB_CLIENT_PREFIX] = RootRedirect(webclient_loc) else: logger.warning( "Running webclient on the same domain is not recommended: " "https://github.com/matrix-org/synapse#security-note - " "after you move webclient to different host you can set " "web_client_location to its full URL to enable redirection." ) # GZip is disabled here due to # https://twistedmatrix.com/trac/ticket/7678 resources[WEB_CLIENT_PREFIX] = File(webclient_loc) if name == "metrics" and self.get_config().enable_metrics: resources[METRICS_PREFIX] = MetricsResource(RegistryProxy) if name == "replication": resources[REPLICATION_PREFIX] = ReplicationRestResource(self) return resources
from twisted.internet import reactor from twisted.web.server import Site from twisted.web.static import File resource = File('/var/www/mysite') factory = Site(resource) reactor.listenTCP(8000, factory) reactor.run()
log.startLogging(sys.stdout) debug = True else: debug = False wsPort = 9000 # Our WebSocket server ## factory = WebSocketServerFactory(u"ws://127.0.0.1:%d" % wsPort, debug=debug, debugCodePaths=debug) factory.protocol = EchoServerProtocol listenWS(factory) # We need to start a "Flash Policy Server" on TCP/843 # which Adobe Flash player will contact to check if # it is allowed to connect to the WebSocket port. ## flashPolicyFactory = FlashPolicyFactory() reactor.listenTCP(843, flashPolicyFactory) # Static Web server ## webdir = File("./web") web = Site(webdir) reactor.listenTCP(8080, web) reactor.run()
from twisted.web.server import Site from twisted.web.static import File from twisted.internet import reactor from twisted.internet import endpoints resource = File("./static") factory = Site(resource) endpoint = endpoints.TCP4ServerEndpoint(reactor, 8000) endpoint.listen(factory) reactor.run()
def build_resource_for_static_content(self): # This is old and should go away: not going to bother adding gzip return File("static")
# Get PLC time plcTime = PLCSystemTime(plc) d = plcTime.get() def onResult(data): print "PLC Time is", data return data d.addCallback(onResult) #plcTime.set(None) # Set PLC to current server time # Start listening as HTTP server root = File("www") #root.putChild("membrane_insert", Membrane(plc)) root.putChild("events", EventSource(plc)) root.putChild("write", Write(plc)) root.putChild("read", Read(plc)) root.putChild("logger", Logger(plc)) root.putChild("membrane", Membrane()) root.putChild("product", Product()) root.putChild("chemical", Chemical()) root.putChild("bag-filter", BagFilter()) factory = Site(root) reactor.listenTCP(8000, factory) reactor.run() exit()
def start_server(camera_type, simulate_pixhawk, port=8000): """ Start the airborne server. Parameters ---------- camera_type: string Camera type. Options: [canon (default), simlulation]. port: int, optional(=8000) Port used by server. """ # # Create the auvsi data folder. # if not os.path.exists(gs.AUVSI_BASE_FOLDER): os.makedirs(gs.AUVSI_BASE_FOLDER) # # Setup logging. # f = DailyLogFile('server.log', gs.AUVSI_BASE_FOLDER) log.startLogging(f) # # Initialize the data base. # DB.initDB() # # Initialize the imageprocessing module. # IM.initIM() # # Initialize the pixhawk module. # if not simulate_pixhawk: PH.initPixHawk() else: PH.initPixHawkSimulation() # # Create the camera object. # global camera if camera_type.lower() == 'canon': camera = CanonCamera() elif camera_type.lower() == 'simulation': camera = SimulationCamera() else: raise NotImplementedError( 'Camera type {camera}, not supported.'.format(camera=camera_type)) # # add a watcher on the images folder # fs = FileSystemWatcher(gs.IMAGES_FOLDER) fs.Start() # # Config the server # root = HTTPserverMain() root.putChild("images", File(gs.RESIZED_IMAGES_FOLDER)) factory = Site(root) # # Startup the reactor. # reactor.listenTCP(port, factory) reactor.run()
def static(self, request): return File(self.static_dir)
def server(): root = File(sys.argv[1]) factory = Site(root) reactor.listenTCP(PORT, factory) reactor.run()
def main(): parser = argparse.ArgumentParser() parser.add_argument('-u', '--url', default='ws://localhost:9001', help='Listen URL [default: %(default)s]') parser.add_argument( '-O', '--options', default=jsondict({'failByDrop': False}), type=jsondict, action='append', help='extra options (overwrites existing) [default: %(default)s]') parser.add_argument('-p', '--webport', default=9090, type=int) parser.add_argument('-i', '--listen-interface', default='localhost', help='interface to listen on') parser.add_argument('-w', '--webdir', default='.') parser.add_argument('-d', '--debug', default=False, action='store_true', help='Debug Mode [default: %(default)s]') parser.add_argument('-o', '--outdir', default='./pages/results', metavar='DIR', help='Output Directory [default: %(default)s]') parser.add_argument('-c', '--cases', default=['*'], nargs='+', help='test cases [default: %(default)s]') parser.add_argument('-x', '--exclude-cases', default=[], nargs='+', help='test cases to exclude [default: %(default)s]') parser.add_argument('-l', '--logfile', type=argparse.FileType('w'), default=sys.stdout, help='logging file [default: stdout]') parser.add_argument( '-t', '--exit-timeout', metavar='SECONDS', default=None, type=float, help= 'Will automatically exit after %(metavar)s seconds [default: %(default)s]' ) args = parser.parse_args() spec = args.__dict__ log.startLogging(args.logfile) ## fuzzing server fuzzer = FuzzingServerFactory(spec) listenWS(fuzzer, interface=args.listen_interface) ## web server webdir = File(args.webdir) web = Site(webdir) reactor.listenTCP(args.webport, web, interface=args.listen_interface) log.msg("Using Twisted reactor class %s" % str(reactor.__class__)) if args.exit_timeout: def exit_callback(): log.msg("Exiting due to timeout (--exit-timeout/-t)") reactor.fireSystemEvent('shutdown') #reactor.stop() sys.exit(12) reactor.callLater(args.exit_timeout, exit_callback) reactor.run()
#! /usr/bin/env python # -*- coding: utf-8 -*- # vim:fenc=utf-8 # # """ HTTP Static content site + dispatches """ from twisted.internet import reactor from twisted.web.server import Site from twisted.web.static import File root = File("/var/www/html") root.putChild("log", File("/var/log/syslog")) root.putChild("doc", File("/usr/share/doc")) reactor.listenTCP(8000, Site(root)) reactor.run()
def serve(port=8765, interface='0.0.0.0', installSignalHandlers=0, nthreads=4, ntranscriptionthreads=2, data_dir=get_datadir('webdata')): logging.info("SERVE %d, %s, %d", port, interface, installSignalHandlers) if not os.path.exists(data_dir): os.makedirs(data_dir) zip_dir = os.path.join(data_dir, 'zip') if not os.path.exists(zip_dir): os.makedirs(zip_dir) f = File(data_dir) f.putChild('', File(get_resource('www/index.html'))) f.putChild('status.html', File(get_resource('www/status.html'))) f.putChild('preloader.gif', File(get_resource('www/preloader.gif'))) trans = Transcriber(data_dir, nthreads=nthreads, ntranscriptionthreads=ntranscriptionthreads) trans_ctrl = TranscriptionsController(trans) f.putChild('transcriptions', trans_ctrl) trans_zippr = TranscriptionZipper(zip_dir, trans) f.putChild('zip', trans_zippr) s = Site(f) logging.info("about to listen") reactor.listenTCP(port, s, interface=interface) logging.info("listening") reactor.run(installSignalHandlers=installSignalHandlers)
def __init__( self, pool, ui_enabled, lua_enabled, lua_sandbox_enabled, lua_package_path, lua_sandbox_allowed_modules, max_timeout, argument_cache_max_entries, strict_lua_runner, ): Resource.__init__(self) self.argument_cache = ArgumentCache(argument_cache_max_entries) self.ui_enabled = ui_enabled self.lua_enabled = lua_enabled _args = pool, max_timeout, self.argument_cache self.putChild(b"render.html", RenderHtmlResource(*_args)) self.putChild(b"render.png", RenderPngResource(*_args)) self.putChild(b"render.jpeg", RenderJpegResource(*_args)) self.putChild(b"render.json", RenderJsonResource(*_args)) self.putChild(b"render.har", RenderHarResource(*_args)) self.putChild(b"_debug", DebugResource(pool, self.argument_cache)) self.putChild(b"_gc", ClearCachesResource(self.argument_cache)) self.putChild(b"_ping", PingResource()) # backwards compatibility self.putChild(b"debug", DebugResource(pool, self.argument_cache, warn=True)) if self.lua_enabled and ExecuteLuaScriptResource is not None: lua_kwargs = dict( pool=pool, sandboxed=lua_sandbox_enabled, lua_package_path=lua_package_path, lua_sandbox_allowed_modules=lua_sandbox_allowed_modules, max_timeout=max_timeout, argument_cache=self.argument_cache, strict=strict_lua_runner, ) self.putChild( b"execute", ExecuteLuaScriptResource(implicit_main=False, **lua_kwargs)) self.putChild( b"run", ExecuteLuaScriptResource(implicit_main=True, **lua_kwargs)) if self.ui_enabled: root = os.path.dirname(__file__) ui = File(os.path.join(root, 'ui')) har_path = os.path.join(root, 'vendor', 'harviewer', 'webapp') ui.putChild(to_bytes(HARVIEWER_PATH), File(har_path)) inspections_path = os.path.join(root, 'kernel', 'inspections') ui.putChild(b"inspections", File(inspections_path)) examples_path = os.path.join(root, 'examples') ui.putChild(b"examples", File(examples_path)) self.putChild(b"_ui", ui) self.putChild( DemoUI.PATH, DemoUI(pool=pool, lua_enabled=self.lua_enabled, max_timeout=max_timeout)) self.max_timeout = max_timeout
def _configure_named_resource(self, name, compress=False): """Build a resource map for a named resource Args: name (str): named resource: one of "client", "federation", etc compress (bool): whether to enable gzip compression for this resource Returns: dict[str, Resource]: map from path to HTTP resource """ resources = {} if name == "client": client_resource = ClientRestResource(self) if compress: client_resource = gz_wrap(client_resource) resources.update({ "/_matrix/client/api/v1": client_resource, "/_matrix/client/r0": client_resource, "/_matrix/client/unstable": client_resource, "/_matrix/client/v2_alpha": client_resource, "/_matrix/client/versions": client_resource, }) if name == "consent": from synapse.rest.consent.consent_resource import ConsentResource consent_resource = ConsentResource(self) if compress: consent_resource = gz_wrap(consent_resource) resources.update({ "/_matrix/consent": consent_resource, }) if name == "federation": resources.update({ FEDERATION_PREFIX: TransportLayerServer(self), }) if name in ["static", "client"]: resources.update({ STATIC_PREFIX: File(os.path.join(os.path.dirname(synapse.__file__), "static")), }) if name in ["media", "federation", "client"]: if self.get_config().enable_media_repo: media_repo = self.get_media_repository_resource() resources.update({ MEDIA_PREFIX: media_repo, LEGACY_MEDIA_PREFIX: media_repo, CONTENT_REPO_PREFIX: ContentRepoResource(self, self.config.uploads_path), }) elif name == "media": raise ConfigError( "'media' resource conflicts with enable_media_repo=False", ) if name in ["keys", "federation"]: resources.update({ SERVER_KEY_PREFIX: LocalKey(self), SERVER_KEY_V2_PREFIX: KeyApiV2Resource(self), }) if name == "webclient": resources[WEB_CLIENT_PREFIX] = build_resource_for_web_client(self) if name == "metrics" and self.get_config().enable_metrics: resources[METRICS_PREFIX] = MetricsResource(RegistryProxy) if name == "replication": resources[REPLICATION_PREFIX] = ReplicationRestResource(self) return resources
def run(self): task.LoopingCall(self.status_check).start(0.5) root = File('www') root.putChild('st', StatusHandler(self)) root.putChild('upd', self.updateHandler) root.putChild('cfg', ConfigHandler(self)) root.putChild('upt', UptimeHandler(self)) if self.config['config']['use_auth']: clk = ClickHandler(self) args={self.config['site']['username']:self.config['site']['password']} checker = checkers.InMemoryUsernamePasswordDatabaseDontUse(**args) realm = HttpPasswordRealm(clk) p = portal.Portal(realm, [checker]) credentialFactory = BasicCredentialFactory("Garage Door Controller") protected_resource = HTTPAuthSessionWrapper(p, [credentialFactory]) root.putChild('clk', protected_resource) cla = CloseHandler(self) cla_args={self.config['site']['username']:self.config['site']['password']} cla_checker = checkers.InMemoryUsernamePasswordDatabaseDontUse(**cla_args) cla_realm = HttpPasswordRealm(cla) cla_p = portal.Portal(cla_realm, [cla_checker]) credentialFactory = BasicCredentialFactory("Garage Door Controller") cla_protected_resource = HTTPAuthSessionWrapper(cla_p, [credentialFactory]) root.putChild('cla', cla_protected_resource) else: root.putChild('clk', ClickHandler(self)) root.putChild('cla', CloseHandler(self)) if self.config['config']['allow_api']: root.putChild('api', APIHandler(self)) site = server.Site(root) if not self.get_config_with_default(self.config['config'], 'use_https', False): reactor.listenTCP(self.config['site']['port'], site) # @UndefinedVariable reactor.run() # @UndefinedVariable else: sslContext = ssl.DefaultOpenSSLContextFactory(self.config['site']['ssl_key'], self.config['site']['ssl_cert']) reactor.listenSSL(self.config['site']['port_secure'], site, sslContext) # @UndefinedVariable reactor.run() # @UndefinedVariable
if lighttype not in lightTypes: logging.error("{}: Type '{}' is unknown, valid would be {}".format( s, lighttype, list(lightTypes.keys()))) continue options = {k: conf.get(s, k) for k in conf.options(s)} del options['type'] try: logging.info("Start {}".format(s)) l = lightTypes[lighttype].open(name=s, **options) except TypeError as e: # When option name is not known logging.error("{}: {}".format(s, e)) continue lights[s] = l root = File("../website/") interface = JSONAnswer(list(lights.keys())) interface.putChild(b"status", TrafficLightWeb(lights['local_light'])) root.putChild(b"interface", interface) for s in lights: # After init, dereference symbolic names lights[s].dereference(lights) # Finally add them to the web tree interface.putChild(bytes(s.encode('ascii')), TrafficLightWeb(lights[s])) # root.putChild("auth", Authenticator()) factory = Site(root) endpoint = endpoints.TCP4ServerEndpoint(reactor, port) endpoint.listen(factory) reactor.run()
def __init__(self, pool, ui_enabled, lua_enabled, lua_sandbox_enabled, lua_package_path, lua_sandbox_allowed_modules, max_timeout): Resource.__init__(self) self.ui_enabled = ui_enabled self.lua_enabled = lua_enabled self.putChild(b"render.html", RenderHtmlResource(pool, max_timeout)) self.putChild(b"render.png", RenderPngResource(pool, max_timeout)) self.putChild(b"render.jpeg", RenderJpegResource(pool, max_timeout)) self.putChild(b"render.json", RenderJsonResource(pool, max_timeout)) self.putChild(b"render.har", RenderHarResource(pool, max_timeout)) self.putChild(b"_debug", DebugResource(pool)) self.putChild(b"_gc", ClearCachesResource()) self.putChild(b"_ping", PingResource()) # backwards compatibility self.putChild(b"debug", DebugResource(pool, warn=True)) if self.lua_enabled and ExecuteLuaScriptResource is not None: self.putChild( b"execute", ExecuteLuaScriptResource( pool=pool, sandboxed=lua_sandbox_enabled, lua_package_path=lua_package_path, lua_sandbox_allowed_modules=lua_sandbox_allowed_modules, max_timeout=max_timeout)) if self.ui_enabled: root = os.path.dirname(__file__) ui = File(os.path.join(root, 'ui')) har_path = os.path.join(root, 'vendor', 'harviewer', 'webapp') ui.putChild(b"harviewer", File(har_path)) inspections_path = os.path.join(root, 'kernel', 'inspections') ui.putChild(b"inspections", File(inspections_path)) examples_path = os.path.join(root, 'examples') ui.putChild(b"examples", File(examples_path)) self.putChild(b"_ui", ui) self.putChild( DemoUI.PATH, DemoUI(pool=pool, lua_enabled=self.lua_enabled, max_timeout=max_timeout)) self.max_timeout = max_timeout
from twisted.web.server import Site from twisted.web.resource import Resource from twisted.internet import reactor from twisted.web.static import File import sys root = Resource() root.putChild("", File(sys.argv[0])) factory = Site(root) reactor.listenTCP(8080, factory) reactor.run()
def render_GET(self, request): parsedURL = theURLparse.urlparse(request.uri.decode().replace( "//", "/")) #scheme,netloc,path,query thisPath = parsedURL.path.replace("//", "/") root, ext = os.path.splitext(thisPath) if thisPath == "/" or thisPath == "": ext = ".py" filename = "/serverInfo.py" fileFolder = self.config['packageFolder'] + "/html/" fullPath = self.config['webServerRoot'] + fileFolder + filename elif thisPath in [ "/client.html", "/monitor.html", "/instructions.html", "/video.html", "/questionnaire.html", "/quiz.html", "/serverInfo.html" ]: ext = ".py" filename = thisPath.replace(".html", ".py").replace("/", "") fileFolder = self.config['packageFolder'] + "/html/" fullPath = self.config['webServerRoot'] + fileFolder + filename elif thisPath in ["/console.html"]: try: thisPage = int(getValueFromQueryKey(parsedURL.query, "page")) except: "thisPage isn't integer" thisPage = "" if thisPage == "" or thisPage == "current": try: thisPage = self.thisCounter.fileCount except: thisPage = 1 self.logURL = self.config['domain'] + self.config[ 'dataFolder'] + "/logs/%s.log" % (thisPage) self.currentLogTab = thisPage self.thisCounter.currentTab = thisPage ext = ".py" filename = thisPath.replace(".html", ".py").replace("/", "") fileFolder = self.config['packageFolder'] + "/html/" fullPath = self.config['webServerRoot'] + fileFolder + filename else: root, ext = os.path.splitext(thisPath) filename = os.path.basename(thisPath) if filename == "": filename = "index.py" ext = ".py" fileFolder = thisPath.replace(filename, "") fullPath = self.config['webServerRoot'] + fileFolder + filename if filename == "favicon.ico": fullPath = self.config['webServerRoot'] + self.config[ 'packageFolder'] + "/html/triangle.png" if ext == ".zip": #will download the data file for ANY zip extension. dataFolder = self.config['webServerRoot'] + self.config[ 'dataFolder'] outputName = self.config['webServerRoot'] + self.config[ 'currentExperiment'] + "/data/" + self.config[ 'serverStartString'] fullPath = outputName + ".zip" print("creating zip file ", fullPath, " for data folder ", dataFolder) shutil.make_archive(outputName, 'zip', dataFolder) filename = self.config['serverStartString'] + ".zip" #this causes file to be downloaded automatically rather than being opened in the browser. request.setHeader("Content-Disposition", "attachment") thisFile = File(fullPath) # os.remove(fullPath) return File.render_GET(thisFile, request) elif os.path.isfile(fullPath): if filename == "console.py": print("running %s from %s" % (filename, self.config['webServerRoot'] + fileFolder)) thisPage = imp.load_source( 'thisPage', self.config['webServerRoot'] + fileFolder + filename) output = thisPage.getPage(self.config, self.thisCounter.fileCount, self.currentLogTab) return output.encode('utf-8') elif ext == ".py": print("running %s from %s" % (filename, self.config['webServerRoot'] + fileFolder)) thisPage = imp.load_source( 'thisPage', self.config['webServerRoot'] + fileFolder + filename) output = thisPage.getPage(self.config) return output.encode('utf-8') elif ext == ".m4a": request.setHeader("Content-Type", "audio/mp4") thisFile = File(self.config['webServerRoot'] + thisPath) return File.render_GET(thisFile, request) elif ext == ".pickle": #this causes file to be downloaded automatically rather than being opened in the browser. request.setHeader("Content-Disposition", "attachment") thisFile = File(self.config['webServerRoot'] + thisPath) return File.render_GET(thisFile, request) else: #print "getting file: "+self.config['webServerRoot']+fileFolder+filename thisFile = File(self.config['webServerRoot'] + fileFolder + filename) return File.render_GET(thisFile, request) else: print(request) print("ErrorLine: File NOT found: %s" % (fullPath), file=sys.stderr) #print >> sys.stderr, "ErrorLine: File NOT found: %s"%(fullPath) return "<html><h1>File Not Found - %s</h1></html>" % (fullPath)
if __name__ == '__main__': if len(sys.argv) > 1 and sys.argv[1] == 'debug': log.startLogging(sys.stdout) debug = True else: debug = False factory = WebSocketServerFactory("ws://127.0.0.1:8080", debug=debug, debugCodePaths=debug) factory.protocol = EchoServerProtocol factory.setProtocolOptions( allowHixie76=True) # needed if Hixie76 is to be supported resource = WebSocketResource(factory) # we server static files under "/" .. root = File(".") # 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 reactor.listenTCP(8080, site) reactor.run()
from twisted.internet import reactor from twisted.web.server import Site from twisted.web.static import File root = File('/') root.putChild('var', File('/var')) factory = Site(root) reactor.listenTCP(8000, factory) reactor.run()
def onMessage(self, payload, isBinary): self.sendMessage(payload, isBinary) if __name__ == '__main__': log.startLogging(sys.stdout) factory = WebSocketServerFactory("ws://127.0.0.1:9000") factory.protocol = EchoServerProtocol # Enable WebSocket extension "permessage-deflate". # Function to accept offers from the client .. def accept(offers): for offer in offers: if isinstance(offer, PerMessageDeflateOffer): return PerMessageDeflateOfferAccept(offer) factory.setProtocolOptions(perMessageCompressionAccept=accept) # run server listenWS(factory) webdir = File(".") web = Site(webdir) reactor.listenTCP(8080, web) reactor.run()
def static(request): """Return the static directory.""" return File('static')
if args.debug: from twisted.python import log log.startLogging(sys.stdout) ## import Twisted reactor ## from twisted.internet import reactor print("Using Twisted reactor {0}".format(reactor.__class__)) ## create embedded web server for static files ## if args.web: from twisted.web.server import Site from twisted.web.static import File reactor.listenTCP(args.web, Site(File("."))) ## run WAMP application component ## from autobahn.twisted.wamp import ApplicationRunner router = args.router or 'ws://localhost:9000' runner = ApplicationRunner( router, u"realm1", standalone=not args.router, debug=False, # low-level logging debug_wamp=args.debug, # WAMP level logging debug_app=args.debug # app-level logging )
def __init__(self, *args, **kwargs): self._cache_timeout = kwargs.pop('cache_timeout', None) File.__init__(self, *args, **kwargs)
p = Builder(self.server_ip, self.server_port, self.project, self.version) p.factory = self return p if __name__ == '__main__': parser = argparse.ArgumentParser(description='A.I.R Builder Daemon') parser.add_argument('--ip', default='127.0.0.1', help='server ip') parser.add_argument('--port', dest='port', default=8090, type=int, help='Server Port') parser.add_argument('project', help='Project') parser.add_argument('version', help='Version') parser.add_argument('img', help='Image file') parser.add_argument('log', help='Changelog file') options = parser.parse_args() print '[Client Started]' resource = File('%s' % os.getcwd()) fileserver = Site(resource) reactor.listenTCP(8092, fileserver) reactor.connectTCP( options.ip, options.port, BuilderFactory(options.ip, options.port, options.project, options.version, options.img, options.log)) reactor.run()
def start_webserver(options, protocol=wsl.ServerProtocol, disableLogging=False): """ Starts the web-server with the given protocol. Options must be an object with the following members: options.host : the interface for the web-server to listen on options.port : port number for the web-server to listen on options.timeout : timeout for reaping process on idle in seconds options.content : root for web-pages to serve. """ from twisted.internet import reactor from twisted.web.server import Site from twisted.web.static import File import sys if not disableLogging: # redirect twisted logs to python standard logging. observer = log.PythonLoggingObserver() observer.start() # log.startLogging(sys.stdout) # Set logging level. if (options.debug): logging.basicConfig(level=logging.DEBUG) contextFactory = None use_SSL = False if options.sslKey and options.sslCert: use_SSL = True wsProtocol = "wss" from twisted.internet import ssl contextFactory = ssl.DefaultOpenSSLContextFactory( options.sslKey, options.sslCert) else: wsProtocol = "ws" # Create default or custom ServerProtocol wslinkServer = protocol() # create a wslink-over-WebSocket transport server factory transport_factory = wsl.TimeoutWebSocketServerFactory(\ url = "%s://%s:%d" % (wsProtocol, options.host, options.port), \ timeout = options.timeout ) transport_factory.protocol = wsl.WslinkWebSocketServerProtocol transport_factory.setServerProtocol(wslinkServer) root = Resource() # Do we serve static content or just websocket ? if len(options.content) > 0: # Static HTTP + WebSocket root = File(options.content) # Handle possibly complex ws endpoint if not options.nows: wsResource = WebSocketResource(transport_factory) handle_complex_resource_path(options.ws, root, wsResource) if options.uploadPath != None: from wslink.upload import UploadPage uploadResource = UploadPage(options.uploadPath) root.putChild("upload", uploadResource) if len(options.fsEndpoints) > 3: for fsResourceInfo in options.fsEndpoints.split('|'): infoSplit = fsResourceInfo.split('=') handle_complex_resource_path(infoSplit[0], root, File(infoSplit[1])) site = Site(root) if use_SSL: reactor.listenSSL(options.port, site, contextFactory) else: reactor.listenTCP(options.port, site) # flush ready line sys.stdout.flush() # Work around to force the output buffer to be flushed # This allow the process launcher to parse the output and # wait for "Start factory" to know that the WebServer # is running. if options.forceFlush: for i in range(200): log.msg("+" * 80, logLevel=logging.CRITICAL) # Initialize testing: checks if we're doing a test and sets it up # testing.initialize(options, reactor, stop_webserver) reactor.callWhenRunning(print_ready) # Start the reactor if options.nosignalhandlers: reactor.run(installSignalHandlers=0) else: reactor.run()
from twisted.internet import reactor from twisted.web.server import Site from twisted.web.static import File root = File('..') root.putChild("chapter-2", File(r"..\Chapter-2")) root.putChild("chapter-3", File(r"..\Chapter-3")) root.putChild("chapter-4", File(r"..\Chapter-4")) factory = Site(root) reactor.listenTCP(8000, factory) reactor.run()