本文整理匯總了Python中urllib.request.get_method方法的典型用法代碼示例。如果您正苦於以下問題:Python request.get_method方法的具體用法?Python request.get_method怎麽用?Python request.get_method使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類urllib.request
的用法示例。
在下文中一共展示了request.get_method方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: check_connection
# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import get_method [as 別名]
def check_connection(self):
request = urllib.request.Request(self._api_hostname)
request.get_method = lambda: 'GET'
try:
data = self._request('/api/v1/service-online', {}, force=True)
except URLError as e:
TestLogger.log_warning('Problem with connecting to {} ({}).'.format(self._api_hostname, e))
return
else:
self._has_connection = (
200 >= self._last_response.status < 400
) and data.get('success')
if data.get('msg'):
TestLogger.log_warning(data.get('msg'))
self.version = data.get('version')
示例2: put
# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import get_method [as 別名]
def put(self, location, params=None):
"""Dispatch a PUT request to a SeaMicro chassis.
The seamicro box has order-dependent HTTP parameters, so we build
our own get URL, and use a list vs. a dict for data, as the order is
implicit.
"""
opener = urllib.request.build_opener(urllib.request.HTTPHandler)
url = self.build_url(location, params)
request = urllib.request.Request(url)
request.get_method = lambda: "PUT"
request.add_header("content-type", "text/json")
response = opener.open(request)
json_data = self.parse_response(url, response)
return json_data["result"]
示例3: OpenWebRequest
# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import get_method [as 別名]
def OpenWebRequest(uri, post_data, headers, timeout, get_method=None):
request = urllib.request.Request(uri, post_data, headers)
if get_method:
request.get_method = get_method
if timeout == 0:
response = urllib.request.urlopen(request)
else:
response = urllib.request.urlopen(request, timeout=timeout)
return response
示例4: ExecuteWebRequest
# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import get_method [as 別名]
def ExecuteWebRequest(uri, post_data, headers, timeout, get_method=None):
response = OpenWebRequest(uri, post_data, headers, timeout, get_method)
return response.read().decode('utf-8')
示例5: ExecuteWithPostData_JSON
# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import get_method [as 別名]
def ExecuteWithPostData_JSON(session_id, uri, sub_url, timeout, post_data, get_method):
headers = CreateHeadersWithSessionCookieAndCustomHeader(session_id)
headers["Content-Type"] = "application/json; charset=UTF-8"
if isinstance(post_data, dict) or isinstance(post_data, list):
post_data = json.dumps(post_data, separators=(',', ':'))
post_data = post_data.encode('utf-8')
return ExecuteWebRequest(uri + sub_url, post_data, headers, timeout, get_method)
示例6: check_target
# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import get_method [as 別名]
def check_target(target, options):
'''
Just put a protocol to a valid IP and check if connection works,
returning HEAD response
'''
# Recover used options
ssldisabled = options.ssldisabled
useget = options.useget
proxy = options.proxy
response = None
target = normalize(target)
try:
request = urllib.request.Request(target, headers=client_headers)
# Set method
method = 'GET' if useget else 'HEAD'
request.get_method = lambda: method
# Set proxy
set_proxy(proxy)
# Set certificate validation
if ssldisabled:
context = get_unsafe_context()
response = urllib.request.urlopen(request, timeout=10, context=context)
else:
response = urllib.request.urlopen(request, timeout=10)
except Exception as e:
print_error(e)
sys.exit(1)
if response is not None:
return response
print("Couldn't read a response from server.")
sys.exit(3)
示例7: open_request
# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import get_method [as 別名]
def open_request(url, query_params=None, user_agent=None, post_data=None, referer=None, get_method=None, cookies=False,
timeout=None, headers=None, **kwargs):
if query_params is None:
query_params = {}
if user_agent is None:
user_agent = ua_cloudbot
query_params.update(kwargs)
url = prepare_url(url, query_params)
request = urllib.request.Request(url, post_data)
if get_method is not None:
request.get_method = lambda: get_method
if headers is not None:
for header_key, header_value in headers.items():
request.add_header(header_key, header_value)
request.add_header('User-Agent', user_agent)
if referer is not None:
request.add_header('Referer', referer)
if cookies:
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(jar))
else:
opener = urllib.request.build_opener()
if timeout:
return opener.open(request, timeout=timeout)
return opener.open(request)
# noinspection PyShadowingBuiltins
示例8: open
# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import get_method [as 別名]
def open(url, query_params=None, user_agent=None, post_data=None,
referer=None, get_method=None, cookies=False, timeout=None, headers=None,
**kwargs): # pylint: disable=locally-disabled, redefined-builtin # pragma: no cover
warnings.warn(
"http.open() is deprecated, use http.open_request() instead.",
DeprecationWarning
)
return open_request(
url, query_params=query_params, user_agent=user_agent, post_data=post_data, referer=referer,
get_method=get_method, cookies=cookies, timeout=timeout, headers=headers, **kwargs
)
示例9: test_default_values
# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import get_method [as 別名]
def test_default_values(self):
Request = urllib.request.Request
request = Request("http://www.python.org")
self.assertEqual(request.get_method(), 'GET')
request = Request("http://www.python.org", {})
self.assertEqual(request.get_method(), 'POST')
示例10: test_with_method_arg
# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import get_method [as 別名]
def test_with_method_arg(self):
Request = urllib.request.Request
request = Request("http://www.python.org", method='HEAD')
self.assertEqual(request.method, 'HEAD')
self.assertEqual(request.get_method(), 'HEAD')
request = Request("http://www.python.org", {}, method='HEAD')
self.assertEqual(request.method, 'HEAD')
self.assertEqual(request.get_method(), 'HEAD')
request = Request("http://www.python.org", method='GET')
self.assertEqual(request.get_method(), 'GET')
request.method = 'HEAD'
self.assertEqual(request.get_method(), 'HEAD')
示例11: get_method
# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import get_method [as 別名]
def get_method(self):
"""Return a string indicating the HTTP request method."""
default_method = "POST" if self.data is not None else "GET"
return getattr(self, 'method', default_method)
示例12: redirect_request
# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import get_method [as 別名]
def redirect_request(self, req, fp, code, msg, headers, newurl):
"""Return a Request or None in response to a redirect.
This is called by the http_error_30x methods when a
redirection response is received. If a redirection should
take place, return a new Request to allow http_error_30x to
perform the redirect. Otherwise, raise HTTPError if no-one
else should try to handle this url. Return None if you can't
but another Handler might.
"""
m = req.get_method()
if (not (code in (301, 302, 303, 307) and m in ("GET", "HEAD")
or code in (301, 302, 303) and m == "POST")):
raise HTTPError(req.full_url, code, msg, headers, fp)
# Strictly (according to RFC 2616), 301 or 302 in response to
# a POST MUST NOT cause a redirection without confirmation
# from the user (of urllib.request, in this case). In practice,
# essentially all clients do redirect in this case, so we do
# the same.
# Be conciliant with URIs containing a space. This is mainly
# redundant with the more complete encoding done in http_error_302(),
# but it is kept for compatibility with other callers.
newurl = newurl.replace(' ', '%20')
CONTENT_HEADERS = ("content-length", "content-type")
newheaders = {k: v for k, v in req.headers.items()
if k.lower() not in CONTENT_HEADERS}
return Request(newurl,
headers=newheaders,
origin_req_host=req.origin_req_host,
unverifiable=True)
# Implementation note: To avoid the server sending us into an
# infinite loop, the request object needs to track what URLs we
# have already seen. Do this by adding a handler-specific
# attribute to the Request object.
示例13: _get_content_length
# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import get_method [as 別名]
def _get_content_length(self, request):
return http.client.HTTPConnection._get_content_length(
request.data,
request.get_method())
示例14: _http_response
# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import get_method [as 別名]
def _http_response(self, request, response):
fetch = {
'url': request.full_url,
'method': request.get_method(),
'response_code': response.code,
'response_headers': response.headers,
}
self.fetches.append(fetch)
return response
示例15: check_url
# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import get_method [as 別名]
def check_url(url,ua):
global total_requests
total_requests += 1
request = urllib.request.Request(url)
request.add_header('User-Agent', ua)
request.get_method = lambda: 'HEAD'
try:
urllib.request.urlopen(request)
return '1'
except urllib.request.HTTPError:
return '0'