本文整理汇总了Python中tornado.httputil.HTTPHeaders.update方法的典型用法代码示例。如果您正苦于以下问题:Python HTTPHeaders.update方法的具体用法?Python HTTPHeaders.update怎么用?Python HTTPHeaders.update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tornado.httputil.HTTPHeaders
的用法示例。
在下文中一共展示了HTTPHeaders.update方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _delete
# 需要导入模块: from tornado.httputil import HTTPHeaders [as 别名]
# 或者: from tornado.httputil.HTTPHeaders import update [as 别名]
def _delete(self, url, headers=None, callback=None):
h = HTTPHeaders()
h.update(self._default_headers)
if headers:
h.update(headers)
req = HTTPRequest(url, headers=headers, method="DELETE")
self._client.fetch(req, callback)
示例2: _get
# 需要导入模块: from tornado.httputil import HTTPHeaders [as 别名]
# 或者: from tornado.httputil.HTTPHeaders import update [as 别名]
def _get(self, url, headers=None, callback=None):
"""
A `GET` request to the solr.
"""
h = HTTPHeaders()
h.update(self._default_headers)
if headers:
h.update(headers)
req = HTTPRequest(url, headers=headers)
self._client.fetch(req, callback)
示例3: SolrClient
# 需要导入模块: from tornado.httputil import HTTPHeaders [as 别名]
# 或者: from tornado.httputil.HTTPHeaders import update [as 别名]
class SolrClient(object):
"""
Apache Solr Client class.
"""
def __init__(self, search_host, update_host=None, default_headers=None,
required_query_params=[], client_args={}, select_path='/select',
update_path='/update/json', mlt_path='/mlt',
document_verifier=None, ioloop=None):
"""
Initialize me.
"""
self._ioloop = ioloop or IOLoop.instance()
self._search_url = '%s%s' % (search_host, select_path)
self._mlt_url = '%s%s' % (search_host, mlt_path)
uhost = update_host or search_host
self._update_url = '%s%s' % (uhost, update_path)
self._required_query_params = required_query_params
if len([k for (k,v) in self._required_query_params if k=="wt"]) == 0:
self._required_query_params.append(('wt', 'json'))
self._document_verifier = document_verifier
self._default_headers = HTTPHeaders()
if default_headers:
self._default_headers.update(default_headers)
self._client = AsyncHTTPClient(self._ioloop, **client_args)
def _get(self, url, headers=None, callback=None):
"""
A `GET` request to the solr.
"""
h = HTTPHeaders()
h.update(self._default_headers)
if headers:
h.update(headers)
req = HTTPRequest(url, headers=headers)
self._client.fetch(req, callback)
def _post(self, url, body, headers=None, callback=None):
"""
A `POST` request to the solr.
"""
h = headers or HTTPHeaders()
h.update(self._default_headers)
h["Content-type"] = "application/json"
request = HTTPRequest(url, headers=h, method="POST",
body=json.dumps(body))
self._client.fetch(request, callback)
def search(self, querybuilder, callback=None):
"""
Search the Solr with `querybuilder.get_params()` as query parameter.
"""
query_params = querybuilder.get_params()
for p in self._required_query_params:
if p not in query_params:
query_params.append(p)
log.debug('Searching solr with params: %s' % query_params)
qs = urllib.urlencode(query_params)
final_url = "?".join([self._search_url, qs])
log.debug('Final search URL: %s' % final_url)
self._get(final_url, headers=querybuilder.headers,
callback=handle_search_response(querybuilder, callback))
def more_like_this(self, querybuilder, callback=None, match_include=True,
match_offset=None, interestingTerms=None):
"""
`interestingTerms` can be one of: 'list', 'details', 'none'.
"""
query_params = querybuilder.get_params()
for p in self._required_query_params:
if p not in query_params:
query_params.append(p)
if match_include and isinstance(match_include, types.BooleanType):
query_params.append(('mlt.match.include', str(match_include).lower()))
if match_offset:
query_params.append(('mlt.match.offset', str(match_offset)))
if interestingTerms:
query_params.append(('mlt.interestingTerms', interestingTerms))
self.log.debug('MoreLikeThis with params: %s' % query_params)
qs = urllib.urlencode(query_params)
final_url = '?'.join([self._mlt_url, qs])
self.log.debug('Final MLT URL: %s' % final_url)
self._get(final_url, headers=querybuilder.headers,
callback=self._handle_search_response(querybuilder, callback))
def index_document(self, doc, callback=None, commit=False):
"""
Index a `doc` into Solr. The `callback` will be called from within
`self._handle_indexing_response`. If `commit is True`, then a `commit`
request is sent to Solr.
#.........这里部分代码省略.........
示例4: SolrClient
# 需要导入模块: from tornado.httputil import HTTPHeaders [as 别名]
# 或者: from tornado.httputil.HTTPHeaders import update [as 别名]
class SolrClient(object):
"""
Apache Solr Client class.
"""
def __init__(self, search_host, update_host=None, default_headers=None,
required_query_params=[], client_args={}, select_path='/select',
update_path='/update/json', mlt_path='/mlt', get_path='/get',
suggest_path='/suggest', document_verifier=None, ioloop=None):
"""
Initialize me.
"""
self._ioloop = ioloop or IOLoop.instance()
self._search_url = '%s%s' % (search_host, select_path)
self._mlt_url = '%s%s' % (search_host, mlt_path)
self._get_url = '%s%s' % (search_host, get_path)
self._termsuggest_url = '%s%s' % (search_host, suggest_path)
uhost = update_host or search_host
self._update_url = '%s%s' % (uhost, update_path)
self._required_query_params = required_query_params
if len([k for (k,v) in self._required_query_params if k=="wt"]) == 0:
self._required_query_params.append(('wt', 'json'))
self._document_verifier = document_verifier or \
default_document_verifier
self._default_headers = HTTPHeaders()
if default_headers:
self._default_headers.update(default_headers)
self._client = AsyncHTTPClient(self._ioloop, **client_args)
def _get(self, url, headers=None, callback=None):
"""
A `GET` request to the solr.
"""
h = HTTPHeaders()
h.update(self._default_headers)
if headers:
h.update(headers)
req = HTTPRequest(url, headers=headers)
self._client.fetch(req, callback)
def _post(self, url, body, headers=None, callback=None):
"""
A `POST` request to the solr.
"""
h = headers or HTTPHeaders()
h.update(self._default_headers)
if type(body) == str:
body, h["Content-type"] = body, "application/x-www-form-urlencoded"
else:
body, h["Content-type"] = json.dumps(body), "application/json"
request = HTTPRequest(url, headers=h, method="POST", body=body)
self._client.fetch(request, callback)
def _get_params(self, querybuilder):
query_params = querybuilder.get_params()
for p in self._required_query_params:
if p not in query_params:
query_params.append(p)
return query_params
def search(self, querybuilder, callback=None):
"""
Search the Solr with `querybuilder.get_params()` as query parameters.
Use GET by default, but switch to POST in case of very long urls.
"""
query_params = self._get_params(querybuilder)
log.debug('Searching solr with params: %s' % query_params)
qs = urllib.urlencode(query_params)
final_url = "?".join([self._search_url, qs])
# use POST if the final url is very long
final_url, use_post = (self._search_url, True) if len(final_url) > 2000 \
else (final_url, False)
log.debug('Final search URL: %s' % final_url)
if use_post:
self._post(final_url, qs, headers=querybuilder.headers,
callback=handle_search_response(querybuilder, callback))
else:
self._get(final_url, headers=querybuilder.headers,
callback=handle_search_response(querybuilder, callback))
def more_like_this(self, querybuilder, callback=None, match_include=True,
match_offset=None, interestingTerms=None):
"""
`interestingTerms` can be one of: 'list', 'details', 'none'.
"""
query_params = self._get_params(querybuilder)
if match_include and isinstance(match_include, types.BooleanType):
query_params.append(('mlt.match.include', str(match_include).lower()))
if match_offset:
query_params.append(('mlt.match.offset', str(match_offset)))
#.........这里部分代码省略.........
示例5: Configuration
# 需要导入模块: from tornado.httputil import HTTPHeaders [as 别名]
# 或者: from tornado.httputil.HTTPHeaders import update [as 别名]
#.........这里部分代码省略.........
raise AttributeError(value)
# store current verb to be passed to Request
self.verb = value.upper()
# set accept if it wasn't set previously
if 'accept' not in self.headers:
for flavor in self.flavors:
if 'accept' in self.FLAVORS[flavor]:
self.headers.add('accept', self.FLAVORS[flavor]['accept'])
# set form type and default if noone is present
verb_allowed = self.verb in ('POST', 'PUT', 'PATCH')
if verb_allowed and 'content-type' not in self.headers:
self.headers['content-type'] = self.FLAVORS['form']['content-type']
# Debug helper
if __debug__:
sys.stderr.write("=" * 70)
sys.stderr.write("\nRequest:{0} {1}".format(self.verb, self.uri))
sys.stderr.write("\nHeaders:")
sys.stderr.write("\n Accept:'{0}'".format(self.headers['accept']))
if 'content-type' in self.headers:
ctype = self.headers['content-type']
sys.stderr.write("\n Content-Type:'{0}'".format(ctype))
sys.stderr.write("\n Compressed:'{0}'".format(self.use_gzip))
if self.uri.startswith("https"):
sys.stderr.write("\nCerts:'{0}'".format(self.ca_certs))
sys.stderr.write("\n{0}\n".format("=" * 70))
return Request(self)
def use(self, feature):
"""Register a feature (processor) at this configuration"""
self.processors.insert(0, feature)
return self
def secure(self, value=None, port=None, ca_certs=None):
"""Force connection using https protocol at port specified"""
if isinstance(value, bool):
scheme = 'http' if not value else 'https'
self.uri = _PROT_RE.sub(scheme + r"://\g<url>", self.uri)
if isinstance(port, int):
regx_str = r"\g<proto>\g<host>:{0}\g<url>".format(port)
self.uri = _PORT_RE.sub(regx_str, self.uri)
if isinstance(ca_certs, basestring):
self.ca_certs = ca_certs
return self
def compress(self, compress=True):
"""Notify server that we will be zipping request"""
self.use_gzip = bool(compress)
return self
def progress(self, progress_callback):
"""
Allow to define a progress callback about operaiton. This
progress callback takes 2 arguments, total length, if any and
amount of bytes already transfered
"""
self.progress_callback = progress_callback
return self
def until(self, connect_timeout=None, request_timeout=None):
"""Set current timeout in seconds for every call"""
self.connect_timeout = connect_timeout or self.connect_timeout
self.request_timeout = request_timeout or self.request_timeout
return self
def as_(self, flavor):
"""Set up the Content-Type"""
if flavor is not None:
# Just use default flavors in case we pass a None param
if flavor in self.FLAVORS:
self.headers.update(self.FLAVORS[flavor])
else:
self.headers["accept"] = flavor
self.headers["content-type"] = flavor
return self
def accepts(self, flavor):
"""Configure the accepted response format"""
if flavor is not None:
if flavor in self.FLAVORS:
flavor = self.FLAVORS[flavor]['accept']
self.headers.add('accept', flavor)
return self
def auth(self, credentials, path="*", method='plain'):
"""Authentication feature. It does simple HTTP auth"""
# already defined ?
if path in self.credentials or method is None:
return self
# process a regex valid for path
expr = "%s.*" if path.endswith('*') else "%s$"
rmatch = re.compile(expr % path.rsplit('*', 1)[0])
# now store it
self.credentials[path] = (rmatch, method, credentials,)
return self