❶ 怎么用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"))