赞
踩
- import urllib.request
-
- import ssl
-
- def send_request():
- CA_FILE = r"D:\station\python_demo\cert\xxx.cer"
- # 如果服务端需要验证客户端证书,则需要额外传入以下两个参数
- # KEY_FILE = "client.key"
- # CERT_FILE = "client.crt"
-
- # 这里屏蔽了不安全的TLSv1与V1.1协议
- context = ssl.SSLContext(ssl.PROTOCOL_TLS)
- context.options |= ssl.OP_NO_TLSv1
- context.options |= ssl.OP_NO_TLSv1_1
- # 这里不检查证书的hostname是否与服务端一致,这是用来连接存储的api管理工具的,一般这个hostname都对不上
- context.check_hostname = False
- # context.load_cert_chain(certfile=CERT_FILE, keyfile=KEY_FILE)
- # 加载受信任的ca列表
- context.load_verify_locations(CA_FILE)
- # context.verify_mode = ssl.CERT_REQUIRED
- try:
- # 通过request()方法创建一个请求:
- request = urllib.request.Request('https://x.x.x.x:xxxx')
- res = urllib.request.urlopen(request, context=context)
- print(res.code)
- print(res.read().decode("utf-8"))
- except Exception as ex:
- print("Found Error in auth phase:%s" % str(ex))
-
-
- if __name__ == '__main__':
- send_request()
如果使用requests包则使用一下方法:
- import requests
-
- ca_file = r'D:\station\python_demo\cert\xxxx.cer'
- ret = requests.get(url="https://8.46.19.193:8088", verify=ca_file)
但是使用该方法目前还不知道怎么跳过hostname验证
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。