我正在制作一個基于 python 的 Mac 應用程式,用于discord.py處理不和諧的事情。正如我從之前制作不和諧機器人的經驗中知道的那樣,運行不和諧機器人需要您Install Certificates.command在您的 python 版本中運行。但是,如果其他用戶使用此應用程式,我不想要求他們安裝 python。我從 中獲取了一段代碼Install Certificates.command,認為它會將證書放在用戶計算機上的正確位置。但是,測驗人員在他們的計算機上運行應用程式時遇到此錯誤:
Traceback (most recent call last):
File "Interface.py", line 136, in <module>
File "installCerts.py", line 25, in installCerts
FileNotFoundError: [Errno 2] No such file or directory: '/Library/Frameworks/Python.framework/Versions/3.8/etc/openssl'
[2514] Failed to execute script 'Interface' due to unhandled exception: [Errno 2] No such file or directory: '/Library/Frameworks/Python.framework/Versions/3.8/etc/openssl'
[2514] Traceback:
Traceback (most recent call last):
File "Interface.py", line 136, in <module>
File "installCerts.py", line 25, in installCerts
FileNotFoundError: [Errno 2] No such file or directory: '/Library/Frameworks/Python.framework/Versions/3.8/etc/openssl'
很清楚這個錯誤在說什么:他們沒有安裝 python (3.8),所以它不能把 ssl 證書放在任何地方(這是因為應用程式在 python 3.8 環境中運行)。
順便說一句,錯誤中提到的路徑是 . 給出的路徑的目錄名稱ssl.get_default_verify_paths().openssl_cafile。
我不是非常精通網路連接的細節和類似的東西,所以我不知道這些證書的確切作用。這是我的問題:
如果用戶在他們的計算機上安裝 python,是否可以讓它作業?
即我可以將 ssl 證書添加到應用程式的本地 python 版本(據我所知,在我的應用程式中,python 只是一個捆綁的大型 exec 檔案)?檔案系統深處是否有我可以放置證書以讓不和諧連接發生的地方?. 幾乎任何解決方案都會受到贊賞。
附加資訊:
我安裝證書的代碼:
STAT_0o775 = (stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
| stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP
| stat.S_IROTH | stat.S_IXOTH)
openssl_dir, openssl_cafile = os.path.split(
ssl.get_default_verify_paths().openssl_cafile)
os.chdir(openssl_dir) #Error happens here
relpath_to_certifi_cafile = os.path.relpath(certifi.where())
print(" -- removing any existing file or link")
try:
os.remove(openssl_cafile)
except FileNotFoundError:
pass
print(" -- creating symlink to certifi certificate bundle")
os.symlink(relpath_to_certifi_cafile, openssl_cafile)
print(" -- setting permissions")
os.chmod(openssl_cafile, STAT_0o775)
print(" -- update complete")
當用戶沒有安裝正確的證書時 discord.py 拋出的錯誤:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/aiohttp/connector.py", line 969, in _wrap_create_connection
return await self._loop.create_connection(*args, **kwargs) # type: ignore # noqa
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/base_events.py", line 1050, in create_connection
transport, protocol = await self._create_connection_transport(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/base_events.py", line 1080, in _create_connection_transport
await waiter
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/sslproto.py", line 529, in data_received
ssldata, appdata = self._sslpipe.feed_ssldata(data)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/sslproto.py", line 189, in feed_ssldata
self._sslobj.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 944, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1125)
如果您需要更多資訊,請告訴我。
uj5u.com熱心網友回復:
好的。這非常困難,但經過大量研究后我得到了答案。ssl在 python 中基本上只是一組用于openSSL. 當你這樣做時import ssl,它會構建一個openSSL環境(我不認為我在這里使用了完全正確的詞)。如您所見,它默認為openSSLin 檔案夾,Python因為從 python 的角度來看,這是openSSL保存其證書的地方。事實證明,ssl.DefaultVerifyPaths物件還有其他屬性,即cafile. 這就是我如何根據需要制作通往證書的路徑。您會看到,在openSSL構建時,它會查找環境變數SSL_CERT_FILE。只要我在 importos.environ之前設定該變數ssl,它就可以作業,因為ssl會找到證書。我簡化了installCerts歸結為以下幾點:
import os
import stat
import certifi
def installCerts():
os.environ['SSL_CERT_FILE'] = certifi.where()
import ssl
# ssl build needs to happen after enviro var is set
STAT_0o775 = (stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
| stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP
| stat.S_IROTH | stat.S_IXOTH)
cafile = ssl.get_default_verify_paths().cafile
os.chmod(cafile, STAT_0o775)
它現在似乎可以在其他人的計算機上正常作業,而無需他們安裝 python。
這個問題幫助了我:
如何更改 Python3 中 ssl 模塊中的“cafile”引數?
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/445819.html
標籤:python-3.x 苹果系统 ssl 不和谐.py
下一篇:linux掛載檔案系統
