Python脚本通过Twilio API返回SSL错误



我正在尝试运行一个基本的SMS脚本(请参阅下文(。

为了绕过下面脚本中的错误,我尝试了:

  1. 在Twilio控制台中禁用SSL进行测试
  2. 从这里安装.pem文件:https://curl.haxx.se/docs/caextract.html到一个文件夹,然后在这里执行以下步骤(除了引用ca-cert.pem文件名(
# Download the helper library from https://www.twilio.com/docs/python/install
from twilio.rest import Client
# Your Account Sid and Auth Token from twilio.com/console
# DANGER! This is insecure. See http://twil.io/secure
account_sid = '###############################'
auth_token = '##############################'
client = Client(account_sid, auth_token)
message = client.messages 
.create(
body="Join Earth's mightiest heroes. Like Kevin Bacon.",
from_='+########',
to='+########'
)
print(message.sid)

我得到以下错误。

C:Users######AppDataLocalContinuumAnaconda3envsuntitled3python.exe "C:/Users/######/PycharmProjects/untitled3/twilio sms alerts.py"
Traceback (most recent call last):
File "C:Users######AppDataLocalContinuumAnaconda3envsuntitled3libsite-packagesurllib3connectionpool.py", line 677, in urlopen
chunked=chunked,
File "C:Users######AppDataLocalContinuumAnaconda3envsuntitled3libsite-packagesurllib3connectionpool.py", line 381, in _make_request
self._validate_conn(conn)
File "C:Users######AppDataLocalContinuumAnaconda3envsuntitled3libsite-packagesurllib3connectionpool.py", line 978, in _validate_conn
conn.connect()
File "C:Users######AppDataLocalContinuumAnaconda3envsuntitled3libsite-packagesurllib3connection.py", line 371, in connect
ssl_context=context,
File "C:Users######AppDataLocalContinuumAnaconda3envsuntitled3libsite-packagesurllib3utilssl_.py", line 384, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "C:Users######AppDataLocalContinuumAnaconda3envsuntitled3libssl.py", line 423, in wrap_socket
session=session
File "C:Users######AppDataLocalContinuumAnaconda3envsuntitled3libssl.py", line 870, in _create
self.do_handshake()
File "C:Users######AppDataLocalContinuumAnaconda3envsuntitled3libssl.py", line 1139, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1091)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:Users######AppDataLocalContinuumAnaconda3envsuntitled3libsite-packagesrequestsadapters.py", line 449, in send
timeout=timeout
File "C:Users######AppDataLocalContinuumAnaconda3envsuntitled3libsite-packagesurllib3connectionpool.py", line 727, in urlopen
method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
File "C:Users######AppDataLocalContinuumAnaconda3envsuntitled3libsite-packagesurllib3utilretry.py", line 439, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.twilio.com', port=443): Max retries exceeded with url: /2010-04-01/Accounts/#############################/Messages.json (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1091)')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/######/PycharmProjects/untitled3/twilio sms alerts.py", line 14, in <module>
to='+16167455489'
File "C:Users######AppDataLocalContinuumAnaconda3envsuntitled3libsite-packagestwiliorestapiv2010accountmessage__init__.py", line 89, in create
payload = self._version.create(method='POST', uri=self._uri, data=data, )
File "C:Users######AppDataLocalContinuumAnaconda3envsuntitled3libsite-packagestwiliobaseversion.py", line 200, in create
allow_redirects=allow_redirects,
File "C:Users######AppDataLocalContinuumAnaconda3envsuntitled3libsite-packagestwiliobaseversion.py", line 47, in request
allow_redirects=allow_redirects
File "C:Users######AppDataLocalContinuumAnaconda3envsuntitled3libsite-packagestwiliobasedomain.py", line 46, in request
allow_redirects=allow_redirects
File "C:Users######AppDataLocalContinuumAnaconda3envsuntitled3libsite-packagestwiliorest__init__.py", line 136, in request
allow_redirects=allow_redirects
File "C:Users######AppDataLocalContinuumAnaconda3envsuntitled3libsite-packagestwiliohttphttp_client.py", line 94, in request
timeout=timeout if timeout is not None else self.timeout,
File "C:Users######AppDataLocalContinuumAnaconda3envsuntitled3libsite-packagesrequestssessions.py", line 643, in send
r = adapter.send(request, **kwargs)
File "C:Users######AppDataLocalContinuumAnaconda3envsuntitled3libsite-packagesrequestsadapters.py", line 514, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='api.twilio.com', port=443): Max retries exceeded with url: /2010-04-01/Accounts/##################################/Messages.json (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1091)')))
Process finished with exit code 1

您可能正在使用虚拟环境。在我的情况下,我使用Anaconda,这是我解决这个问题的方法:

(来源:https://github.com/conda/conda/issues/8273)

我引用:

我的解决方法:我已经复制了以下文件

  • libcrypto-1_1-x64*
  • libssl-1-1-x64*

D:Anaconda3LibrarybinD:Anaconda3DLLs

它是一种魅力!

相关内容

  • 没有找到相关文章

最新更新