本文整理汇总了Python中urllib3.HTTPSConnectionPool.urlopen方法的典型用法代码示例。如果您正苦于以下问题:Python HTTPSConnectionPool.urlopen方法的具体用法?Python HTTPSConnectionPool.urlopen怎么用?Python HTTPSConnectionPool.urlopen使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib3.HTTPSConnectionPool
的用法示例。
在下文中一共展示了HTTPSConnectionPool.urlopen方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: request
# 需要导入模块: from urllib3 import HTTPSConnectionPool [as 别名]
# 或者: from urllib3.HTTPSConnectionPool import urlopen [as 别名]
def request():
try:
pool = HTTPSConnectionPool(self.host, self.port, assert_fingerprint=fingerprint)
response = pool.urlopen("GET", "/", preload_content=False, timeout=Timeout(connect=1, read=0.001))
response.read()
finally:
pool.close()
示例2: test_ssl_read_timeout
# 需要导入模块: from urllib3 import HTTPSConnectionPool [as 别名]
# 或者: from urllib3.HTTPSConnectionPool import urlopen [as 别名]
def test_ssl_read_timeout(self):
timed_out = Event()
def socket_handler(listener):
sock = listener.accept()[0]
ssl_sock = ssl.wrap_socket(sock,
server_side=True,
keyfile=DEFAULT_CERTS['keyfile'],
certfile=DEFAULT_CERTS['certfile'],
ca_certs=DEFAULT_CA)
buf = b''
while not buf.endswith(b'\r\n\r\n'):
buf += ssl_sock.recv(65536)
# Send incomplete message (note Content-Length)
ssl_sock.send((
'HTTP/1.1 200 OK\r\n'
'Content-Type: text/plain\r\n'
'Content-Length: 10\r\n'
'\r\n'
'Hi-').encode('utf-8'))
timed_out.wait()
sock.close()
ssl_sock.close()
self._start_server(socket_handler)
pool = HTTPSConnectionPool(self.host, self.port)
self.addCleanup(pool.close)
response = pool.urlopen('GET', '/', retries=0, preload_content=False,
timeout=Timeout(connect=1, read=0.001))
try:
self.assertRaises(ReadTimeoutError, response.read)
finally:
timed_out.set()
示例3: test_ssl_read_timeout
# 需要导入模块: from urllib3 import HTTPSConnectionPool [as 别名]
# 或者: from urllib3.HTTPSConnectionPool import urlopen [as 别名]
def test_ssl_read_timeout(self):
timed_out = Event()
def socket_handler(listener):
sock = listener.accept()[0]
ssl_sock = ssl.wrap_socket(
sock,
server_side=True,
keyfile=DEFAULT_CERTS["keyfile"],
certfile=DEFAULT_CERTS["certfile"],
ca_certs=DEFAULT_CA,
)
buf = b""
while not buf.endswith(b"\r\n\r\n"):
buf += ssl_sock.recv(65536)
# Send incomplete message (note Content-Length)
ssl_sock.send(
("HTTP/1.1 200 OK\r\n" "Content-Type: text/plain\r\n" "Content-Length: 10\r\n" "\r\n" "Hi-").encode(
"utf-8"
)
)
timed_out.wait()
sock.close()
ssl_sock.close()
self._start_server(socket_handler)
pool = HTTPSConnectionPool(self.host, self.port)
response = pool.urlopen("GET", "/", retries=0, preload_content=False, timeout=Timeout(connect=1, read=0.001))
try:
self.assertRaises(ReadTimeoutError, response.read)
finally:
timed_out.set()
示例4: OttProbe
# 需要导入模块: from urllib3 import HTTPSConnectionPool [as 别名]
# 或者: from urllib3.HTTPSConnectionPool import urlopen [as 别名]
class OttProbe():
"""
This class manages interactions with the supervisor:
registration, specification retrievement, and return of results
"""
def __init__(self, config):
"""
Initiates a OTT probe for component-initiated workflow.
Command line parameters ported to config file equivalents.
"""
self.config = config
self.supervisorhost = None
if "supervisorhost" in config["module_ott"]:
self.supervisorhost = config["module_ott"]["supervisorhost"]
self.supervisorhost = self.supervisorhost or SUPERVISOR_IP4
self.supervisorport = None
if "supervisorport" in config["module_ott"]:
self.supervisorport = config["module_ott"]["supervisorport"]
self.supervisorport = self.supervisorport or SUPERVISOR_PORT
self.ip4addr = None
if "ip4addr" in config["module_ott"]:
self.ip4addr = config["module_ott"]["ip4addr"]
self.ip4addr = self.ip4addr or IP4ADDR
print(">>> supervisor = " + self.supervisorhost + ":" + str(self.supervisorport) + ", ip4addr = "+ str(self.ip4addr) + "\n")
tls_state = mplane.tls.TlsState(config)
self.dn = self.get_dn( tls_state._keyfile, tls_state)
self.forged_identity = mplane.tls.TlsState.extract_local_identity(tls_state, None)
headers={"content-type": "application/x-mplane+json"}
if tls_state._keyfile is None:
if( self.forged_identity is not None ):
headers={"content-type": "application/x-mplane+json","Forged-MPlane-Identity": self.forged_identity}
self.pool = HTTPConnectionPool(self.supervisorhost, self.supervisorport, headers=headers)
else:
self.pool = HTTPSConnectionPool(self.supervisorhost, self.supervisorport, key_file=tls_state._keyfile, cert_file=tls_state._certfile, ca_certs=tls_state._cafile, headers=headers)
self.scheduler = mplane.scheduler.Scheduler(self.config)
if self.ip4addr is not None:
self.scheduler.add_service(OttService(ott_capability(self.ip4addr)))
# self.scheduler.add_service(OttService(cap(self.ip4addr)))
def register_capabilities(self):
print( ">>> register_capabilities(): register with supervisor at " + self.supervisorhost + ":" + str(self.supervisorport) )
caps_list = ""
for key in self.scheduler.capability_keys():
cap = self.scheduler.capability_for_key(key)
#if (self.scheduler.ac.check_azn(cap._label, self.dn)):
if (self.scheduler.azn.check(cap, self.dn)):
caps_list = caps_list + mplane.model.unparse_json(cap) + ","
caps_list = "[" + caps_list[:-1].replace("\n","") + "]"
print( ">>> register_capabilities(): caps_list = \n" + caps_list )
if self.forged_identity is None:
self.forged_identity = ""
print( ">>> register_capabilities(): self.forged_identity = " + self.forged_identity )
while True:
try:
# print(">>> register_capabilities(): urlopen with headers={content-type: application/x-mplane+json, Forged-MPlane-Identity: " + self.forged_identity + "})\n")
res = self.pool.urlopen('POST', "/" + REGISTRATION_PATH,
body=caps_list.encode("utf-8"),
headers={"content-type": "application/x-mplane+json", "Forged-MPlane-Identity": self.forged_identity})
if res.status == 200:
body = json.loads(res.data.decode("utf-8"))
print("\nCapability registration outcome:")
for key in body:
if body[key]['registered'] == "ok":
print(key + ": Ok")
else:
print(key + ": Failed (" + body[key]['reason'] + ")")
print("")
else:
print("Error registering capabilities, Supervisor said: " + str(res.status) + " - " + res.data.decode("utf-8"))
exit(1)
break
except:
print("Supervisor unreachable. Retrying connection in 5 seconds")
sleep(5)
def check_for_specs(self):
"""
Poll the supervisor for specifications
"""
url = "/" + SPECIFICATION_PATH
# send a request for specifications
res = self.pool.request('GET', url, headers={"Forged-MPlane-Identity": self.forged_identity})
if res.status == 200:
#.........这里部分代码省略.........