本文整理汇总了Python中twisted.web.http_headers.Headers方法的典型用法代码示例。如果您正苦于以下问题:Python http_headers.Headers方法的具体用法?Python http_headers.Headers怎么用?Python http_headers.Headers使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.web.http_headers
的用法示例。
在下文中一共展示了http_headers.Headers方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _requestWithEndpoint
# 需要导入模块: from twisted.web import http_headers [as 别名]
# 或者: from twisted.web.http_headers import Headers [as 别名]
def _requestWithEndpoint(self, key, endpoint, method, parsedURI,
headers, bodyProducer, requestPath):
"""
Issue a new request, given the endpoint and the path sent as part of
the request.
"""
# Create minimal headers, if necessary:
if headers is None:
headers = Headers()
if not headers.hasHeader(b'host'):
headers = headers.copy()
headers.addRawHeader(
b'host', self._computeHostValue(parsedURI.scheme,
parsedURI.host,
parsedURI.port))
d = self._pool.getConnection(key, endpoint)
def cbConnected(proto):
return proto.request(
Request._construct(method, requestPath, headers, bodyProducer,
persistent=self._pool.persistent,
parsedURI=parsedURI))
d.addCallback(cbConnected)
return d
示例2: __init__
# 需要导入模块: from twisted.web import http_headers [as 别名]
# 或者: from twisted.web.http_headers import Headers [as 别名]
def __init__(self, uri):
"""
Create a fake Urllib2 request.
@param uri: Request URI.
@type uri: L{bytes}
"""
self.uri = nativeString(uri)
self.headers = Headers()
_uri = URI.fromBytes(uri)
self.type = nativeString(_uri.scheme)
self.host = nativeString(_uri.host)
if (_uri.scheme, _uri.port) not in ((b'http', 80), (b'https', 443)):
# If it's not a schema on the regular port, add the port.
self.host += ":" + str(_uri.port)
if _PY3:
self.origin_req_host = nativeString(_uri.host)
self.unverifiable = lambda _: False
示例3: __init__
# 需要导入模块: from twisted.web import http_headers [as 别名]
# 或者: from twisted.web.http_headers import Headers [as 别名]
def __init__(self, version, code, phrase, headers, _transport):
"""
@param version: HTTP version components protocol, major, minor. E.g.
C{(b'HTTP', 1, 1)} to mean C{b'HTTP/1.1'}.
@param code: HTTP status code.
@type code: L{int}
@param phrase: HTTP reason phrase, intended to give a short description
of the HTTP status code.
@param headers: HTTP response headers.
@type headers: L{twisted.web.http_headers.Headers}
@param _transport: The transport which is delivering this response.
"""
self.version = version
self.code = code
self.phrase = phrase
self.headers = headers
self._transport = _transport
self._bodyBuffer = []
self._state = 'INITIAL'
self.request = None
self.previousResponse = None
示例4: __init__
# 需要导入模块: from twisted.web import http_headers [as 别名]
# 或者: from twisted.web.http_headers import Headers [as 别名]
def __init__(self, channel, queued=_QUEUED_SENTINEL):
"""
@param channel: the channel we're connected to.
@param queued: (deprecated) are we in the request queue, or can we
start writing to the transport?
"""
self.notifications = []
self.channel = channel
self.requestHeaders = Headers()
self.received_cookies = {}
self.responseHeaders = Headers()
self.cookies = [] # outgoing cookies
self.transport = self.channel.transport
if queued is _QUEUED_SENTINEL:
queued = False
self.queued = queued
示例5: writeHeaders
# 需要导入模块: from twisted.web import http_headers [as 别名]
# 或者: from twisted.web.http_headers import Headers [as 别名]
def writeHeaders(self, version, code, reason, headers):
"""
Called by L{Request} objects to write a complete set of HTTP headers to
a transport.
@param version: The HTTP version in use.
@type version: L{bytes}
@param code: The HTTP status code to write.
@type code: L{bytes}
@param reason: The HTTP reason phrase to write.
@type reason: L{bytes}
@param headers: The headers to write to the transport.
@type headers: L{twisted.web.http_headers.Headers}
"""
responseLine = version + b" " + code + b" " + reason + b"\r\n"
headerSequence = [responseLine]
headerSequence.extend(
name + b': ' + value + b"\r\n" for name, value in headers
)
headerSequence.append(b"\r\n")
self.transport.writeSequence(headerSequence)
示例6: test_requestHeaders
# 需要导入模块: from twisted.web import http_headers [as 别名]
# 或者: from twisted.web.http_headers import Headers [as 别名]
def test_requestHeaders(self):
"""
The request headers are available on the request object passed to a
distributed resource's C{render} method.
"""
requestHeaders = {}
class ReportRequestHeaders(resource.Resource):
def render(self, request):
requestHeaders.update(dict(
request.requestHeaders.getAllRawHeaders()))
return ""
request = self._requestTest(
ReportRequestHeaders(), headers=Headers({'foo': ['bar']}))
def cbRequested(result):
self.assertEqual(requestHeaders['Foo'], ['bar'])
request.addCallback(cbRequested)
return request
示例7: test_headersUnmodified
# 需要导入模块: from twisted.web import http_headers [as 别名]
# 或者: from twisted.web.http_headers import Headers [as 别名]
def test_headersUnmodified(self):
"""
If a I{Host} header must be added to the request, the L{Headers}
instance passed to L{Agent.request} is not modified.
"""
headers = http_headers.Headers()
self.agent._getEndpoint = lambda *args: self
self.agent.request(
b'GET', b'http://example.com/foo', headers)
protocol = self.protocol
# The request should have been issued.
self.assertEqual(len(protocol.requests), 1)
# And the headers object passed in should not have changed.
self.assertEqual(headers, http_headers.Headers())
示例8: test_existingHeaders
# 需要导入模块: from twisted.web import http_headers [as 别名]
# 或者: from twisted.web.http_headers import Headers [as 别名]
def test_existingHeaders(self):
"""
If there are existing I{Accept-Encoding} fields,
L{client.ContentDecoderAgent} creates a new field for the decoders it
knows about.
"""
headers = http_headers.Headers({b'foo': [b'bar'],
b'accept-encoding': [b'fizz']})
agent = client.ContentDecoderAgent(
self.agent, [(b'decoder1', Decoder1), (b'decoder2', Decoder2)])
agent.request(b'GET', b'http://example.com/foo', headers=headers)
protocol = self.protocol
self.assertEqual(len(protocol.requests), 1)
req, res = protocol.requests.pop()
self.assertEqual(
list(sorted(req.headers.getAllRawHeaders())),
[(b'Accept-Encoding', [b'fizz', b'decoder1,decoder2']),
(b'Foo', [b'bar']),
(b'Host', [b'example.com'])])
示例9: test_plainEncodingResponse
# 需要导入模块: from twisted.web import http_headers [as 别名]
# 或者: from twisted.web.http_headers import Headers [as 别名]
def test_plainEncodingResponse(self):
"""
If the response is not encoded despited the request I{Accept-Encoding}
headers, L{client.ContentDecoderAgent} simply forwards the response.
"""
agent = client.ContentDecoderAgent(
self.agent, [(b'decoder1', Decoder1), (b'decoder2', Decoder2)])
deferred = agent.request(b'GET', b'http://example.com/foo')
req, res = self.protocol.requests.pop()
response = Response((b'HTTP', 1, 1), 200, b'OK', http_headers.Headers(),
None)
res.callback(response)
return deferred.addCallback(self.assertIdentical, response)
示例10: test_unknownEncoding
# 需要导入模块: from twisted.web import http_headers [as 别名]
# 或者: from twisted.web.http_headers import Headers [as 别名]
def test_unknownEncoding(self):
"""
When L{client.ContentDecoderAgent} encounters a decoder it doesn't know
about, it stops decoding even if another encoding is known afterwards.
"""
agent = client.ContentDecoderAgent(
self.agent, [(b'decoder1', Decoder1), (b'decoder2', Decoder2)])
deferred = agent.request(b'GET', b'http://example.com/foo')
req, res = self.protocol.requests.pop()
headers = http_headers.Headers({b'foo': [b'bar'],
b'content-encoding':
[b'decoder1,fizz,decoder2']})
response = Response((b'HTTP', 1, 1), 200, b'OK', headers, None)
res.callback(response)
def check(result):
self.assertNotIdentical(response, result)
self.assertIsInstance(result, Decoder2)
self.assertEqual([b'decoder1,fizz'],
result.headers.getRawHeaders(b'content-encoding'))
return deferred.addCallback(check)
示例11: test_noRedirect
# 需要导入模块: from twisted.web import http_headers [as 别名]
# 或者: from twisted.web.http_headers import Headers [as 别名]
def test_noRedirect(self):
"""
L{client.RedirectAgent} behaves like L{client.Agent} if the response
doesn't contain a redirect.
"""
deferred = self.agent.request(b'GET', b'http://example.com/foo')
req, res = self.protocol.requests.pop()
headers = http_headers.Headers()
response = Response((b'HTTP', 1, 1), 200, b'OK', headers, None)
res.callback(response)
self.assertEqual(0, len(self.protocol.requests))
result = self.successResultOf(deferred)
self.assertIdentical(response, result)
self.assertIdentical(result.previousResponse, None)
示例12: _testRedirectToGet
# 需要导入模块: from twisted.web import http_headers [as 别名]
# 或者: from twisted.web.http_headers import Headers [as 别名]
def _testRedirectToGet(self, code, method):
"""
L{client.RedirectAgent} changes the method to I{GET} when getting
a redirect on a non-I{GET} request.
@param code: HTTP status code.
@param method: HTTP request method.
"""
self.agent.request(method, b'http://example.com/foo')
req, res = self.protocol.requests.pop()
headers = http_headers.Headers(
{b'location': [b'http://example.com/bar']})
response = Response((b'HTTP', 1, 1), code, b'OK', headers, None)
res.callback(response)
req2, res2 = self.protocol.requests.pop()
self.assertEqual(b'GET', req2.method)
self.assertEqual(b'/bar', req2.uri)
示例13: test_noLocationField
# 需要导入模块: from twisted.web import http_headers [as 别名]
# 或者: from twisted.web.http_headers import Headers [as 别名]
def test_noLocationField(self):
"""
If no L{Location} header field is found when getting a redirect,
L{client.RedirectAgent} fails with a L{ResponseFailed} error wrapping a
L{error.RedirectWithNoLocation} exception.
"""
deferred = self.agent.request(b'GET', b'http://example.com/foo')
req, res = self.protocol.requests.pop()
headers = http_headers.Headers()
response = Response((b'HTTP', 1, 1), 301, b'OK', headers, None)
res.callback(response)
fail = self.failureResultOf(deferred, client.ResponseFailed)
fail.value.reasons[0].trap(error.RedirectWithNoLocation)
self.assertEqual(b'http://example.com/foo',
fail.value.reasons[0].value.uri)
self.assertEqual(301, fail.value.response.code)
示例14: _testPageRedirectFailure
# 需要导入模块: from twisted.web import http_headers [as 别名]
# 或者: from twisted.web.http_headers import Headers [as 别名]
def _testPageRedirectFailure(self, code, method):
"""
When getting a redirect on an unsupported request method,
L{client.RedirectAgent} fails with a L{ResponseFailed} error wrapping
a L{error.PageRedirect} exception.
@param code: HTTP status code.
@param method: HTTP request method.
"""
deferred = self.agent.request(method, b'http://example.com/foo')
req, res = self.protocol.requests.pop()
headers = http_headers.Headers()
response = Response((b'HTTP', 1, 1), code, b'OK', headers, None)
res.callback(response)
fail = self.failureResultOf(deferred, client.ResponseFailed)
fail.value.reasons[0].trap(error.PageRedirect)
self.assertEqual(b'http://example.com/foo',
fail.value.reasons[0].value.location)
self.assertEqual(code, fail.value.response.code)
示例15: _testRedirectURI
# 需要导入模块: from twisted.web import http_headers [as 别名]
# 或者: from twisted.web.http_headers import Headers [as 别名]
def _testRedirectURI(self, uri, location, finalURI):
"""
When L{client.RedirectAgent} encounters a relative redirect I{URI}, it
is resolved against the request I{URI} before following the redirect.
@param uri: Request URI.
@param location: I{Location} header redirect URI.
@param finalURI: Expected final URI.
"""
self.agent.request(b'GET', uri)
req, res = self.protocol.requests.pop()
headers = http_headers.Headers(
{b'location': [location]})
response = Response((b'HTTP', 1, 1), 302, b'OK', headers, None)
res.callback(response)
req2, res2 = self.protocol.requests.pop()
self.assertEqual(b'GET', req2.method)
self.assertEqual(finalURI, req2.absoluteURI)