本文整理汇总了Python中requests.org方法的典型用法代码示例。如果您正苦于以下问题:Python requests.org方法的具体用法?Python requests.org怎么用?Python requests.org使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类requests
的用法示例。
在下文中一共展示了requests.org方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _query
# 需要导入模块: import requests [as 别名]
# 或者: from requests import org [as 别名]
def _query(
self,
page: 'WikipediaPage',
params: Dict[str, Any]
):
base_url = 'https://' + page.language + '.wikipedia.org/w/api.php'
log.info(
"Request URL: %s",
base_url + "?" + "&".join(
[k + "=" + str(v) for k, v in params.items()]
)
)
params['format'] = 'json'
params['redirects'] = 1
r = self._session.get(
base_url,
params=params,
**self._request_kwargs
)
return r.json()
示例2: infer_msg
# 需要导入模块: import requests [as 别名]
# 或者: from requests import org [as 别名]
def infer_msg(self, tts, rsp):
"""Attempt to guess what went wrong by using known
information (e.g. http response) and observed behaviour
"""
# rsp should be <requests.Response>
# http://docs.python-requests.org/en/master/api/
status = rsp.status_code
reason = rsp.reason
cause = "Unknown"
if status == 403:
cause = "Bad token or upstream API changes"
elif status == 404 and not tts.lang_check:
cause = "Unsupported language '%s'" % self.tts.lang
elif status >= 500:
cause = "Uptream API error. Try again later."
return "%i (%s) from TTS API. Probable cause: %s" % (
status, reason, cause)
示例3: get_request_files
# 需要导入模块: import requests [as 别名]
# 或者: from requests import org [as 别名]
def get_request_files(self, var_name):
"""
Returns a dictionary containing attachments as `{var_name: ('foo.png', open('foo.png', 'rb'), 'image/png')}`.
For the format of thoses tuples see the requests docs:
http://docs.python-requests.org/en/master/user/advanced/#post-multiple-multipart-encoded-files
Used by :py:func:`~pytgbot.bot.Bot._do_fileupload`.
:param var_name: The variable name we want to send the file as.
:type var_name: str
:return: A dictionary, containing attachments how they are needed by the requests library.
:rtype: dict
"""
raise NotImplementedError('Your sub-class should implement this.')
# end def
示例4: delete
# 需要导入模块: import requests [as 别名]
# 或者: from requests import org [as 别名]
def delete(self, endpoint, *, headers=None, data=None, verify=False,
params=None):
"""Wrapper for authenticated HTTP DELETE to API endpoint.
endpoint = URL (can be partial; for example, 'me/contacts')
headers = HTTP header dictionary; will be merged with graphrest's
standard headers, which include access token
data = HTTP request body
verify = the Requests option for verifying SSL certificate; defaults
to False for demo purposes. For more information see:
http://docs.python-requests.org/en/master/user/advanced/#ssl-csert-verification
params = query string parameters
Returns Requests response object.
"""
self.token_validation()
return requests.delete(self.api_endpoint(endpoint),
headers=self.headers(headers),
data=data, verify=verify, params=params)
示例5: get
# 需要导入模块: import requests [as 别名]
# 或者: from requests import org [as 别名]
def get(self, endpoint='me', *, headers=None, stream=False, verify=False, params=None):
"""Wrapper for authenticated HTTP GET to API endpoint.
endpoint = URL (can be partial; for example, 'me/contacts')
headers = HTTP header dictionary; will be merged with graphrest's
standard headers, which include access token
stream = Requests streaming option; set to True for image data, etc.
verify = the Requests option for verifying SSL certificate; defaults
to False for demo purposes. For more information see:
http://docs.python-requests.org/en/master/user/advanced/#ssl-csert-verification
params = query string parameters
Returns Requests response object.
"""
self.token_validation()
# Merge passed headers with default headers.
merged_headers = self.headers()
if headers:
merged_headers.update(headers)
return requests.get(self.api_endpoint(endpoint),
headers=merged_headers,
stream=stream, verify=verify, params=params)
示例6: patch
# 需要导入模块: import requests [as 别名]
# 或者: from requests import org [as 别名]
def patch(self, endpoint, *, headers=None, data=None, verify=False, params=None):
"""Wrapper for authenticated HTTP PATCH to API endpoint.
endpoint = URL (can be partial; for example, 'me/contacts')
headers = HTTP header dictionary; will be merged with graphrest's
standard headers, which include access token
data = HTTP request body
verify = the Requests option for verifying SSL certificate; defaults
to False for demo purposes. For more information see:
http://docs.python-requests.org/en/master/user/advanced/#ssl-csert-verification
params = query string parameters
Returns Requests response object.
"""
self.token_validation()
return requests.patch(self.api_endpoint(endpoint),
headers=self.headers(headers),
data=data, verify=verify, params=params)
示例7: post
# 需要导入模块: import requests [as 别名]
# 或者: from requests import org [as 别名]
def post(self, endpoint, headers=None, data=None, verify=False, params=None):
"""POST to API (authenticated with access token).
headers = custom HTTP headers (merged with defaults, including access token)
verify = the Requests option for verifying SSL certificate; defaults
to False for demo purposes. For more information see:
http://docs.python-requests.org/en/master/user/advanced/#ssl-cert-verification
"""
self.token_validation()
merged_headers = self.headers()
if headers:
merged_headers.update(headers)
return requests.post(self.api_endpoint(endpoint),
headers=merged_headers, data=data,
verify=verify, params=params)
示例8: put
# 需要导入模块: import requests [as 别名]
# 或者: from requests import org [as 别名]
def put(self, endpoint, *, headers=None, data=None, verify=False, params=None):
"""Wrapper for authenticated HTTP PUT to API endpoint.
endpoint = URL (can be partial; for example, 'me/contacts')
headers = HTTP header dictionary; will be merged with graphrest's
standard headers, which include access token
data = HTTP request body
verify = the Requests option for verifying SSL certificate; defaults
to False for demo purposes. For more information see:
http://docs.python-requests.org/en/master/user/advanced/#ssl-csert-verification
params = query string parameters
Returns Requests response object.
"""
self.token_validation()
return requests.put(self.api_endpoint(endpoint),
headers=self.headers(headers),
data=data, verify=verify, params=params)
示例9: get
# 需要导入模块: import requests [as 别名]
# 或者: from requests import org [as 别名]
def get(self, path, **kwargs):
"""Perform an HTTP GET request of the specified path in Device Cloud
Make an HTTP GET request against Device Cloud with this accounts
credentials and base url. This method uses the
`requests <http://docs.python-requests.org/en/latest/>`_ library
`request method <http://docs.python-requests.org/en/latest/api/#requests.request>`_
and all keyword arguments will be passed on to that method.
:param str path: Device Cloud path to GET
:param int retries: The number of times the request should be retried if an
unsuccessful response is received. Most likely, you should leave this at 0.
:raises DeviceCloudHttpException: if a non-success response to the request is received
from Device Cloud
:returns: A requests ``Response`` object
"""
url = self._make_url(path)
return self._make_request("GET", url, **kwargs)
示例10: post
# 需要导入模块: import requests [as 别名]
# 或者: from requests import org [as 别名]
def post(self, path, data, **kwargs):
"""Perform an HTTP POST request of the specified path in Device Cloud
Make an HTTP POST request against Device Cloud with this accounts
credentials and base url. This method uses the
`requests <http://docs.python-requests.org/en/latest/>`_ library
`request method <http://docs.python-requests.org/en/latest/api/#requests.request>`_
and all keyword arguments will be passed on to that method.
:param str path: Device Cloud path to POST
:param int retries: The number of times the request should be retried if an
unsuccessful response is received. Most likely, you should leave this at 0.
:param data: The data to be posted in the body of the POST request (see docs for
``requests.post``
:raises DeviceCloudHttpException: if a non-success response to the request is received
from Device Cloud
:returns: A requests ``Response`` object
"""
url = self._make_url(path)
return self._make_request("POST", url, data=data, **kwargs)
示例11: put
# 需要导入模块: import requests [as 别名]
# 或者: from requests import org [as 别名]
def put(self, path, data, **kwargs):
"""Perform an HTTP PUT request of the specified path in Device Cloud
Make an HTTP PUT request against Device Cloud with this accounts
credentials and base url. This method uses the
`requests <http://docs.python-requests.org/en/latest/>`_ library
`request method <http://docs.python-requests.org/en/latest/api/#requests.request>`_
and all keyword arguments will be passed on to that method.
:param str path: Device Cloud path to PUT
:param int retries: The number of times the request should be retried if an
unsuccessful response is received. Most likely, you should leave this at 0.
:param data: The data to be posted in the body of the POST request (see docs for
``requests.post``
:raises DeviceCloudHttpException: if a non-success response to the request is received
from Device Cloud
:returns: A requests ``Response`` object
"""
url = self._make_url(path)
return self._make_request("PUT", url, data=data, **kwargs)
示例12: delete
# 需要导入模块: import requests [as 别名]
# 或者: from requests import org [as 别名]
def delete(self, path, retries=DEFAULT_THROTTLE_RETRIES, **kwargs):
"""Perform an HTTP DELETE request of the specified path in Device Cloud
Make an HTTP DELETE request against Device Cloud with this accounts
credentials and base url. This method uses the
`requests <http://docs.python-requests.org/en/latest/>`_ library
`request method <http://docs.python-requests.org/en/latest/api/#requests.request>`_
and all keyword arguments will be passed on to that method.
:param str path: Device Cloud path to DELETE
:param int retries: The number of times the request should be retried if an
unsuccessful response is received. Most likely, you should leave this at 0.
:raises DeviceCloudHttpException: if a non-success response to the request is received
from Device Cloud
:returns: A requests ``Response`` object
"""
url = self._make_url(path)
return self._make_request("DELETE", url, **kwargs)
示例13: send_http_json
# 需要导入模块: import requests [as 别名]
# 或者: from requests import org [as 别名]
def send_http_json(method, url, exc=True, headers=None, **kwargs):
"""Send the HTTP request.
See: http://www.python-requests.org/en/master/api/#requests.Session.request
"""
if headers:
headers["Accept"] = "application/json"
else:
headers = {"Accept": "application/json"}
resp = getattr(requests, method)(url, headers=headers, **kwargs)
if exc:
resp.raise_for_status()
data = resp.content
if resp.status_code == 200:
if resp.content:
data = resp.json()
return resp.status_code, data
示例14: _send_request
# 需要导入模块: import requests [as 别名]
# 或者: from requests import org [as 别名]
def _send_request(self, request, ignore_content):
# http://docs.python-requests.org/en/master/user/advanced/#timeouts
response = self.session.send(request, timeout=(6.5, self.request_timeout), stream=True)
try:
if not is_ok_status(response.status_code):
content = self._read_content(response)
return response, content
self.check_content_type(response)
content = None
if not ignore_content:
content = self._read_content(response)
finally:
# Fix: Requests memory leak
# https://github.com/psf/requests/issues/4601
response.close()
return response, content
示例15: create_session
# 需要导入模块: import requests [as 别名]
# 或者: from requests import org [as 别名]
def create_session(max_connections=8, proxies=None):
"""
Creates a session object that can be used by multiple :class:`Dropbox` and
:class:`DropboxTeam` instances. This lets you share a connection pool
amongst them, as well as proxy parameters.
:param int max_connections: Maximum connection pool size.
:param dict proxies: See the `requests module
<http://docs.python-requests.org/en/latest/user/advanced/#proxies>`_
for more details.
:rtype: :class:`requests.sessions.Session`. `See the requests module
<http://docs.python-requests.org/en/latest/user/advanced/#session-objects>`_
for more details.
"""
# We only need as many pool_connections as we have unique hostnames.
session = pinned_session(pool_maxsize=max_connections)
if proxies:
session.proxies = proxies
return session