本文整理汇总了Python中tornado.httputil.HTTPHeaders.add方法的典型用法代码示例。如果您正苦于以下问题:Python HTTPHeaders.add方法的具体用法?Python HTTPHeaders.add怎么用?Python HTTPHeaders.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tornado.httputil.HTTPHeaders
的用法示例。
在下文中一共展示了HTTPHeaders.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _clean_headers
# 需要导入模块: from tornado.httputil import HTTPHeaders [as 别名]
# 或者: from tornado.httputil.HTTPHeaders import add [as 别名]
def _clean_headers(self):
"""
清理headers中不需要的部分,以及替换值
:return:
"""
headers = self.request.headers
# 更新host字段为后端访问网站的host
headers['Host'] = self.client.request.endpoint['netloc']
new_headers = HTTPHeaders()
# 如果 header 有的是 str,有的是 unicode
# 会出现 422 错误
for name, value in headers.get_all():
# 过滤 x-api 开头的, 这些只是发给 api-gateway
l_name = name.lower()
# 这些 headers 需要传递给后端
required_headers = ['x-api-user-json', 'x-api-access-key']
if l_name.startswith('x-api-') and l_name not in required_headers:
pass
# 不需要提供 Content-Length, 自动计算
# 如果 Content-Length 不正确, 请求后端网站会出错,
# 太大会出现超时问题, 太小会出现内容被截断
elif l_name == 'content-length':
pass
else:
new_headers.add(text_type(name), text_type(value))
return new_headers
示例2: execute
# 需要导入模块: from tornado.httputil import HTTPHeaders [as 别名]
# 或者: from tornado.httputil.HTTPHeaders import add [as 别名]
def execute(self):
url = self._make_url('/images/{0}/push'.format(self.name))
registry, name = resolve_repository_name(self.name)
headers = HTTPHeaders()
headers.add(REGISTRY_AUTH_HEADER, self._prepare_auth_header_value())
body = ''
log.info('Pushing "%s" into "%s"... ', name, registry)
log.debug('Pushing url: %s', url)
request = HTTPRequest(url, method='POST',
headers=headers,
body=body,
allow_ipv6=True,
request_timeout=self.timeout,
streaming_callback=self._on_body)
try:
result = yield self._http_client.fetch(request)
if self._lasterr is not None:
raise self._lasterr
log.info('OK')
except Exception as err:
log.error('FAIL - %s', err)
raise err
raise gen.Return(result)
示例3: post
# 需要导入模块: from tornado.httputil import HTTPHeaders [as 别名]
# 或者: from tornado.httputil.HTTPHeaders import add [as 别名]
def post(self,param):
targetURL = self.get_argument('url')
if DEBUG: print "target URL: " + targetURL
try:
serverURL= self.request.protocol + '://' + self.request.host
http_client = AsyncHTTPClient()
sub = yield http_client.fetch(targetURL, validate_cert=False)
sub_filename = targetURL[targetURL.rfind('/'):]
sub_filename = "fornow" #TODO - the URL doesn;t have to end with a filename, is it worth keeping?
files = []
files.append((sub_filename, sub_filename, sub.body))
fields = []
fields.append(("_xsrf" , self.xsrf_token))
content_type, body = encode_multipart_formdata(fields, files)
headers = HTTPHeaders({"Content-Type": content_type, 'content-length': str(len(body))})
headers.add("Cookie", "_xsrf=" + self.xsrf_token)
request = HTTPRequest(serverURL + "/import/", "POST", headers=headers, body=body, validate_cert=False)
response = yield http_client.fetch(request)
self.write(response.body)
except Exception, e:
print 'Failed to upload from URL (DocumentWrapperHandler)', e
self.write("Failed to upload from '" + targetURL + "'")
self.finish()
self.flush()
示例4: test_string
# 需要导入模块: from tornado.httputil import HTTPHeaders [as 别名]
# 或者: from tornado.httputil.HTTPHeaders import add [as 别名]
def test_string(self):
headers = HTTPHeaders()
headers.add("Foo", "1")
headers.add("Foo", "2")
headers.add("Foo", "3")
headers2 = HTTPHeaders.parse(str(headers))
self.assertEquals(headers, headers2)
示例5: weibo_request
# 需要导入模块: from tornado.httputil import HTTPHeaders [as 别名]
# 或者: from tornado.httputil.HTTPHeaders import add [as 别名]
def weibo_request(self, path, callback, access_token=None, expires_in=None,
post_args=None, **args):
url = "https://api.weibo.com/2/" + path + ".json"
all_args = {}
if access_token:
all_args['access_token'] = access_token
all_args.update(args)
all_args.update(post_args or {})
header = HTTPHeaders({'Authorization': 'OAuth2 %s' % access_token})
callback = self.async_callback(self._on_weibo_request, callback)
http = httpclient.AsyncHTTPClient()
if post_args is not None:
has_file = False
for key,value in post_args.iteritems():
if hasattr(value,"read"):
has_file = True
if has_file:
post_args,boundary = encode_multipart(post_args)
header.add('Content-Type', 'multipart/form-data; boundary=%s' %boundary)
header.add('Content-Length', len(post_args))
http.fetch(url, method="POST", body=post_args,
callback=callback,headers=header)
else:
http.fetch(url, method="POST", body=urllib.urlencode(all_args),
callback=callback,headers=header)
else:
if all_args: url += "?" + urllib.urlencode(all_args)
http.fetch(url, callback=callback,headers=header)
示例6: _prepare_request
# 需要导入模块: from tornado.httputil import HTTPHeaders [as 别名]
# 或者: from tornado.httputil.HTTPHeaders import add [as 别名]
def _prepare_request(self, messages):
# Determine the URL for the messages
url = self.url
if self._append_message_type and len(messages) == 1 and messages[0].channel.is_meta():
message_type = '/'.join(messages[0].channel.parts()[1:])
if not url.endswith('/'):
url += '/'
url += message_type
# Get the headers for the request
headers = HTTPHeaders()
for header, values in self.get_headers().iteritems():
for value in values:
headers.add(header, value)
for header, value in headers.get_all():
self.log.debug('Request header %s: %s' % (header, value))
# Get the body for the request
body = Message.to_json(messages, encoding='utf8')
self.log.debug('Request body (length: %d): %s' % (len(body), body))
# Get the timeout (in seconds)
timeout = self.get_timeout(messages) / 1000.0
self.log.debug('Request timeout: %ss' % timeout)
# Build and return the request
return HTTPRequest(
url,
method='POST',
headers=headers,
body=body,
connect_timeout=timeout,
request_timeout=timeout
)
示例7: send_object
# 需要导入模块: from tornado.httputil import HTTPHeaders [as 别名]
# 或者: from tornado.httputil.HTTPHeaders import add [as 别名]
def send_object(cls, object_url):
"""
Sends an OpenSlides object to all connected clients (waiters).
First, retrieve the object from the OpenSlides REST api using the given
object_url.
"""
# Join network location with object URL.
# TODO: Use host and port as given in the start script
wsgi_network_location = settings.OPENSLIDES_WSGI_NETWORK_LOCATION or 'http://localhost:8000'
url = ''.join((wsgi_network_location, object_url))
# Send out internal HTTP request to get data from the REST api.
for waiter in cls.waiters:
# Read waiter's former cookies and parse session cookie to new header object.
headers = HTTPHeaders()
try:
session_cookie = waiter.connection_info.cookies[settings.SESSION_COOKIE_NAME]
except KeyError:
# There is no session cookie
pass
else:
headers.add('Cookie', '%s=%s' % (settings.SESSION_COOKIE_NAME, session_cookie.value))
# Setup uncompressed request.
request = HTTPRequest(
url=url,
headers=headers,
decompress_response=False)
# Setup non-blocking HTTP client
http_client = AsyncHTTPClient()
# Executes the request, asynchronously returning an HTTPResponse
# and calling waiter's forward_rest_response() method.
http_client.fetch(request, waiter.forward_rest_response)
示例8: test_pickle_roundtrip
# 需要导入模块: from tornado.httputil import HTTPHeaders [as 别名]
# 或者: from tornado.httputil.HTTPHeaders import add [as 别名]
def test_pickle_roundtrip(self):
headers = HTTPHeaders()
headers.add('Set-Cookie', 'a=b')
headers.add('Set-Cookie', 'c=d')
headers.add('Content-Type', 'text/html')
pickled = pickle.dumps(headers)
unpickled = pickle.loads(pickled)
self.assertEqual(sorted(headers.get_all()), sorted(unpickled.get_all()))
self.assertEqual(sorted(headers.items()), sorted(unpickled.items()))
示例9: _parse_headers
# 需要导入模块: from tornado.httputil import HTTPHeaders [as 别名]
# 或者: from tornado.httputil.HTTPHeaders import add [as 别名]
def _parse_headers(self):
frame = self._header_frames[0]
data = b''.join(f.data for f in self._header_frames)
self._header_frames = []
if frame.flags & constants.FrameFlag.PRIORITY:
# TODO: support PRIORITY and PADDING.
# This is just enough to cover an error case tested in h2spec.
stream_dep, weight = struct.unpack('>ib', data[:5])
data = data[5:]
# strip off the "exclusive" bit
stream_dep = stream_dep & 0x7fffffff
if stream_dep == frame.stream_id:
raise ConnectionError(constants.ErrorCode.PROTOCOL_ERROR,
"stream cannot depend on itself")
pseudo_headers = {}
headers = HTTPHeaders()
try:
# Pseudo-headers must come before any regular headers,
# and only in the first HEADERS phase.
has_regular_header = bool(self._phase == constants.HTTPPhase.TRAILERS)
for k, v, idx in self.conn.hpack_decoder.decode(bytearray(data)):
if k != k.lower():
# RFC section 8.1.2
raise StreamError(self.stream_id,
constants.ErrorCode.PROTOCOL_ERROR)
if k.startswith(b':'):
if self.conn.is_client:
valid_pseudo_headers = (b':status',)
else:
valid_pseudo_headers = (b':method', b':scheme',
b':authority', b':path')
if (has_regular_header or
k not in valid_pseudo_headers or
native_str(k) in pseudo_headers):
raise StreamError(self.stream_id,
constants.ErrorCode.PROTOCOL_ERROR)
pseudo_headers[native_str(k)] = native_str(v)
if k == b":authority":
headers.add("Host", native_str(v))
else:
headers.add(native_str(k), native_str(v))
has_regular_header = True
except HpackError:
raise ConnectionError(constants.ErrorCode.COMPRESSION_ERROR)
if self._phase == constants.HTTPPhase.HEADERS:
self._start_request(pseudo_headers, headers)
elif self._phase == constants.HTTPPhase.TRAILERS:
# TODO: support trailers
pass
if (not self._maybe_end_stream(frame.flags) and
self._phase == constants.HTTPPhase.TRAILERS):
# The frame that finishes the trailers must also finish
# the stream.
raise StreamError(self.stream_id, constants.ErrorCode.PROTOCOL_ERROR)
示例10: headers_received
# 需要导入模块: from tornado.httputil import HTTPHeaders [as 别名]
# 或者: from tornado.httputil.HTTPHeaders import add [as 别名]
def headers_received(
self,
start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],
headers: httputil.HTTPHeaders,
) -> Optional[Awaitable[None]]:
if headers.get("Content-Encoding") == "gzip":
self._decompressor = GzipDecompressor()
# Downstream delegates will only see uncompressed data,
# so rename the content-encoding header.
# (but note that curl_httpclient doesn't do this).
headers.add("X-Consumed-Content-Encoding", headers["Content-Encoding"])
del headers["Content-Encoding"]
return self._delegate.headers_received(start_line, headers)
示例11: test_copy
# 需要导入模块: from tornado.httputil import HTTPHeaders [as 别名]
# 或者: from tornado.httputil.HTTPHeaders import add [as 别名]
def test_copy(self):
all_pairs = [('A', '1'), ('A', '2'), ('B', 'c')]
h1 = HTTPHeaders()
for k, v in all_pairs:
h1.add(k, v)
h2 = h1.copy()
h3 = copy.copy(h1)
h4 = copy.deepcopy(h1)
for headers in [h1, h2, h3, h4]:
# All the copies are identical, no matter how they were
# constructed.
self.assertEqual(list(sorted(headers.get_all())), all_pairs)
for headers in [h2, h3, h4]:
# Neither the dict or its member lists are reused.
self.assertIsNot(headers, h1)
self.assertIsNot(headers.get_list('A'), h1.get_list('A'))
示例12: execute
# 需要导入模块: from tornado.httputil import HTTPHeaders [as 别名]
# 或者: from tornado.httputil.HTTPHeaders import add [as 别名]
def execute(self):
url = self._make_url('/images/{0}/push'.format(self.name))
registry, name = resolve_repository_name(self.name)
headers = HTTPHeaders()
headers.add('X-Registry-Auth', self._prepare_auth_header_value())
body = ''
log.info('Pushing "%s" into "%s"... ', name, registry)
request = HTTPRequest(url, method='POST',
headers=headers,
body=body,
request_timeout=self.timeout,
streaming_callback=self._on_body)
try:
yield self._http_client.fetch(request)
log.info('OK')
except Exception as err:
log.error('FAIL - %s', err)
raise err
示例13: _deserialize_from_cache
# 需要导入模块: from tornado.httputil import HTTPHeaders [as 别名]
# 或者: from tornado.httputil.HTTPHeaders import add [as 别名]
def _deserialize_from_cache(self, value):
data = json.loads(value.decode())
if not self.cache_meta_data.is_valid(data):
logging.debug('cache is expired!')
return None
try:
headers = HTTPHeaders()
for k, v in data['headers']:
headers.add(k, v)
response = HTTPResponse(
HTTPRequest(url=data['url']),
int(data['code']),
headers,
buffer=BytesIO(data['body'].encode()),
)
except KeyError:
return None
return response
示例14: request
# 需要导入模块: from tornado.httputil import HTTPHeaders [as 别名]
# 或者: from tornado.httputil.HTTPHeaders import add [as 别名]
def request(self, req_and_resp, opt=None, headers=None):
"""
"""
# make sure all prepared state are clean before processing
req, resp = req_and_resp
req.reset()
resp.reset()
opt = opt or {}
req, resp = super(TornadoClient, self).request((req, resp), opt)
req.prepare(scheme=self.prepare_schemes(req), handle_files=True)
req._patch(opt)
composed_headers = self.compose_headers(req, headers, opt)
tornado_headers = HTTPHeaders()
for h in composed_headers:
tornado_headers.add(*h)
url = url_concat(req.url, req.query)
rq = HTTPRequest(
url=url,
method=req.method.upper(),
headers=tornado_headers,
body=req.data
)
rs = yield self.__client.fetch(rq)
resp.apply_with(
status=rs.code,
raw=rs.body
)
for k, v in rs.headers.get_all():
resp.apply_with(header={k: v})
raise gen.Return(resp)
示例15: init_headers
# 需要导入模块: from tornado.httputil import HTTPHeaders [as 别名]
# 或者: from tornado.httputil.HTTPHeaders import add [as 别名]
def init_headers(self, args):
headers = HTTPHeaders()
if args["headers"]:
for header in args["headers"]:
values = header.split(':', 1)
if len(values) == 2:
headers.add(*values)
else: # values == 1
headers.add(values[0], "")
# Setting Cookies
if args["cookie"]:
headers.add("Cookie", args["cookie"])
return headers