本文整理汇总了Python中urllib.request.HTTPHandler方法的典型用法代码示例。如果您正苦于以下问题:Python request.HTTPHandler方法的具体用法?Python request.HTTPHandler怎么用?Python request.HTTPHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib.request
的用法示例。
在下文中一共展示了request.HTTPHandler方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: send_response
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPHandler [as 别名]
def send_response(event, context, response_status, response_data):
'''Send a resource manipulation status response to CloudFormation'''
response_body = json.dumps({
"Status": response_status,
"Reason": "See the details in CloudWatch Log Stream: " + context.log_stream_name,
"PhysicalResourceId": context.log_stream_name,
"StackId": event['StackId'],
"RequestId": event['RequestId'],
"LogicalResourceId": event['LogicalResourceId'],
"Data": response_data
})
logger.info('ResponseURL: %s', event['ResponseURL'])
logger.info('ResponseBody: %s', response_body)
opener = build_opener(HTTPHandler)
request = Request(event['ResponseURL'], data=response_body.encode('utf-8'))
request.add_header('Content-Type', '')
request.add_header('Content-Length', len(response_body))
request.get_method = lambda: 'PUT'
response = opener.open(request)
logger.info("Status code: %s", response.getcode())
logger.info("Status message: %s", response.msg)
示例2: send_response
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPHandler [as 别名]
def send_response(event, context, response_status, response_data):
"""
Send a resource manipulation status response to CloudFormation
"""
response_body = json.dumps({
"Status": response_status,
"Reason": "See the details in CloudWatch Log Stream: " + context.log_stream_name,
"PhysicalResourceId": context.log_stream_name,
"StackId": event['StackId'],
"RequestId": event['RequestId'],
"LogicalResourceId": event['LogicalResourceId'],
"Data": response_data
})
LOGGER.info('ResponseURL: {s}'.format(s=event['ResponseURL']))
LOGGER.info('ResponseBody: {s}'.format(s=response_body))
opener = build_opener(HTTPHandler)
request = Request(event['ResponseURL'], data=response_body.encode('utf-8'))
request.add_header('Content-Type', '')
request.add_header('Content-Length', len(response_body))
request.get_method = lambda: 'PUT'
response = opener.open(request)
LOGGER.info("Status code: {s}".format(s=response.getcode))
LOGGER.info("Status message: {s}".format(s=response.msg))
示例3: delete
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPHandler [as 别名]
def delete(uri, params={}, headers={}):
data = None # always none in GET
if params:
uri = "%s?%s" % (uri, urlencode(params))
url_opener = build_opener(HTTPHandler)
req = Request(uri, data)
req.get_method = lambda: 'DELETE'
for (k, v) in headers.items():
req.add_header(k, v)
request = url_opener.open(req)
response = request.read()
# code = request.code
return response
示例4: post
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPHandler [as 别名]
def post(uri, params={}, headers={}):
data = None # always none in GET
if params:
# TODO: sure it's json and not urlencode?
# data = urlencode(params)
data = unicode_to_bytes(jsoner.dumps(params))
url_opener = build_opener(HTTPHandler)
req = Request(uri, data)
req.get_method = lambda: 'POST'
for (k, v) in headers.items():
req.add_header(k, v)
request = url_opener.open(req)
response = request.read()
# code = request.code
return response
示例5: put
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPHandler [as 别名]
def put(uri, data=None, params={}, headers=None):
# data = None # always none in GET
if headers is None:
headers = {}
if params:
# TODO: sure it's json and not urlencode?
# data = urlencode(params)
uri = "%s?%s" % (uri, urlencode(params))
headers['Content-Type'] = 'your/contenttype'
url_opener = build_opener(HTTPHandler)
req = Request(uri, data)
req.get_method = lambda: 'PUT'
for (k, v) in headers.items():
req.add_header(k, v)
request = url_opener.open(req)
response = request.read()
# code = request.code
return response
示例6: default_handler
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPHandler [as 别名]
def default_handler(url, method, timeout, headers, data):
"""Default handler that implements HTTP/HTTPS connections.
Used by the push_to_gateway functions. Can be re-used by other handlers."""
def handle():
request = Request(url, data=data)
request.get_method = lambda: method
for k, v in headers:
request.add_header(k, v)
resp = build_opener(HTTPHandler).open(request, timeout=timeout)
if resp.code >= 400:
raise IOError("error talking to pushgateway: {0} {1}".format(
resp.code, resp.msg))
return handle
示例7: getFile
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPHandler [as 别名]
def getFile(cls, getfile, unpack=True):
if cls.getProxy():
proxy = req.ProxyHandler({'http': cls.getProxy(), 'https': cls.getProxy()})
auth = req.HTTPBasicAuthHandler()
opener = req.build_opener(proxy, auth, req.HTTPHandler)
req.install_opener(opener)
if cls.ignoreCerts():
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
opener = req.build_opener(urllib.request.HTTPSHandler(context=ctx))
req.install_opener(opener)
response = req.urlopen(getfile)
data = response
# TODO: if data == text/plain; charset=utf-8, read and decode
if unpack:
if 'gzip' in response.info().get('Content-Type'):
buf = BytesIO(response.read())
data = gzip.GzipFile(fileobj=buf)
elif 'bzip2' in response.info().get('Content-Type'):
data = BytesIO(bz2.decompress(response.read()))
elif 'zip' in response.info().get('Content-Type'):
fzip = zipfile.ZipFile(BytesIO(response.read()), 'r')
if len(fzip.namelist())>0:
data=BytesIO(fzip.read(fzip.namelist()[0]))
return (data, response)
# Feeds
示例8: __init__
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPHandler [as 别名]
def __init__(self, *args, **kwargs):
self.args = args
self.kw = kwargs
urllib2.HTTPHandler.__init__(self)
示例9: hashed_download
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPHandler [as 别名]
def hashed_download(url, temp, digest):
"""Download ``url`` to ``temp``, make sure it has the SHA-256 ``digest``,
and return its path."""
# Based on pip 1.4.1's URLOpener but with cert verification removed
def opener():
opener = build_opener(HTTPSHandler())
# Strip out HTTPHandler to prevent MITM spoof:
for handler in opener.handlers:
if isinstance(handler, HTTPHandler):
opener.handlers.remove(handler)
return opener
def read_chunks(response, chunk_size):
while True:
chunk = response.read(chunk_size)
if not chunk:
break
yield chunk
response = opener().open(url)
path = join(temp, urlparse(url).path.split('/')[-1])
actual_hash = sha256()
with open(path, 'wb') as file:
for chunk in read_chunks(response, 4096):
file.write(chunk)
actual_hash.update(chunk)
actual_digest = actual_hash.hexdigest()
if actual_digest != digest:
raise HashError(url, path, actual_digest, digest)
return path
示例10: __init__
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPHandler [as 别名]
def __init__(self, base_url="", tag="", cookiejar=None, debug=False):
self.tag = tag
hh = urllib2.HTTPHandler( debuglevel=1 if debug else 0)
hsh = urllib2.HTTPSHandler(debuglevel=1 if debug else 0)
cp = urllib2.HTTPCookieProcessor(cookiejar)
self._opener = urllib2.build_opener(hh, hsh, cp)
scheme, netloc, path, q, f = urlparse.urlsplit(base_url, "http")
if not netloc:
netloc, _, path = path.partition('/')
self.base_url = urlparse.urlunsplit((scheme, netloc, path, q, f))
示例11: __open
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPHandler [as 别名]
def __open(self, url, headers={}, data=None, baseurl=""):
"""Raw urlopen command"""
if not baseurl:
baseurl = self.baseurl
req = Request("%s%s" % (baseurl, url), headers=headers)
try:
req.data = urlencode(data).encode('utf-8') # Python 3
except:
try:
req.add_data(urlencode(data)) # Python 2
except:
pass
# Proxy support
if self.proxy_url:
if self.proxy_user:
proxy = ProxyHandler({'https': 'https://%s:%s@%s' % (self.proxy_user,
self.proxy_password,
self.proxy_url)})
auth = HTTPBasicAuthHandler()
opener = build_opener(proxy, auth, HTTPHandler)
else:
handler = ProxyHandler({'https': self.proxy_url})
opener = build_opener(handler)
else:
opener = build_opener()
resp = opener.open(req)
charset = resp.info().get('charset', 'utf-8')
return json.loads(resp.read().decode(charset))
示例12: get_stock_html
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPHandler [as 别名]
def get_stock_html(URL):
opener = urllib2.build_opener(
urllib2.HTTPRedirectHandler(),
urllib2.HTTPHandler(debuglevel=0),
)
opener.addheaders = [
('User-agent',
'Mozilla/4.0 (compatible;MSIE 7.0;'
'Windows NT 5.1; .NET CLR 2.0.50727;'
'.NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)')
]
url = "http://proxy.com.ru/%s" % URL
response = opener.open(url)
return str(b''.join(response.readlines()))
示例13: get
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPHandler [as 别名]
def get(self, uri, params={}, headers={}, with_status_code=False, timeout=10, user=None, password=None):
data = None # always none in GET
if params:
uri = "%s?%s" % (uri, urlencode(params))
# SSL, user/password and basic
# NOTE: currently don't manage ssl & user/password
if uri.startswith('https://'):
handler = HTTPSHandler(context=self.ssl_context)
elif user and password:
passwordMgr = HTTPPasswordMgrWithDefaultRealm()
passwordMgr.add_password(None, uri, user, password)
handler = HTTPBasicAuthHandler(passwordMgr)
else:
handler = HTTPHandler
url_opener = build_opener(handler)
req = Request(uri, data)
req.get_method = lambda: 'GET'
for (k, v) in headers.items():
req.add_header(k, v)
request = url_opener.open(req, timeout=timeout)
response = request.read()
status_code = request.code
request.close()
if not with_status_code:
return response
else:
return (status_code, response)
示例14: get_local
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPHandler [as 别名]
def get_local(u, local_socket, params={}, method='GET', timeout=10):
UnixHTTPConnection.socket_timeout = timeout
data = None
special_headers = []
if method == 'GET' and params:
u = "%s?%s" % (u, urlencode(params))
if method == 'POST' and params:
data = string_encode(urlencode(params))
if method == 'PUT' and params:
special_headers.append(('Content-Type', 'your/contenttype'))
data = string_encode(params)
# not the same way to connect
# * windows: TCP
# * unix : unix socket
if os.name == 'nt':
url_opener = build_opener(HTTPHandler)
uri = 'http://127.0.0.1:6770%s' % u
else: # unix
url_opener = build_opener(UnixSocketHandler())
uri = 'unix:/%s%s' % (local_socket, u)
logger.debug("Connecting to local http/unix socket at: %s with method %s" % (uri, method))
req = Request(uri, data)
req.get_method = lambda: method
for (k, v) in special_headers:
req.add_header(k, v)
request = url_opener.open(req)
response = request.read()
code = request.code
return (code, response)
示例15: __init__
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPHandler [as 别名]
def __init__(self, unix_socket, **kwargs):
urllib_request.HTTPHandler.__init__(self, **kwargs)
self._unix_socket = unix_socket