Here are the examples of the python api twisted.web.wsgi.WSGIResource taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
7 Examples
3
Example 1
def __init__(self, pool):
"""
Setup the django+twisted resource.
Args:
pool (ThreadPool): The twisted threadpool.
"""
resource.Resource.__init__(self)
self.wsgi_resource = WSGIResource(reactor, pool, WSGIHandler())
3
Example 2
Project: lunar Source File: server.py
def run(self, app):
from twisted.web import server, wsgi
from twisted.internet import reactor
resource = wsgi.WSGIResource(reactor, reactor.getThreadPool(), app)
server = server.Site(resource)
reactor.listenTCP(port=self.port, factory=server, interface=self.host)
reactor.run()
3
Example 3
Project: SubliminalCollaborator Source File: tap.py
def opt_wsgi(self, name):
"""
The FQPN of a WSGI application object to serve as the root resource of
the webserver.
"""
pool = threadpool.ThreadPool()
reactor.callWhenRunning(pool.start)
reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
try:
application = reflect.namedAny(name)
except (AttributeError, ValueError):
raise usage.UsageError("No such WSGI application: %r" % (name,))
self['root'] = wsgi.WSGIResource(reactor, pool, application)
0
Example 4
Project: yabgp Source File: __init__.py
def prepare_twisted_service():
LOG.info('Prepare twisted services')
# check all peers
all_peers = {}
# check running mode
if not CONF.standalone:
# rabbitmq factory
rabbit_mq_factory = PikaFactory(url=CONF.rabbit_mq.rabbit_url)
rabbit_mq_factory.peer_list = CONF.bgp.running_config.keys()
rabbit_mq_factory.connect()
# mongodb connection
if CONF.database.use_replica:
mongo_connection = MongoApi(
connection_url=CONF.database.connection,
db_name=CONF.database.dbname,
use_replica=CONF.database.use_replica,
replica_name=CONF.database.replica_name,
read_preference=CONF.database.read_preference,
write_concern=CONF.database.write_concern,
w_timeout=CONF.database.write_concern_timeout
)
else:
mongo_connection = MongoApi(connection_url=CONF.database.connection, db_name=CONF.database.dbname)
# check api bind host
if CONF.rest.bind_host == '0.0.0.0':
LOG.error('please use the exactly rest host ip address when not running in standalone mode')
sys.exit()
# TODO load channel filter and peer policy
else:
rabbit_mq_factory = None
mongo_connection = None
for peer in CONF.bgp.running_config:
LOG.info('Get peer %s configuration', peer)
if not CONF.standalone:
if CONF.bgp.running_config[peer]['local_addr'] == '0.0.0.0':
LOG.error('please use the exactly local bgp ip address when not running in standalone mode')
sys.exit()
if CONF.message.write_disk:
msg_file_path_for_peer = os.path.join(
CONF.message.write_dir,
peer.lower()
)
if not os.path.exists(msg_file_path_for_peer):
os.makedirs(msg_file_path_for_peer)
LOG.info('Create dir %s for peer %s', msg_file_path_for_peer, peer)
LOG.info('BGP message file path is %s', msg_file_path_for_peer)
else:
msg_file_path_for_peer = None
LOG.info('Create BGPPeering instance')
afi_safi_list = [bgp_cons.AFI_SAFI_STR_DICT[afi_safi]
for afi_safi in CONF.bgp.running_config[peer]['afi_safi']]
CONF.bgp.running_config[peer]['afi_safi'] = afi_safi_list
CONF.bgp.running_config[peer]['capability']['local']['afi_safi'] = afi_safi_list
bgp_peering = BGPPeering(
myasn=CONF.bgp.running_config[peer]['local_as'],
myaddr=CONF.bgp.running_config[peer]['local_addr'],
peerasn=CONF.bgp.running_config[peer]['remote_as'],
peeraddr=CONF.bgp.running_config[peer]['remote_addr'],
tag=CONF.bgp.running_config[peer]['tag'],
afisafi=CONF.bgp.running_config[peer]['afi_safi'],
msgpath=msg_file_path_for_peer,
md5=CONF.bgp.running_config[peer]['md5'],
channel=rabbit_mq_factory,
mongo_conn=mongo_connection
)
all_peers[peer] = bgp_peering
CONF.bgp.running_config[peer]['factory'] = bgp_peering
# register to database and check agent role
if not CONF.standalone:
register_to_db(peer_ip=peer, mongo_api=mongo_connection)
if not CONF.bgp.running_config[peer]['tag']:
LOG.error('Please point out the role tag(SRC,DST or BOTH)for not running in standalone mode')
sys.exit()
load_channel_filter_from_db(peer_ip=peer, mongo_api=mongo_connection)
# Starting api server
if sys.version_info[0] == 2:
from twisted.web.wsgi import WSGIResource
LOG.info("Prepare RESTAPI service")
resource = WSGIResource(reactor, reactor.getThreadPool(), app)
site = Site(resource)
try:
reactor.listenTCP(CONF.rest.bind_port, site, interface=CONF.rest.bind_host)
LOG.info("serving RESTAPI on http://%s:%s", CONF.rest.bind_host, CONF.rest.bind_port)
except Exception as e:
LOG.error(e, exc_info=True)
sys.exit()
for peer in all_peers:
LOG.info('start peer, peer address=%s', peer)
all_peers[peer].automatic_start()
reactor.run()
0
Example 5
Project: crossbar Source File: router.py
def _create_resource(self, path_config, nested=True):
"""
Creates child resource to be added to the parent.
:param path_config: Configuration for the new child resource.
:type path_config: dict
:returns: Resource -- the new child resource
"""
# WAMP-WebSocket resource
#
if path_config['type'] == 'websocket':
ws_factory = WampWebSocketServerFactory(self._router_session_factory, self.config.extra.cbdir, path_config, self._templates)
# FIXME: Site.start/stopFactory should start/stop factories wrapped as Resources
ws_factory.startFactory()
return WebSocketResource(ws_factory)
# Static file hierarchy resource
#
elif path_config['type'] == 'static':
static_options = path_config.get('options', {})
if 'directory' in path_config:
static_dir = os.path.abspath(os.path.join(self.config.extra.cbdir, path_config['directory']))
elif 'package' in path_config:
if 'resource' not in path_config:
raise ApplicationError(u"crossbar.error.invalid_configuration", "missing resource")
try:
mod = importlib.import_module(path_config['package'])
except ImportError as e:
emsg = "Could not import resource {} from package {}: {}".format(path_config['resource'], path_config['package'], e)
self.log.error(emsg)
raise ApplicationError(u"crossbar.error.invalid_configuration", emsg)
else:
try:
static_dir = os.path.abspath(pkg_resources.resource_filename(path_config['package'], path_config['resource']))
except Exception as e:
emsg = "Could not import resource {} from package {}: {}".format(path_config['resource'], path_config['package'], e)
self.log.error(emsg)
raise ApplicationError(u"crossbar.error.invalid_configuration", emsg)
else:
raise ApplicationError(u"crossbar.error.invalid_configuration", "missing web spec")
static_dir = static_dir.encode('ascii', 'ignore') # http://stackoverflow.com/a/20433918/884770
# create resource for file system hierarchy
#
if static_options.get('enable_directory_listing', False):
static_resource_class = StaticResource
else:
static_resource_class = StaticResourceNoListing
cache_timeout = static_options.get('cache_timeout', DEFAULT_CACHE_TIMEOUT)
static_resource = static_resource_class(static_dir, cache_timeout=cache_timeout)
# set extra MIME types
#
static_resource.contentTypes.update(EXTRA_MIME_TYPES)
if 'mime_types' in static_options:
static_resource.contentTypes.update(static_options['mime_types'])
patchFileContentTypes(static_resource)
# render 404 page on any concrete path not found
#
static_resource.childNotFound = Resource404(self._templates, static_dir)
return static_resource
# WSGI resource
#
elif path_config['type'] == 'wsgi':
if not _HAS_WSGI:
raise ApplicationError(u"crossbar.error.invalid_configuration", "WSGI unsupported")
if 'module' not in path_config:
raise ApplicationError(u"crossbar.error.invalid_configuration", "missing WSGI app module")
if 'object' not in path_config:
raise ApplicationError(u"crossbar.error.invalid_configuration", "missing WSGI app object")
# import WSGI app module and object
mod_name = path_config['module']
try:
mod = importlib.import_module(mod_name)
except ImportError as e:
raise ApplicationError(u"crossbar.error.invalid_configuration", "WSGI app module '{}' import failed: {} - Python search path was {}".format(mod_name, e, sys.path))
else:
obj_name = path_config['object']
if obj_name not in mod.__dict__:
raise ApplicationError(u"crossbar.error.invalid_configuration", "WSGI app object '{}' not in module '{}'".format(obj_name, mod_name))
else:
app = getattr(mod, obj_name)
# Create a threadpool for running the WSGI requests in
pool = ThreadPool(maxthreads=path_config.get("maxthreads", 20),
minthreads=path_config.get("minthreads", 0),
name="crossbar_wsgi_threadpool")
self._reactor.addSystemEventTrigger('before', 'shutdown', pool.stop)
pool.start()
# Create a Twisted Web WSGI resource from the user's WSGI application object
try:
wsgi_resource = WSGIResource(self._reactor, pool, app)
if not nested:
wsgi_resource = WSGIRootResource(wsgi_resource, {})
except Exception as e:
raise ApplicationError(u"crossbar.error.invalid_configuration", "could not instantiate WSGI resource: {}".format(e))
else:
return wsgi_resource
# Redirecting resource
#
elif path_config['type'] == 'redirect':
redirect_url = path_config['url'].encode('ascii', 'ignore')
return RedirectResource(redirect_url)
# Reverse proxy resource
#
elif path_config['type'] == 'reverseproxy':
host = path_config['host']
port = int(path_config.get('port', 80))
path = path_config.get('path', '').encode('ascii', 'ignore')
return ReverseProxyResource(host, port, path)
# JSON value resource
#
elif path_config['type'] == 'json':
value = path_config['value']
return JsonResource(value)
# CGI script resource
#
elif path_config['type'] == 'cgi':
cgi_processor = path_config['processor']
cgi_directory = os.path.abspath(os.path.join(self.config.extra.cbdir, path_config['directory']))
cgi_directory = cgi_directory.encode('ascii', 'ignore') # http://stackoverflow.com/a/20433918/884770
return CgiDirectory(cgi_directory, cgi_processor, Resource404(self._templates, cgi_directory))
# WAMP-Longpoll transport resource
#
elif path_config['type'] == 'longpoll':
path_options = path_config.get('options', {})
lp_resource = WampLongPollResource(self._router_session_factory,
timeout=path_options.get('request_timeout', 10),
killAfter=path_options.get('session_timeout', 30),
queueLimitBytes=path_options.get('queue_limit_bytes', 128 * 1024),
queueLimitMessages=path_options.get('queue_limit_messages', 100),
debug_transport_id=path_options.get('debug_transport_id', None)
)
lp_resource._templates = self._templates
return lp_resource
# Publisher resource (part of REST-bridge)
#
elif path_config['type'] == 'publisher':
# create a vanilla session: the publisher will use this to inject events
#
publisher_session_config = ComponentConfig(realm=path_config['realm'], extra=None)
publisher_session = ApplicationSession(publisher_session_config)
# add the publisher session to the router
#
self._router_session_factory.add(publisher_session, authrole=path_config.get('role', 'anonymous'))
# now create the publisher Twisted Web resource
#
return PublisherResource(path_config.get('options', {}), publisher_session)
# Webhook resource (part of REST-bridge)
#
elif path_config['type'] == 'webhook':
# create a vanilla session: the webhook will use this to inject events
#
webhook_session_config = ComponentConfig(realm=path_config['realm'], extra=None)
webhook_session = ApplicationSession(webhook_session_config)
# add the webhook session to the router
#
self._router_session_factory.add(webhook_session, authrole=path_config.get('role', 'anonymous'))
# now create the webhook Twisted Web resource
#
return WebhookResource(path_config.get('options', {}), webhook_session)
# Caller resource (part of REST-bridge)
#
elif path_config['type'] == 'caller':
# create a vanilla session: the caller will use this to inject calls
#
caller_session_config = ComponentConfig(realm=path_config['realm'], extra=None)
caller_session = ApplicationSession(caller_session_config)
# add the calling session to the router
#
self._router_session_factory.add(caller_session, authrole=path_config.get('role', 'anonymous'))
# now create the caller Twisted Web resource
#
return CallerResource(path_config.get('options', {}), caller_session)
# File Upload resource
#
elif path_config['type'] == 'upload':
upload_directory = os.path.abspath(os.path.join(self.config.extra.cbdir, path_config['directory']))
upload_directory = upload_directory.encode('ascii', 'ignore') # http://stackoverflow.com/a/20433918/884770
if not os.path.isdir(upload_directory):
emsg = "configured upload directory '{}' in file upload resource isn't a directory".format(upload_directory)
self.log.error(emsg)
raise ApplicationError(u"crossbar.error.invalid_configuration", emsg)
if 'temp_directory' in path_config:
temp_directory = os.path.abspath(os.path.join(self.config.extra.cbdir, path_config['temp_directory']))
temp_directory = temp_directory.encode('ascii', 'ignore') # http://stackoverflow.com/a/20433918/884770
else:
temp_directory = os.path.abspath(tempfile.gettempdir())
temp_directory = os.path.join(temp_directory, 'crossbar-uploads')
if not os.path.exists(temp_directory):
os.makedirs(temp_directory)
if not os.path.isdir(temp_directory):
emsg = "configured temp directory '{}' in file upload resource isn't a directory".format(temp_directory)
self.log.error(emsg)
raise ApplicationError(u"crossbar.error.invalid_configuration", emsg)
# file upload progress and finish events are published via this session
#
upload_session_config = ComponentConfig(realm=path_config['realm'], extra=None)
upload_session = ApplicationSession(upload_session_config)
self._router_session_factory.add(upload_session, authrole=path_config.get('role', 'anonymous'))
self.log.info("File upload resource started. Uploads to {upl} using temp folder {tmp}.", upl=upload_directory, tmp=temp_directory)
return FileUploadResource(upload_directory, temp_directory, path_config['form_fields'], upload_session, path_config.get('options', {}))
# Generic Twisted Web resource
#
elif path_config['type'] == 'resource':
try:
klassname = path_config['classname']
self.log.debug("Starting class '{name}'", name=klassname)
c = klassname.split('.')
module_name, klass_name = '.'.join(c[:-1]), c[-1]
module = importlib.import_module(module_name)
make = getattr(module, klass_name)
return make(path_config.get('extra', {}))
except Exception as e:
emsg = "Failed to import class '{}' - {}".format(klassname, e)
self.log.error(emsg)
self.log.error("PYTHONPATH: {pythonpath}", pythonpath=sys.path)
raise ApplicationError(u"crossbar.error.class_import_failed", emsg)
# Schema Docs resource
#
elif path_config['type'] == 'schemadoc':
realm = path_config['realm']
if realm not in self.realm_to_id:
raise ApplicationError(u"crossbar.error.no_such_object", "No realm with URI '{}' configured".format(realm))
realm_id = self.realm_to_id[realm]
realm_schemas = self.realms[realm_id].session._schemas
return SchemaDocResource(self._templates, realm, realm_schemas)
# Nested subpath resource
#
elif path_config['type'] == 'path':
nested_paths = path_config.get('paths', {})
if '/' in nested_paths:
nested_resource = self._create_resource(nested_paths['/'])
else:
nested_resource = Resource404(self._templates, b'')
# nest subpaths under the current entry
#
self._add_paths(nested_resource, nested_paths)
return nested_resource
else:
raise ApplicationError(u"crossbar.error.invalid_configuration",
"invalid Web path type '{}' in {} config".format(path_config['type'],
'nested' if nested else 'root'))
0
Example 6
Project: crochet Source File: fromtwisted.py
def main():
log.startLogging(sys.stdout)
pool = reactor.getThreadPool()
reactor.listenTCP(5000, Site(WSGIResource(reactor, pool, application)))
reactor.run()
0
Example 7
def listen(_reactor, base_port=BASE_PORT, retry_cache=None):
# in likelihood there is no benefit to passing in the reactor as only one of
# them can ever run at a time.
retry_cache = retry_cache or {}
retries_app = create_retries_app(retry_cache)
sleep_resource = WSGIResource(reactor, reactor.getThreadPool(), sleep_app)
sleep_site = HammsSite(sleep_resource)
status_resource = WSGIResource(reactor, reactor.getThreadPool(), status_app)
status_site = HammsSite(status_resource)
large_header_resource = WSGIResource(reactor, reactor.getThreadPool(),
large_header_app)
large_header_site = HammsSite(large_header_resource)
retries_resource = WSGIResource(reactor, reactor.getThreadPool(), retries_app)
retries_site = HammsSite(retries_resource)
unparseable_resource = WSGIResource(reactor, reactor.getThreadPool(),
unparseable_app)
unparseable_site = HammsSite(unparseable_resource)
toolong_content_resource = WSGIResource(reactor, reactor.getThreadPool(),
toolong_content_app)
toolong_content_site = HammsSite(toolong_content_resource)
_reactor.listenTCP(base_port + ListenForeverServer.PORT, ListenForeverFactory())
_reactor.listenTCP(base_port + EmptyStringTerminateImmediatelyServer.PORT,
EmptyStringTerminateImmediatelyFactory())
_reactor.listenTCP(base_port + EmptyStringTerminateOnReceiveServer.PORT,
EmptyStringTerminateOnReceiveFactory())
_reactor.listenTCP(base_port + MalformedStringTerminateImmediatelyServer.PORT,
MalformedStringTerminateImmediatelyFactory())
_reactor.listenTCP(base_port + MalformedStringTerminateOnReceiveServer.PORT,
MalformedStringTerminateOnReceiveFactory())
_reactor.listenTCP(base_port + FiveSecondByteResponseServer.PORT,
FiveSecondByteResponseFactory())
_reactor.listenTCP(base_port + ThirtySecondByteResponseServer.PORT,
ThirtySecondByteResponseFactory())
_reactor.listenTCP(base_port + sleep_app.PORT, sleep_site)
_reactor.listenTCP(base_port + status_app.PORT, status_site)
_reactor.listenTCP(base_port + SendDataPastContentLengthServer.PORT,
SendDataPastContentLengthFactory())
_reactor.listenTCP(base_port + large_header_app.PORT, large_header_site)
_reactor.listenTCP(base_port + retries_app.PORT, retries_site)
_reactor.listenTCP(base_port + DropRandomRequestsServer.PORT,
DropRandomRequestsFactory())
_reactor.listenTCP(base_port + unparseable_app.PORT, unparseable_site)
_reactor.listenTCP(base_port + IncompleteResponseServer.PORT,
IncompleteResponseFactory())
_reactor.listenTCP(base_port + toolong_content_app.PORT, toolong_content_site)