我正在使用 python 编写一个简单的服务,该服务使用 MIME 发送电子邮件。 它运行代码并给我这个错误:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 360, in send
self.sock.sendall(s)
OSError: [Errno 9] Bad file descriptor
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/kiansahafi/PycharmProjects/test/Email/main_email.py", line 10, in <module>
DatabaseRepositoryInstance.update_database_task()
File "/Users/kiansahafi/PycharmProjects/test/Email/DatabaseRepository.py", line 22, in update_database_task
sendEmailInstance.send_email(subject, body, to_email)
File "/Users/kiansahafi/PycharmProjects/test/Email/send_email.py", line 63, in send_email
server.quit()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 999, in quit
res = self.docmd("quit")
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 426, in docmd
self.putcmd(cmd, args)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 373, in putcmd
self.send(str)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 363, in send
raise SMTPServerDisconnected('Server not connected')
smtplib.SMTPServerDisconnected: Server not connected
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)
搜索后我发现这篇文章说我必须从 /Applications/Python 3.11/Install Certificates.command 在 python 中安装证书,当我尝试打开它时(即使使用 sudo 命令!)它给了我这个错误:
❯ /Applications/Python\ 3.11/Install\ Certificates.command ; exit;
-- pip install --upgrade certifi
Requirement already satisfied: certifi in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (2023.11.17)
-- removing any existing file or link
-- creating symlink to certifi certificate bundle
-- setting permissions
Traceback (most recent call last):
File "<stdin>", line 44, in <module>
File "<stdin>", line 40, in main
PermissionError: [Errno 1] Operation not permitted: 'cert.pem'
Saving session...completed.
[Process completed]
我也搜索了这个问题,发现这篇文章说我应该给终端(和Finder!)完全磁盘访问权限,我这样做了,但问题没有解决。
提前感谢您提供任何解决方案。
我的 macOS 版本是 14.0,我的机器是 m1 macbook pro,我的代码编辑器是 pycharm。
我尝试过升级 python 和 pip,但不确定这是否会起到任何作用...
问题出在解释器上,原来是3.9,改成3.11后问题解决了!