在做一些敏感信息交互中往往需要使用到https来加强信息安全性。
交互双方需要交换RAS密钥,进行数据的加密和校验。
今天需要使用http SSL通道来传输数据,所以写下来做个备忘。
不需要特定的私钥文件(在跟使用urllib2.urlopen()差不多),代码如下:
httpsConn = httplib.HTTPSConnection("www.baidu.com")
httpsConn.request("GET", "/")
res = httpsConn.getresponse()
print res.status, res.reason, res.read()
需要使用到特定证书的方式(.pem证书),代码如下
# coding=utf8
import ssl
import Properties
from httplib import HTTPSConnection
import base64
def send(path):
port = Properties.SSLPORT
host = Properties.HOST
url = host + ":" + str(port) + path
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.load_cert_chain(Properties.PRIVATE_KEY, **{"password":Properties.PASSWORD})
data = '123'
httpsConn = HTTPSConnection(host, port, None, None, "", 60000, "", context)
httpsConn.request("POST", path, data, {"Authorization": "Basic " + base64.encodestring("12345678")})
res = httpsConn.getresponse()
print res.status,res.reason, res.getheaders(), res.read()
if __name__=="__main__":
"""
httpsConn = httplib.HTTPSConnection("www.baidu.com")
httpsConn.request("GET", "/")
res = httpsConn.getresponse()
print res.status, res.reason, len(res.read())
"""
send("/post")

本文介绍了在Python中如何使用HTTPSconnection进行SSL安全访问,特别是在处理敏感信息交互时,通过交换RSA密钥确保数据的加密和校验。文中分别展示了无需特定私钥文件和使用.pem证书的两种实现方式。
1万+

被折叠的 条评论
为什么被折叠?



