您的位置:首页 > 理论基础 > 计算机网络

python HTTPSconnection SSL 访问https

2015-12-23 10:05 465 查看
在做一些敏感信息交互中往往需要使用到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 加密 信息安全