Forums

Twilio failing with ConnectionRefusedError: [Errno 111] Connection refused

I'm having trouble sending sms via Twilio, and I'm using the free account. I need help to resolve it. I read previous forum discussions, and I tried to incorporate the suggestion made to add proxy. But I'm not getting anywhere.

I've run my code using pdb in virualenv using the command below

  • python -m pdb twilio_sms.py

But i keep getting this error: ConnectionRefusedError: [Errno 111] Connection refused.

Code is:


from twilio.rest import TwilioRestClient
from twilio import TwilioRestException
import os
from urllib.parse import urlparse

from twilio.rest.resources import Connection
from twilio.rest.resources.connection import PROXY_TYPE_HTTP

proxy_url = os.environ.get("http_proxy")
host, port = urlparse(proxy_url).netloc.split(":")
Connection.set_proxy_info(host, int(port), proxy_type=PROXY_TYPE_HTTP)
# put your own credentials here
ACCOUNT_SID = "XXXX"
AUTH_TOKEN = "YYYY"

client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)

twilio_message = client.messages.create(
    to="+1##########",
    from_="+1##########",
    body="This is a test 201501070916P",
    )

Error log is listed below


(venv)21:07 ~/Mohamed_Python $ python -m pdb twilio_sms.py                                                                                          
> /home/mohamedab/Mohamed_Python/twilio_sms.py(1)<module>()                                                                                         
-> from twilio.rest import TwilioRestClient                                                                                                         
(Pdb) c                                                                                                                                             
Traceback (most recent call last):                                                                                                                  
  File "/usr/lib/python3.4/pdb.py", line 1661, in main                                                                                              
    pdb._runscript(mainpyfile)                                                                                                                      
  File "/usr/lib/python3.4/pdb.py", line 1542, in _runscript                                                                                        
    self.run(statement)                                                                                                                             
  File "/usr/lib/python3.4/bdb.py", line 431, in run                                                                                                
    exec(cmd, globals, locals)                                                                                                                      
  File "<string>", line 1, in <module>                                                                                                              
  File "/home/mohamedab/Mohamed_Python/twilio_sms.py", line 1, in <module>                                                                          
    from twilio.rest import TwilioRestClient                                                                                                        
  File "/home/mohamedab/prvproj/venv/lib/python3.4/site-packages/twilio/rest/resources/messages.py", line 122, in create                            
    return self.create_instance(kwargs)                                                                                                             
  File "/home/mohamedab/prvproj/venv/lib/python3.4/site-packages/twilio/rest/resources/base.py", line 365, in create_instance                       
    data=transform_params(body))
  File "/home/mohamedab/prvproj/venv/lib/python3.4/site-packages/twilio/rest/resources/base.py", line 200, in request
    resp = make_twilio_request(method, uri, auth=self.auth, **kwargs)
  File "/home/mohamedab/prvproj/venv/lib/python3.4/site-packages/twilio/rest/resources/base.py", line 152, in make_twilio_request
    resp = make_request(method, uri, **kwargs)
  File "/home/mohamedab/prvproj/venv/lib/python3.4/site-packages/twilio/rest/resources/base.py", line 117, in make_request
    resp, content = http.request(url, method, headers=headers, body=data)
  File "/home/mohamedab/prvproj/venv/lib/python3.4/site-packages/httplib2/__init__.py", line 1314, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/home/mohamedab/prvproj/venv/lib/python3.4/site-packages/httplib2/__init__.py", line 1064, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/home/mohamedab/prvproj/venv/lib/python3.4/site-packages/httplib2/__init__.py", line 987, in _conn_request
    conn.connect()
  File "/usr/lib/python3.4/http/client.py", line 1223, in connect
    super().connect()
  File "/usr/lib/python3.4/http/client.py", line 834, in connect
    self.timeout, self.source_address)
  File "/usr/lib/python3.4/socket.py", line 512, in create_connection
    raise err
  File "/usr/lib/python3.4/socket.py", line 503, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /usr/lib/python3.4/socket.py(503)create_connection()
-> sock.connect(sa)
(Pdb)

[edited by admin: formatting]

One thing to add; echo $PATH shows: /home/mohamedab/prvproj/venv/bin:/home/mohamedab/.local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

I think twilio has had some issues in the past with respect to honouring the proxy settings/variables. This definitely sounds like twilio is trying to connect directly instead of through the proxy we have setup for free users.

Two things I can think of to debug right now:

  1. when you are stepping through it in pdb, dig into the socket connection to see if they are using the correct proxy
  2. upgrade to a paying account to see if the error goes away