本文整理汇总了Python中urllib.request.Request.method方法的典型用法代码示例。如果您正苦于以下问题:Python Request.method方法的具体用法?Python Request.method怎么用?Python Request.method使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib.request.Request
的用法示例。
在下文中一共展示了Request.method方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: raw
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import method [as 别名]
def raw(ctx, method, url, datafp=None):
"""Do the raw http method call on url."""
if method not in ("GET", "POST", "PUT", "DELETE"):
raise ValueError("HTTP method '{}' is not known".format(method))
if method in ("PUT", "DELETE"):
raise NotImplementedError("HTTP method '{}' is not yet implemented".format(method))
#TODO: we need a real debugging
print("DEBUG: {} {}".format(method, url))
data = None
request = Request(url)
if hasattr(request, method):
request.method = method
else:
request.get_method = lambda: method
if method == "POST":
data = datafp.read() if datafp is not None else b""
request.add_header("Content-Type", "application/octet-stream")
response = ctx.opener.open(request, data)
if response.getcode() != 200:
raise NotImplementedError("non 200 responses are not yet implemented")
return response
示例2: getUsers
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import method [as 别名]
def getUsers(self, users):
req = Request("https://api.twitter.com/1.1/users/lookup.json")
req.method = "POST"
req.data = urllib.parse.urlencode({"screen_name": ",".join(users)}).encode()
req.add_header("Authorization", "Bearer {}".format(self.bearerToken))
req.add_header("Content-type", "application/x-www-form-urlencoded")
with urlopen(req) as resp:
return json.loads(resp.read().decode())
示例3: retrieveBearerToken
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import method [as 别名]
def retrieveBearerToken(self):
req = Request("https://api.twitter.com/oauth2/token")
req.method = "POST"
req.add_header("Authorization", "Basic {}".format(self.key))
req.add_header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
req.data = b"grant_type=client_credentials"
with urlopen(req) as resp:
data = json.loads(resp.read().decode())
self.bearerToken = data["access_token"]
示例4: hit_url
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import method [as 别名]
def hit_url(url, data, method):
if data:
data = json.dumps(data).encode('utf-8')
r = Request(url, data=data)
r.method = method
r.get_method = lambda: method
r.add_header('Authorization', 'Basic %s' % get_basic_auth())
r.add_header('Accept', 'application/json')
r.add_header('Content-type', 'application/json')
r.add_header('User-Agent', user_agent())
return urlopen(r, timeout=5)
示例5: hit_url
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import method [as 别名]
def hit_url(host, url, data, method):
if data:
data = json.dumps(data).encode("utf-8")
msg.debug("url: ", url, " method: ", method, " data: ", data)
r = Request(url, data=data)
r.method = method
r.get_method = lambda: method
auth = get_basic_auth(host)
if auth:
r.add_header("Authorization", "Basic %s" % auth)
r.add_header("Accept", "application/json")
r.add_header("Content-type", "application/json")
r.add_header("User-Agent", user_agent())
return urlopen(r, timeout=5)
示例6: hit_url
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import method [as 别名]
def hit_url(url, data, method):
if data:
data = json.dumps(data).encode("utf-8")
r = Request(url, data=data)
r.method = method
r.get_method = lambda: method
r.add_header("Authorization", "Basic %s" % get_basic_auth())
r.add_header("Accept", "application/json")
r.add_header("Content-type", "application/json")
r.add_header(
"User-Agent",
"Floobits Plugin %s %s %s py-%s.%s"
% (editor.name(), G.__PLUGIN_VERSION__, editor.platform(), sys.version_info[0], sys.version_info[1]),
)
return urlopen(r, timeout=5)
示例7: hit_url
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import method [as 别名]
def hit_url(host, url, data, method):
if data:
data = json.dumps(data).encode('utf-8')
msg.debug('url: ', url, ' method: ', method, ' data: ', data)
r = Request(url, data=data)
r.method = method
r.get_method = lambda: method
auth = get_basic_auth(host)
if auth:
r.add_header('Authorization', 'Basic %s' % auth)
r.add_header('Accept', 'application/json')
r.add_header('Content-type', 'application/json')
r.add_header('User-Agent', user_agent())
cafile = os.path.join(G.BASE_DIR, 'floobits.pem')
with open(cafile, 'wb') as cert_fd:
cert_fd.write(cert.CA_CERT.encode('utf-8'))
return urlopen(r, timeout=10, cafile=cafile)
示例8: _dl_to_cache
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import method [as 别名]
def _dl_to_cache(self, url):
"""Downloads a file and stores it in cachedir().
Doesn't do any existence or redundancy checking."""
fname = uuid4().urn[9:]
cached = {'id': fname}
req = Request(url)
req.method = 'HEAD'
resp = urlopen(req)
lastmod = resp.getheader('Last-Modified')
etag = resp.getheader('ETag')
if lastmod is not None:
cached['last-modified'] = lastmod
elif etag is not None:
cached['etag'] = etag
self._curl_(url, self.cachedir(fname))
self._cache_[url] = cached
self._save_cache_()
return self._dl_from_cache_(url)