本篇文章给大家分享的是有关怎样用原生的urllib2 httplib请求Https,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
环境
python2.7.5
# https服务器生成证书
https://www.cnblogs.com/loleina/p/8418111.html
#HTTPSConnection
密钥# https服务器使用的键
ca_certs #https服务器使用的加拿大
证书#在浏览器下载的证书窗户下载后Linux操作系统操作系统可以使用
### https.py
导入urllib2、httplib、ssl、套接字
DEFAULT _ HTTP _ TIMEOUT=10 #秒
# http://代码。活动状态。com/recipes/577548-https-httplib-client-connection-with-certificate-v/
# http://stackoverflow。com/questions/1875052/using-pain-certificates-with-URL lib 2
类httpclientauthhandler(URL lib 2 .httphandler):
'''
允许使用HTTPS连接发送客户端证书。
这个版本也验证了对等(服务器)证书,因为.
如果你不认证和你说话的人,WTF就是加密套接字协议层的意义所在!
'''
def __init__(自身,密钥=无,证书=无,ca _ certs=无,ssl _ version=无,密码=无):
urllib 2。Httpshandler。_ _ init _ _(自我)
self.key=key
自我认证
self.ca_certs=ca_certs
self.ssl_version=ssl_version
self.ciphers=密码
def https_open(self,req):
#我们不是传入对连接类的引用,而是传入
#对功能的引用,出于所有意图和目的,
#将充当构造函数
返回自我。do _ open(self。获取连接,请求)
def get_connection(自身,主机,超时=DEFAULT _ HTTP _超时):
返回HTTPSConnection(主机,
key_file=self.key,
cert_file=self.cert,
> timeout = timeout,
ciphers = self.ciphers,
ca_certs = self.ca_certs )
class HTTPSConnection(httplib.HTTPSConnection):
'''
Overridden to allow peer certificate validation, configuration
of SSL/ TLS version and cipher selection. See:
http://hg.python.org/cpython/file/c1c45755397b/Lib/httplib.py#l1144
and `ssl.wrap_socket()`
'''
def __init__(self, host, **kwargs):
self.ciphers = kwargs.pop('ciphers',None)
self.ca_certs = kwargs.pop('ca_certs',None)
self.ssl_version = kwargs.pop('ssl_version', ssl.PROTOCOL_SSLv23)
httplib.HTTPSConnection.__init__(self,host,**kwargs)
def connect(self):
sock = socket.create_connection( (self.host, self.port), self.timeout )
if self._tunnel_host:
self.sock = sock
self._tunnel()
self.sock = ssl.wrap_socket( sock,
keyfile = self.key_file,
certfile = self.cert_file,
ca_certs = self.ca_certs,
cert_reqs = ssl.CERT_REQUIRED if self.ca_certs else ssl.CERT_NONE )
### test.py
import urllib2
import urllib
import https
import ssl
import json
client_cert_key = "etcd-client-key.pem" # file path
client_cert_pem = "etcd-client.pem" # file path
ca_certs = "etcd-ca.pem" # file path
handlers = []
handlers.append( https.HTTPSClientAuthHandler(
key = client_cert_key,
cert = client_cert_pem,
ca_certs = ca_certs,
ssl_version = ssl.PROTOCOL_SSLv23,
ciphers = 'TLS_RSA_WITH_AES_256_CBC_SHA' ) )
http = urllib2.build_opener(*handlers)
# request https
# GET
resp = http.open('https://xxxx:2379/v2/members')
data = resp.read()
# POST
req = urllib2.Request(url)
data = urllib.urlencode(data)
resp = http.open(req, data)
# PUT
request = urllib2.Request(url, data=json_data)
request.add_header('Content-Type', 'application/json')
request.get_method = lambda: 'PUT'
resp = http.open(request)
# DELETE
request = urllib2.Request(url, data=data)
request.get_method = lambda: 'DELETE'
resp = http.open(request)
resp.close()
以上就是怎样用原生的urllib2+httplib请求Https,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/133806.html