❶ 怎麼用python 來實現 SSL登錄FTP python中有ftplib模塊,但它是普通登錄,也有ssl模塊,怎麼來實現呢
從python 2.7開始ftplib模塊就有一個支持ftp ssl的類FTP_TLS了
具體可以看這里
http://docs.python.org/library/ftplib.html#ftplib.FTP_TLS
❷ 如何驗證在Python SSL證書
importos
importglob
fromOpenSSL.SSLimportContext,TLSv1_METHOD,VERIFY_PEER,VERIFY_FAIL_IF_NO_PEER_CERT,OP_NO_SSLv2
fromOpenSSL.cryptoimportload_certificate,FILETYPE_PEM
fromtwisted.python.urlpathimportURLPath
fromtwisted.internet.sslimportContextFactory
fromtwisted.internetimportreactor
fromtwisted.web.clientimportgetPage
certificateAuthorityMap={}
forcertFileNameinglob.glob("/etc/ssl/certs/*.pem"):
#,solet'smakesureit'sreal.
ifos.path.exists(certFileName):
data=open(certFileName).read()
x509=load_certificate(FILETYPE_PEM,data)
digest=x509.digest('sha1')
#Now,de-.
certificateAuthorityMap[digest]=x509
(ContextFactory):
def__init__(self,hostname):
self.hostname=hostname
isClient=True
defgetContext(self):
ctx=Context(TLSv1_METHOD)
store=ctx.get_cert_store()
.values():
store.add_cert(value)
ctx.set_verify(VERIFY_PEER|VERIFY_FAIL_IF_NO_PEER_CERT,self.verifyHostname)
ctx.set_options(OP_NO_SSLv2)
returnctx
defverifyHostname(self,connection,x509,errno,depth,preverifyOK):
ifpreverifyOK:
ifself.hostname!=x509.get_subject().commonName:
returnFalse
returnpreverifyOK
defsecureGet(url):
returngetPage(url,HTTPSVerifyingContextFactory(URLPath.fromString(url).netloc))
defdone(result):
print'Done!',len(result)
secureGet("https://google.com/").addCallback(done)
reactor.run()
這個驗證要安裝Twisted 庫。
當然這樣的庫好多,如:pycurl,requests,M2Crypto
importrequests
requests.get('https://somesite.com',cert='/path/server.crt',verify=True)
❸ python3 代碼怎樣關閉url與ssl屬性校驗
importurllib.request
importssl
url="httpsurl"
header=headers={
"User-Agent":"Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0.963.56Safari/535.11"
}
request=urllib.request.Request(url,headers=header)
context=ssl._create_unverified_context()#忽略ssl證書驗證
res=urllib.request.urlopen(request,context=context)
print(res.read().decode("utf8"))