本文整理汇总了Python中twisted.web.http_headers.Headers.setRawHeaders方法的典型用法代码示例。如果您正苦于以下问题:Python Headers.setRawHeaders方法的具体用法?Python Headers.setRawHeaders怎么用?Python Headers.setRawHeaders使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.web.http_headers.Headers
的用法示例。
在下文中一共展示了Headers.setRawHeaders方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: request
# 需要导入模块: from twisted.web.http_headers import Headers [as 别名]
# 或者: from twisted.web.http_headers.Headers import setRawHeaders [as 别名]
def request(self, url, method='GET', data=None):
d = defer.Deferred()
_headers = {}
headers = None
if method == 'GET':
data = None
elif method == 'POST':
_headers['Content-Type'] = 'application/x-www-form-urlencoded'
else:
d.errback(Exception('Not Support Method:%s.' % method))
return
if _headers:
headers = Headers()
for k, v in _headers.iteritems():
headers.setRawHeaders(urllib.quote(k), [urllib.quote(v)])
if data:
data = StrBodyProducer(data)
#print 'Req url:', url, ' Head:', headers
self.agent.request(
method,
url,
headers,
data).addCallback(self.received, d).addErrback(
self.errback, d)
return d
示例2: test_copy
# 需要导入模块: from twisted.web.http_headers import Headers [as 别名]
# 或者: from twisted.web.http_headers.Headers import setRawHeaders [as 别名]
def test_copy(self):
"""
L{Headers.copy} creates a new independent copy of an existing
L{Headers} instance, allowing future modifications without impacts
between the copies.
"""
h = Headers()
h.setRawHeaders(u'test\u00E1', [u'foo\u2603'])
i = h.copy()
# The copy contains the same value as the original
self.assertEqual(i.getRawHeaders(u'test\u00E1'), [u'foo\u2603'])
self.assertEqual(i.getRawHeaders(b'test\xe1'), [b'foo\xe2\x98\x83'])
# Add a header to the original
h.addRawHeader(u'test\u00E1', u'bar')
# Verify that the copy has not changed
self.assertEqual(i.getRawHeaders(u'test\u00E1'), [u'foo\u2603'])
self.assertEqual(i.getRawHeaders(b'test\xe1'), [b'foo\xe2\x98\x83'])
# Add a header to the copy
i.addRawHeader(u'test\u00E1', b'baz')
# Verify that the orignal does not have it
self.assertEqual(
h.getRawHeaders(u'test\u00E1'), [u'foo\u2603', u'bar'])
self.assertEqual(
h.getRawHeaders(b'test\xe1'), [b'foo\xe2\x98\x83', b'bar'])
示例3: request
# 需要导入模块: from twisted.web.http_headers import Headers [as 别名]
# 或者: from twisted.web.http_headers.Headers import setRawHeaders [as 别名]
def request(rq):
headers = Headers(dict((k, [v]) for k, v in rq.headers.iteritems()))
scheme, host, port, path = _parse(rq.url)
headers.setRawHeaders('Host', [host])
return (rq.method, rq.url,
headers,
StringProducer(rq.data) if rq.data else None)
示例4: request
# 需要导入模块: from twisted.web.http_headers import Headers [as 别名]
# 或者: from twisted.web.http_headers.Headers import setRawHeaders [as 别名]
def request(self, method, path, headers=None, body=None, expected_code=200,
retry=0, delay=0):
"""
Send an http request to the gerrit service for the given path.
If 'retry' is specified, transient errors (http response code 500-599) will
be retried after an exponentially-increasing delay.
Returns a Deferred which will call back with the parsed json body of the
gerrit server's response.
Args:
method: 'GET', 'POST', etc.
path: Path element of the url.
headers: dict of http request headers.
body: json-encodable body of http request.
expected_code: http response code expected in reply.
retry: How many times to retry transient errors.
delay: Wait this many seconds before sending the request.
"""
retry_delay = delay * 2 if delay else 0.5
retry_args = (
method, path, headers, body, expected_code, retry - 1, retry_delay)
if not path.startswith('/'):
path = '/' + path
if not headers:
headers = Headers()
else:
# Make a copy so mutations don't affect retry attempts.
headers = Headers(dict(headers.getAllRawHeaders()))
if self.auth_token:
if not path.startswith('/a/'):
path = '/a' + path
headers.setRawHeaders('Authorization', [self.auth_token])
url = '%s://%s%s' % (self.gerrit_protocol, self.gerrit_host, path)
if body:
body = JsonBodyProducer(json.dumps(body))
headers.setRawHeaders('Content-Type', ['application/json'])
if DEBUG:
log.msg(url)
if delay:
d = defer.succeed(None)
d.addCallback(
reactor.callLater, delay, Agent.request, self, method, str(url),
headers, body)
else:
d = Agent.request(self, method, str(url), headers, body)
def _check_code(response):
if response.code == expected_code:
return response
if retry > 0 and response.code >= 500 and response.code < 600:
return self.request(*retry_args)
msg = 'Failed gerrit request (code %s, expected %s): %s' % (
response.code, expected_code, url)
raise GerritError(msg, response.code)
d.addCallback(_check_code)
d.addCallback(JsonResponse.Get, url=url)
return d
示例5: test_hasHeaderTrue
# 需要导入模块: from twisted.web.http_headers import Headers [as 别名]
# 或者: from twisted.web.http_headers.Headers import setRawHeaders [as 别名]
def test_hasHeaderTrue(self):
"""
Check that L{Headers.hasHeader} returns C{True} when the given header
is found.
"""
h = Headers()
h.setRawHeaders("test", ["lemur"])
self.assertTrue(h.hasHeader("test"))
self.assertTrue(h.hasHeader("Test"))
示例6: test_getRawHeaders
# 需要导入模块: from twisted.web.http_headers import Headers [as 别名]
# 或者: from twisted.web.http_headers.Headers import setRawHeaders [as 别名]
def test_getRawHeaders(self):
"""
L{Headers.getRawHeaders} returns the values which have been set for a
given header.
"""
h = Headers()
h.setRawHeaders("test", ["lemur"])
self.assertEqual(h.getRawHeaders("test"), ["lemur"])
self.assertEqual(h.getRawHeaders("Test"), ["lemur"])
示例7: test_rawHeadersValueEncoding
# 需要导入模块: from twisted.web.http_headers import Headers [as 别名]
# 或者: from twisted.web.http_headers.Headers import setRawHeaders [as 别名]
def test_rawHeadersValueEncoding(self):
"""
Passing L{unicode} to L{Headers.setRawHeaders} will encode the name as
ISO-8859-1 and values as UTF-8.
"""
h = Headers()
h.setRawHeaders(u"\u00E1", [u"\u2603", b"foo"])
self.assertTrue(h.hasHeader(b"\xe1"))
self.assertEqual(h.getRawHeaders(b"\xe1"), [b'\xe2\x98\x83', b'foo'])
示例8: headers
# 需要导入模块: from twisted.web.http_headers import Headers [as 别名]
# 或者: from twisted.web.http_headers.Headers import setRawHeaders [as 别名]
def headers(self, **kw):
"""
Create a L{Headers} instance populated with the header name/values
specified by C{kw} and a L{_DictHeaders} wrapped around it and return
them both.
"""
h = Headers()
for k, v in kw.iteritems():
h.setRawHeaders(k, v)
return h, _DictHeaders(h)
示例9: test_getRawHeadersWithDefaultMatchingValue
# 需要导入模块: from twisted.web.http_headers import Headers [as 别名]
# 或者: from twisted.web.http_headers.Headers import setRawHeaders [as 别名]
def test_getRawHeadersWithDefaultMatchingValue(self):
"""
If the object passed as the value list to L{Headers.setRawHeaders}
is later passed as a default to L{Headers.getRawHeaders}, the
result nevertheless contains encoded values.
"""
h = Headers()
default = [u"value"]
h.setRawHeaders(b"key", default)
self.assertIsInstance(h.getRawHeaders(b"key", default)[0], bytes)
self.assertEqual(h.getRawHeaders(b"key", default), [b"value"])
示例10: test_hasHeaderTrue
# 需要导入模块: from twisted.web.http_headers import Headers [as 别名]
# 或者: from twisted.web.http_headers.Headers import setRawHeaders [as 别名]
def test_hasHeaderTrue(self):
"""
Check that L{Headers.hasHeader} returns C{True} when the given header
is found.
"""
h = Headers()
h.setRawHeaders(u"test\u00E1", [u"lemur"])
self.assertTrue(h.hasHeader(u"test\u00E1"))
self.assertTrue(h.hasHeader(u"Test\u00E1"))
self.assertTrue(h.hasHeader(b"test\xe1"))
self.assertTrue(h.hasHeader(b"Test\xe1"))
示例11: test_setRawHeaders
# 需要导入模块: from twisted.web.http_headers import Headers [as 别名]
# 或者: from twisted.web.http_headers.Headers import setRawHeaders [as 别名]
def test_setRawHeaders(self):
"""
L{Headers.setRawHeaders} sets the header values for the given
header name to the sequence of string values.
"""
rawValue = ["value1", "value2"]
h = Headers()
h.setRawHeaders("test", rawValue)
self.assertTrue(h.hasHeader("test"))
self.assertTrue(h.hasHeader("Test"))
self.assertEqual(h.getRawHeaders("test"), rawValue)
示例12: test_getRawHeaders
# 需要导入模块: from twisted.web.http_headers import Headers [as 别名]
# 或者: from twisted.web.http_headers.Headers import setRawHeaders [as 别名]
def test_getRawHeaders(self):
"""
L{Headers.getRawHeaders} returns the values which have been set for a
given header.
"""
h = Headers()
h.setRawHeaders(u"test\u00E1", [u"lemur"])
self.assertEqual(h.getRawHeaders(u"test\u00E1"), [u"lemur"])
self.assertEqual(h.getRawHeaders(u"Test\u00E1"), [u"lemur"])
self.assertEqual(h.getRawHeaders(b"test\xe1"), [b"lemur"])
self.assertEqual(h.getRawHeaders(b"Test\xe1"), [b"lemur"])
示例13: test_getAllRawHeaders
# 需要导入模块: from twisted.web.http_headers import Headers [as 别名]
# 或者: from twisted.web.http_headers.Headers import setRawHeaders [as 别名]
def test_getAllRawHeaders(self):
"""
L{Headers.getAllRawHeaders} returns an iterable of (k, v) pairs, where
C{k} is the canonicalized representation of the header name, and C{v}
is a sequence of values.
"""
h = Headers()
h.setRawHeaders(b"test", [b"lemurs"])
h.setRawHeaders(b"www-authenticate", [b"basic aksljdlk="])
allHeaders = set([(k, tuple(v)) for k, v in h.getAllRawHeaders()])
self.assertEqual(allHeaders, set([(b"WWW-Authenticate", (b"basic aksljdlk=",)), (b"Test", (b"lemurs",))]))
示例14: test_copy
# 需要导入模块: from twisted.web.http_headers import Headers [as 别名]
# 或者: from twisted.web.http_headers.Headers import setRawHeaders [as 别名]
def test_copy(self):
"""
L{Headers.copy} creates a new independant copy of an existing
L{Headers} instance, allowing future modifications without impacts
between the copies.
"""
h = Headers()
h.setRawHeaders('test', ['foo'])
i = h.copy()
self.assertEquals(i.getRawHeaders('test'), ['foo'])
h.addRawHeader('test', 'bar')
self.assertEquals(i.getRawHeaders('test'), ['foo'])
i.addRawHeader('test', 'baz')
self.assertEquals(h.getRawHeaders('test'), ['foo', 'bar'])
示例15: test_copy
# 需要导入模块: from twisted.web.http_headers import Headers [as 别名]
# 或者: from twisted.web.http_headers.Headers import setRawHeaders [as 别名]
def test_copy(self):
"""
L{Headers.copy} creates a new independant copy of an existing
L{Headers} instance, allowing future modifications without impacts
between the copies.
"""
h = Headers()
h.setRawHeaders(b"test", [b"foo"])
i = h.copy()
self.assertEqual(i.getRawHeaders(b"test"), [b"foo"])
h.addRawHeader(b"test", b"bar")
self.assertEqual(i.getRawHeaders(b"test"), [b"foo"])
i.addRawHeader(b"test", b"baz")
self.assertEqual(h.getRawHeaders(b"test"), [b"foo", b"bar"])