本文整理汇总了Python中urllib.request.OpenerDirector.add_handler方法的典型用法代码示例。如果您正苦于以下问题:Python OpenerDirector.add_handler方法的具体用法?Python OpenerDirector.add_handler怎么用?Python OpenerDirector.add_handler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib.request.OpenerDirector
的用法示例。
在下文中一共展示了OpenerDirector.add_handler方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fetch
# 需要导入模块: from urllib.request import OpenerDirector [as 别名]
# 或者: from urllib.request.OpenerDirector import add_handler [as 别名]
def fetch(self, request, opener=None, summary=None):
if not self.__enable_http:
return (None, None)
if opener is None:
opener = OpenerDirector()
opener.add_handler(HTTPDefaultErrorHandler())
opener.add_handler(HTTPSHandler())
t = time.clock()
response = opener.open(request)
body = response.read()
t = timedelta(seconds=time.clock() - t)
url = request.get_full_url()
self.__context.get_logger().info('HTTP time: %s\n%s' % (t, url))
if self.__log_http:
log_dir = os.path.join(self.__context.get_config_dir(), 'http-log')
makedirs(log_dir)
log_file = os.path.join(log_dir,
datetime.utcnow().strftime(
'%Y-%m-%d-%H-%M-%S-%f'))
if summary is not None:
log_file += '-' + _safe_str(summary)
fp = open(log_file, 'w')
fp.write('\n\n'.join([
request.get_full_url(),
request.get_data() or 'No request data',
body or 'No response body',
]))
fp.close()
return (response, body)
示例2: test_proxy_no_proxy
# 需要导入模块: from urllib.request import OpenerDirector [as 别名]
# 或者: from urllib.request.OpenerDirector import add_handler [as 别名]
def test_proxy_no_proxy(self):
os.environ['no_proxy'] = 'python.org'
o = OpenerDirector()
ph = urllib.request.ProxyHandler(dict(http="proxy.example.com"))
o.add_handler(ph)
req = Request("http://www.perl.org/")
self.assertEqual(req.get_host(), "www.perl.org")
r = o.open(req)
self.assertEqual(req.get_host(), "proxy.example.com")
req = Request("http://www.python.org")
self.assertEqual(req.get_host(), "www.python.org")
r = o.open(req)
self.assertEqual(req.get_host(), "www.python.org")
del os.environ['no_proxy']
示例3: test_basic_auth
# 需要导入模块: from urllib.request import OpenerDirector [as 别名]
# 或者: from urllib.request.OpenerDirector import add_handler [as 别名]
def test_basic_auth(self, quote_char='"'):
opener = OpenerDirector()
password_manager = MockPasswordManager()
auth_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
realm = "ACME Widget Store"
http_handler = MockHTTPHandler(
401, 'WWW-Authenticate: Basic realm=%s%s%s\r\n\r\n' %
(quote_char, realm, quote_char) )
opener.add_handler(auth_handler)
opener.add_handler(http_handler)
self._test_basic_auth(opener, auth_handler, "Authorization",
realm, http_handler, password_manager,
"http://acme.example.com/protected",
"http://acme.example.com/protected",
)
示例4: test_proxy_basic_auth
# 需要导入模块: from urllib.request import OpenerDirector [as 别名]
# 或者: from urllib.request.OpenerDirector import add_handler [as 别名]
def test_proxy_basic_auth(self):
opener = OpenerDirector()
ph = urllib.request.ProxyHandler(dict(http="proxy.example.com:3128"))
opener.add_handler(ph)
password_manager = MockPasswordManager()
auth_handler = urllib.request.ProxyBasicAuthHandler(password_manager)
realm = "ACME Networks"
http_handler = MockHTTPHandler(
407, 'Proxy-Authenticate: Basic realm="%s"\r\n\r\n' % realm)
opener.add_handler(auth_handler)
opener.add_handler(http_handler)
self._test_basic_auth(opener, auth_handler, "Proxy-authorization",
realm, http_handler, password_manager,
"http://acme.example.com:3128/protected",
"proxy.example.com:3128",
)
示例5: test_handler_order
# 需要导入模块: from urllib.request import OpenerDirector [as 别名]
# 或者: from urllib.request.OpenerDirector import add_handler [as 别名]
def test_handler_order(self):
o = OpenerDirector()
handlers = []
for meths, handler_order in [
([("http_open", "return self")], 500),
(["http_open"], 0),
]:
class MockHandlerSubclass(MockHandler): pass
h = MockHandlerSubclass(meths)
h.handler_order = handler_order
handlers.append(h)
o.add_handler(h)
r = o.open("http://example.com/")
# handlers called in reverse order, thanks to their sort order
self.assertEqual(o.calls[0][0], handlers[1])
self.assertEqual(o.calls[1][0], handlers[0])
示例6: build_opener
# 需要导入模块: from urllib.request import OpenerDirector [as 别名]
# 或者: from urllib.request.OpenerDirector import add_handler [as 别名]
def build_opener(*handlers, **kw):
"""Create an opener object from a list of handlers.
The opener will use several default handlers, including support
for HTTP and FTP.
If any of the handlers passed as arguments are subclasses of the
default handlers, the default handlers will not be used.
"""
def isclass(obj):
return isinstance(obj, class_type_) or hasattr(obj, "__bases__")
opener = OpenerDirector()
default_classes = [ProxyHandler, UnknownHandler, HTTPHandler,
HTTPDefaultErrorHandler, HTTPRedirectHandler,
FTPHandler, FileHandler, HTTPErrorProcessor]
check_classes = list(default_classes)
check_classes.append(HTTPSContextHandler)
skip = []
for klass in check_classes:
for check in handlers:
if isclass(check):
if issubclass(check, klass):
skip.append(klass)
elif isinstance(check, klass):
skip.append(klass)
for klass in default_classes:
if klass not in skip:
opener.add_handler(klass())
# Pick up SSL context from keyword settings
ssl_context = kw.get('ssl_context')
# Add the HTTPS handler with ssl_context
if HTTPSContextHandler not in skip:
opener.add_handler(HTTPSContextHandler(ssl_context))
for h in handlers:
if isclass(h):
h = h()
opener.add_handler(h)
return opener
示例7: build_test_opener
# 需要导入模块: from urllib.request import OpenerDirector [as 别名]
# 或者: from urllib.request.OpenerDirector import add_handler [as 别名]
def build_test_opener(*handler_instances):
opener = OpenerDirector()
for h in handler_instances:
opener.add_handler(h)
return opener
示例8: HeadRequest
# 需要导入模块: from urllib.request import OpenerDirector [as 别名]
# 或者: from urllib.request.OpenerDirector import add_handler [as 别名]
auth_token = self._get_outh2_token(www_authenticate_header)
request.add_unredirected_header('Authorization', 'Bearer ' + auth_token)
return self.parent.open(request, timeout=request.timeout)
# Got some help from this example https://gist.github.com/FiloSottile/2077115
class HeadRequest(Request):
def get_method(self):
return "HEAD"
better_urllib_get = OpenerDirector()
better_urllib_get.addheaders = DEFAULT_HEADERS.copy()
better_urllib_get.add_handler(HTTPHandler())
better_urllib_get.add_handler(HTTPSHandler())
better_urllib_get.add_handler(HTTPRedirectHandler())
better_urllib_get.add_handler(SocketFileHandler())
better_urllib_get.add_handler(Oauth2TokenAuthHandler())
class RegistryError(Exception):
def __init__(self, response):
self.response_obj = response
# Util functions
#############################################################################################
def parse_thresholds(spec, include_units=True, units_required=True):
"""
示例9: connect
# 需要导入模块: from urllib.request import OpenerDirector [as 别名]
# 或者: from urllib.request.OpenerDirector import add_handler [as 别名]
def connect(self):
self.sock = socket.socket(family=socket.AF_UNIX, type=socket.SOCK_STREAM, proto=0, fileno=None)
self.sock.settimeout(self.timeout)
self.sock.connect(self.socket_file)
def socket_open(self, req):
socket_file, path = req.selector.split(':', 1)
req.host = socket_file
req.selector = path
return self.do_open(self.SocketFileToHttpConnectionAdaptor, req)
better_urllib_get = OpenerDirector()
better_urllib_get.addheaders = DEFAULT_HEADERS.copy()
better_urllib_get.add_handler(HTTPHandler())
better_urllib_get.add_handler(HTTPSHandler())
better_urllib_get.add_handler(SocketFileHandler())
# Util functions
#############################################################################################
@lru_cache()
def get_url(url):
response = better_urllib_get.open(url, timeout=timeout)
return process_urllib_response(response), response.status
def process_urllib_response(response):