使用Twilio快速连续发送多个短信

时间:2018-02-14 17:27:55

标签: python django ssl-certificate twilio sslhandshakeexception

我正在尝试使用twilio和django 1.8.2发送短信。当有多个调用在同一个函数中发送短信时,我遇到了问题。

这是将短信发送到任何特定电话号码的代码段:

 private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (listView1.SelectedItems != null)
        {
            bntDeleteFile.Enabled = true;
        }
        else
        {
            bntDeleteFile.Enabled = false;
        }
     }

当我向这个函数发出两个请求时:

def send_twilio_message(to_number, body):
    client = twilio.rest.TwilioRestClient(
        settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
    return client.messages.create(
        body=body,
        to=to_number,
        from_=settings.TWILIO_PHONE_NUMBER
    )

我收到此错误:

def some_function():
    send_twilio_message(number1, "text 1")
    send_twilio_message(number2, "text 2")

但是,当我这样做时:

Traceback:
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, 
**callback_kwargs)
File "/home/arka/Project/TreadWill/dreamhost/CCBT/trial_IITK/views.py" 
in waitlist_phq
  782.             exclusion_code = waitlist_alert(wl.participant, 
phq_score)
File "/home/arka/Project/TreadWill/dreamhost/CCBT/trial_IITK/views.py" 
in waitlist_alert
  857.             send_twilio_message(phonenumber, 'Looks like your 
child maybe having suicidal thoughts. Get in touch with your child 
ASAP. - TreadWill.')
File "/home/arka/Project/TreadWill/dreamhost/CCBT/sendsms/utils.py" in 
send_twilio_message
  13.         from_=settings.TWILIO_PHONE_NUMBER
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/twilio/rest/resources/messages.py" in create
  122. return self.create_instance(kwargs)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/twilio/rest/resources/base.py" in create_instance
  365. data=transform_params(body))
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/twilio/rest/resources/base.py" in request
  200. resp = make_twilio_request(method, uri, auth=self.auth, 
**kwargs)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/twilio/rest/resources/base.py" in make_twilio_request
  152. resp = make_request(method, uri, **kwargs)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/twilio/rest/resources/base.py" in make_request
  117. resp, content = http.request(url, method, headers=headers, 
body=data)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/httplib2/__init__.py" in request
  1314. (response, content) = self._request(conn, authority, uri, 
request_uri, method, body, headers, redirections, cachekey)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/httplib2/__init__.py" in _request
  1064. (response, content) = self._conn_request(conn, request_uri,         
method, body, headers)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-    
packages/httplib2/__init__.py" in _conn_request
  987. conn.connect()
File "/usr/lib/python3.5/http/client.py" in connect
  1260. server_hostname=server_hostname)
File "/usr/lib/python3.5/ssl.py" in wrap_socket
  377. _context=self)
File "/usr/lib/python3.5/ssl.py" in __init__
  752. self.do_handshake()
File "/usr/lib/python3.5/ssl.py" in do_handshake
  988. self._sslobj.do_handshake()
File "/usr/lib/python3.5/ssl.py" in do_handshake
  633. self._sslobj.do_handshake()

Exception Type: SSLError at     
/iitk/phq/NnBLRUEsbm5jbERmfGZWNkt6RVNPcEFBNnplNkA4RVFOTkN4TnI=/
Exception Value: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify 
failed (_ssl.c:645)

工作正常。这种方法的问题是,如果页面持续加载60秒,用户可能会关闭浏览器(我尝试了不到60秒,我得到了同样的错误)。

我尝试在我的Twilio帐户中禁用SSL验证,但这不起作用。

我已经查看了其他答案,并且我已经阅读了Twilio的文档。我一直无法找到一个好的解决方案。任何帮助将不胜感激。

编辑: 我在本地服务器上尝试了所有这些。当我尝试代码时:

import time
def some_function():
    send_twilio_message(number1, "text 1")
    time.sleep(60)
    send_twilio_message(number2, "text 2")

在托管服务器上,它工作正常。所以,我认为问题可能是由于我所在学院的网络。问题已经解决了,但我仍然无法弄清楚为什么我会面临这个问题。如果有人知道为什么会这样,请发表你的答案。

0 个答案:

没有答案