本文整理汇总了Python中twisted.web.server.Site.getResourceFor方法的典型用法代码示例。如果您正苦于以下问题:Python Site.getResourceFor方法的具体用法?Python Site.getResourceFor怎么用?Python Site.getResourceFor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.web.server.Site
的用法示例。
在下文中一共展示了Site.getResourceFor方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getResourceFor
# 需要导入模块: from twisted.web.server import Site [as 别名]
# 或者: from twisted.web.server.Site import getResourceFor [as 别名]
def getResourceFor(self, request):
"Fetch a resource and measure the time until it's finished writing."
resource = Site.getResourceFor(self, request)
now = time.time()
request.requestHeaders.addRawHeader('X-Request-Started-At', '%0.8f' % now)
request.notifyFinish().addBoth(self._recordRequestTime, now, request)
self._activeRequests.add(request)
return resource
示例2: getResourceFor
# 需要导入模块: from twisted.web.server import Site [as 别名]
# 或者: from twisted.web.server.Site import getResourceFor [as 别名]
def getResourceFor(self, request):
userpass = self.check_userpass_auth(self, request)
if userpass == AUTH_SUCCESS:
return PurgeAuthCredentials()
cookie = self.check_cookie_auth(self, request)
if cookie == AUTH_SUCCESS:
return Site.getResourceFor(self, request)
return RequestAuthenticationResource()
示例3: getResourceFor
# 需要导入模块: from twisted.web.server import Site [as 别名]
# 或者: from twisted.web.server.Site import getResourceFor [as 别名]
def getResourceFor(self, request):
"Fetch a resource and measure the time until it's finished writing."
resource = Site.getResourceFor(self, request)
if self.endpointFunc is None:
endpoint = None
else:
endpoint, _ = self.endpointFunc(request.path)
request.notifyFinish().addBoth(
self._recordRequestTime, time.time(), endpoint, request)
self._activeRequests.add(request)
return resource
示例4: getResourceFor
# 需要导入模块: from twisted.web.server import Site [as 别名]
# 或者: from twisted.web.server.Site import getResourceFor [as 别名]
def getResourceFor(self, request):
# Scan well-known search paths first
search_paths = ["build-out", "build-gen"]
for search_path in search_paths:
resource = self.resource
prepath = copy.copy(request.prepath)
postpath = copy.copy(request.postpath)
postpath.insert(0, search_path)
while postpath and not resource.isLeaf:
path_element = postpath.pop(0)
prepath.append(path_element)
resource = resource.getChildWithDefault(path_element, request)
if resource and not isinstance(resource, NoResource):
return resource
# Fallback to normal handling
return Site.getResourceFor(self, request)
示例5: getResourceFor
# 需要导入模块: from twisted.web.server import Site [as 别名]
# 或者: from twisted.web.server.Site import getResourceFor [as 别名]
def getResourceFor(self, request):
"Override Site so we can track active requests"
if request.requestHeaders.hasHeader('user-agent'):
ua = ' '.join(request.requestHeaders.getRawHeaders('user-agent'))
print('{}: Serving request to User-Agent "{}".'.format(time.asctime(), ua))
else:
print('{}: Serving request with no incoming User-Agent header.'.format(time.asctime()))
# track requsts currently being serviced, so we can nicely
# shut them off
self.active_requests.add(request)
request.notifyFinish().addBoth(self._forget_request, request)
# see if we've reached the maximum requests
self._request_count += 1
if self._max_requests is not None:
if self._request_count >= self._max_requests:
d = self.gracefully_stop()
d.addBoth(lambda x: reactor.stop())
# call through to parent
return Site.getResourceFor(self, request)
示例6: getResourceFor
# 需要导入模块: from twisted.web.server import Site [as 别名]
# 或者: from twisted.web.server.Site import getResourceFor [as 别名]
def getResourceFor(self, request):
request.setHeader('Server', SERVER_HEADER)
return Site.getResourceFor(self, request)
示例7: getResourceFor
# 需要导入模块: from twisted.web.server import Site [as 别名]
# 或者: from twisted.web.server.Site import getResourceFor [as 别名]
def getResourceFor(self, request):
request.setHeader('Server', 'MUDSling/%s' % mudsling.version)
return Site.getResourceFor(self, request)
示例8: getResourceFor
# 需要导入模块: from twisted.web.server import Site [as 别名]
# 或者: from twisted.web.server.Site import getResourceFor [as 别名]
def getResourceFor(self, request):
"""
Remove the server header from the response.
"""
request.responseHeaders.removeHeader('server')
return Site.getResourceFor(self, request)
示例9: TestClient
# 需要导入模块: from twisted.web.server import Site [as 别名]
# 或者: from twisted.web.server.Site import getResourceFor [as 别名]
class TestClient(object):
def __init__(self, resource):
self.site = Site(resource)
def get(self, path_with_params, status=None):
return self.request(path_with_params, method='GET', status=status)
def post(self, path_with_params, content=None, content_type=None, status=None):
return self.request(path_with_params, method='POST', content=content,
content_type=content_type, status=status)
def delete(self, path_with_params, status=None):
return self.request(path_with_params, method='DELETE', status=status)
def patch(self, path_with_params, content=None, content_type=None, status=None):
return self.request(path_with_params, method='PATCH', content=content,
content_type=content_type, status=None)
def put(self, path_with_params, content=None, content_type=None, status=None):
return self.request(path_with_params, method='PUT', content=content,
content_type=content_type, status=status)
def head(self, path_with_params, status=None):
return self.request(path_with_params, method='HEAD', status=status)
def options(self, path_with_params, status=None):
return self.request(path_with_params, method='OPTIONS', status=status)
@defer.inlineCallbacks
def request(self, req_or_path_with_params, status=None, **request_kwargs):
if isinstance(req_or_path_with_params, TestRequest):
req = req_or_path_with_params
else:
path, params = self.parse_url(req_or_path_with_params)
req = TestRequest(path, params=params, **request_kwargs)
resp = yield self._handle_request(req)
if status is not None:
assert resp.status_code == status, resp.status_code
defer.returnValue(resp)
def _handle_request(self, request):
''' Resolves a test request. '''
finished = request.notifyFinish()
def extract_response(none):
return TestResponse(request.responseCode or 200, request.responseHeaders,
''.join(request.written))
def _render(resource):
result = resource.render(request)
if isinstance(result, str):
request.write(result)
request.finish()
return finished
elif result is NOT_DONE_YET:
return finished
else:
raise ValueError("Unexpected return value: %r" % (result,))
resource_handler = self.site.getResourceFor(request)
d = _render(resource_handler)
d.addCallback(extract_response)
return d
def parse_url(self, path_with_params):
parts = path_with_params.split('?')
if len(parts) == 2:
path, param_str = parts
params = parse_qs(param_str)
else:
path, = parts
params = {}
return path, params
示例10: getResourceFor
# 需要导入模块: from twisted.web.server import Site [as 别名]
# 或者: from twisted.web.server.Site import getResourceFor [as 别名]
def getResourceFor(self, request):
request.setHeader('X-Frame-Options', 'DENY')
request.setHeader('X-XSS-Protection', '1')
request.setHeader('X-Content-Type-Options','nosniff')
request.setHeader('Server', 'None')
return Site.getResourceFor(self, request)
示例11: getResourceFor
# 需要导入模块: from twisted.web.server import Site [as 别名]
# 或者: from twisted.web.server.Site import getResourceFor [as 别名]
def getResourceFor(self, request):
request.setHeader('server', 'PYPDDemo/1.0')
request.setHeader('cache-control: ', 'cache-control: private, max-age=0, no-cache')
return Site.getResourceFor(self, request)
示例12: getResourceFor
# 需要导入模块: from twisted.web.server import Site [as 别名]
# 或者: from twisted.web.server.Site import getResourceFor [as 别名]
def getResourceFor(self, request):
self.cnt += 1
return Site.getResourceFor(self, request)