導航:首頁 > 編程語言 > pythonssl庫

pythonssl庫

發布時間:2022-12-18 20:26:49

1. 如何用 python 解析 ssl 證書文件

PyOpenSSL

OpenSSL.SSL.Connection()
get_peer_certificate()
get_peer_cert_chain()

2. python報錯urllib.error.URLError: <urlopen error unknown url type: src="https>,Windows系統怎麼解決

jango站點使用django_cas接入SSO(單點登錄系統),配置完成後登錄,拋出「urlopen error unknown url type: https」異常。尋根朔源發現是python內置的urllib模塊不支持https協議。

>>> import urllib
>>> urllib.urlopen('htom')
<addinfourl at 269231456 whose fp = <socket._fileobject object at 0xff98250>>
>>> urllib.urlopen('hm')
Traceback (most recent call last):
File "<stdin>", line 1, in <mole>
File "/usr/local/python27/lib/python2.7/urllib.py", line 86, in urlopen
return opener.open(url)
File "/usr/local/python27/lib/python2.7/urllib.py", line 204, in open
return self.open_unknown(fullurl, data)
File "/usr/local/python27/lib/python2.7/urllib.py", line 216, in open_unknown
raise IOError, ('url error', 'unknown url type', type)
IOError: [Errno url error] unknown url type: 'https'

之所以python內置的urllib模塊不支持https協議是因為編譯安裝python之前沒有編譯安裝類似於openssl這樣的SSL庫,以至於python不支持SSL

因為我用的是Centos系統所以安裝openssl-devel
sudo yum install openssl-devel

之後重新編譯Python
./configure(可選,因為之前已經配置過,按之前的配置來就行了,而且最好按之前的配置配編譯安裝以免依賴的庫需要重新編譯安裝。)
make
make install

>>> import urllib
>>> urllib.urlopen('htt.com')
沒有再報同樣的錯誤。

在安裝完openssl-devel後重新編譯python前也有說需要編輯Moles文件夾內Setup.dist文件的
修改
# Socket mole helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
#SSL=/usr/local/ssl
#_ssl _ssl.c \
# -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
# -L$(SSL)/lib -lssl -lcrypto

# Socket mole helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto

但實際測試下來好像並不需要修改這個文件,編譯的時候能自動將SSL庫編譯進python中。

另外需要特別注意的是,重新編譯安裝python後,通過可執行文件名(可能是個連接文件)運行python可能運行的還是老的python,這是因為可執行文件名沒有連接到新的python可執行程序。因此要用最新的python可執行文件名或指向該名字的連接來運行python。

重新編譯安裝python後有可能導致需要重新編譯django,MySQLdb,pycrypto,python-ldap,django-auth-ldap,django_cas,django_cas,pymongo等一些列依賴python的模塊。這里要特別注意

3. python環境設置ssl

爬取數據需要用python請求網路數據, https的加密實現依賴openssl

利用源碼直接安裝

安裝zlib庫

安裝openssl到 /usr/local/openssl 目錄,安裝之後,編譯

安裝結束後執行以下命令

進入/usr/local目錄下,執行以下命令

在/etc/ld.so.conf文件的最後面,添加如下內容:/usr/local/openssl/lib
然後執行以下命令

添加OPESSL的環境變數,在etc/的profile的最後一行,添加:

查看openssl版本

使用源碼按過於繁瑣,如果對軟體版本沒有特殊要求的話可以使用yum命令安裝和更新,既方便又快捷

先看下ssl有沒有被引入

報錯是因為python沒有帶--with-ssl 選項編譯
修改Setup.dist文件 (把Setup這個不帶後綴的也一起改了)

修改結果如下:(去掉最後4行的注釋)

切到python文件夾下

完成之後

沒有報錯, 大功告成

參考鏈接
ImportError: No mole named _ssl
linux上安裝Openssl步驟詳解

4. Anaconda python 中 SSL模塊無法正常使用

2.conda創建的環境中因為pip缺少ssl無法安裝包,可將

從 D:\miniconda\envs\py37\Library\bin 粘貼至 D:\miniconda\envs\py37\DLLs

5. 怎麼樣在windows上安裝Python的SSL mole

1、說明:win7安裝python非常簡單,只要到官方網站下載然後直接安裝即可。2、步驟:1)打開python官方網站windows版下載地址https://www.python.org/downloads/windows/

以python2.7為例,選擇python2.7.11,打開下載頁面,按操作系統位(32或者64)選擇相應版本下載,等待下載完成。

2)下載完成後,雙擊下載的安裝文件。

3)選擇Install for all users即可,點Next

4)選擇安裝的路徑,默認即可。

5)選擇安裝的組件,默認即可,也可以把選上Add Python.exe to Path,就是添加到環境變數,可以使用戶直接在cmd下執行python。選好後點下一步。

6)等待安裝完成後,點Finish即可。

3、驗證安裝安裝完成後開始菜單會出現如下菜單

點擊菜單IDLE (Python GUI),出現IDLE窗口,輸入一個簡單的程序,回車運行如下,就說明安裝成功

6. 321.【python】_ssl 模塊支持

記錄一下搭建環境是遇到的 python 里出現的 缺少 _ssl 模塊的問題。

當我們直接配置的時候,會缺少 _ssl ,所以我們在使用 configure 配置前,先編輯 ${python_install_dirctory}/Mole/Setup 文件,如下所示:

將第210、215、216、217 行的注釋去了,去了之後,再執行配置安裝即可。

Linux 的學習是使用基本上都得面臨枯燥的黑窗口,慢慢來吧。

7. 解決Python找不到ssl模塊問題 No mole named _ssl

python安裝完畢後,提示找不到ssl模塊:

解決方法:

查看安裝結果
[ [email protected] ~]$ rpm -aq|grep openssl
openssl-devel-1.0.1e-57.el6.x86_64
openssl-1.0.1e-57.el6.x86_64

8. 如何驗證在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)

9. 如何使用證書存儲在python中的字元串變數打開ssl套接字

查看源代碼,ssl.wrap_socket直接調用本地代碼(openssl)函數SSL_CTX_use_cert_chain_file,它需要一個文件的路徑,因此您無法做的是嘗試做的。

參考:

在ssl / init .py中,我們看到:

def wrap_socket(sock,keyfile =無,certfile =無,
server_side = False,cert_reqs = CERT_NONE,
ssl_version = PROTOCOL_SSLv23,ca_certs = None,
do_handshake_on_connect = True):

return SSLSocket sock,keyfile = keyfile,certfile = certfile,
server_side = server_side,cert_reqs = cert_reqs,
ssl_version = ssl_version,ca_certs = ca_certs,
do_handshake_on_connect = do_handshake_on_connect)

將我們指向SSLSocket構造函數(它在同一個文件中),我們看到以下情況:

self._sslobj = _ssl2.sslwrap(self._sock,server_side,
keyfile,certfile,
cert_reqs,ssl_version,ca_certs)

_ssl2在C(_ssl2.c)中實現

查看sslwrap函數,我們看到它正在創建一個新對象:

return(PyObject *)newPySSLObject(Sock,key_file ,cert_file,
server_side,verification_mode,
protocol,cacerts_file);

查看該對象的構造函數,我們最終會看到:

ret = SSL_CTX_use_certificate_chain_file(self-> ctx,
cert_file);

這個函數在openssl中定義,所以現在我們需要切換到那個代碼庫。

在ssl / ssl_rsa.c中,我們最終在函數中找到:

BIO_read_filename in,file)

如果你深入到BIO代碼(openssl的一部分)最終成為正常的fopen():

fp = fopen(ptr,p)

所以它看起來像它當前寫的。它必須在C的fopen()可打開的文件中。

此外,由於python的ssl庫很快跳轉到C,我在一個解決方法中看不到一個明顯的monkeypatch的地方。

閱讀全文

與pythonssl庫相關的資料

熱點內容
dvd光碟存儲漢子演算法 瀏覽:757
蘋果郵件無法連接伺服器地址 瀏覽:963
phpffmpeg轉碼 瀏覽:672
長沙好玩的解壓項目 瀏覽:145
專屬學情分析報告是什麼app 瀏覽:564
php工程部署 瀏覽:833
android全屏透明 瀏覽:737
阿里雲伺服器已開通怎麼辦 瀏覽:803
光遇為什麼登錄時伺服器已滿 瀏覽:302
PDF分析 瀏覽:486
h3c光纖全工半全工設置命令 瀏覽:143
公司法pdf下載 瀏覽:382
linuxmarkdown 瀏覽:350
華為手機怎麼多選文件夾 瀏覽:683
如何取消命令方塊指令 瀏覽:350
風翼app為什麼進不去了 瀏覽:779
im4java壓縮圖片 瀏覽:362
數據查詢網站源碼 瀏覽:151
伊克塞爾文檔怎麼進行加密 瀏覽:892
app轉賬是什麼 瀏覽:163