我想加載遠程服務器證書並將其保存在本地磁盤中。這是我使用的python腳本:無法使用python將證書保存到本地磁盤
from M2Crypto.X509 import FORMAT_PEM
import StringIO
import traceback
from M2Crypto.Err import SSLError
import ssl
import socket
import pprint
import M2Crypto
from M2Crypto import X509, RSA
from datetime import datetime
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.verify_mode = ssl.CERT_NONE
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
port = 443
host='216.58.212.67' #google
#creating ssl socket
ssock = context.wrap_socket(socket.socket(socket.AF_INET), server_hostname=host)
#ssl connection
try:
ssock.connect((host, port))
except socket.error: #if tls connection is not possible
print "Faile connection with: " + host
#get the certificate
cert = ssock.getpeercert(True)
x509 = M2Crypto.X509.load_cert_der_string(cert)
x509_file= M2Crypto.X509.load_cert('C:/Users/xxx/Documents/temp',format=FORMAT_PEM)
當我運行它,我得到這個錯誤:
Traceback (most recent call last):
File "C:/Users/ealas/PycharmProjects/tlsScan/test.py", line 36, in <module>
x509_file= M2Crypto.X509.load_cert('C:/Users/xxx/Documents/temp',format=FORMAT_PEM)
File "C:\Python27\lib\site-packages\M2Crypto\X509.py", line 609, in load_cert
bio = BIO.openfile(file)
File "C:\Python27\lib\site-packages\M2Crypto\BIO.py", line 186, in openfile
return File(open(filename, mode))
IOError: [Errno 13] Permission denied: 'C:/Users/xxx/Documents/temp'
什麼是錯在我的代碼嗎?
那麼,該文件的權限是什麼?看起來你的代碼沒有任何問題。 –
這是一個文件夾。腳本應該在其上寫入證書。 – user2192774
這就是問題所在。您無法將數據寫入文件夾,您必須在該文件夾中指定一個文件。 –