Forums

Trying to send SMS with Twilio. ConnectionRefusedError: [Errno 111] Connection refused.

I have been trying to send an SMS with Twilio but its not working. It shows this error.

    Traceback (most recent call last):
  File "/home/dhruvsomani/sms.py", line 11, in <module>
    body="Trial is a Trial.",
  File "/usr/local/lib/python3.4/dist-packages/twilio/rest/resources/messages.py", line 122, in create
    return self.create_instance(kwargs)
  File "/usr/local/lib/python3.4/dist-packages/twilio/rest/resources/base.py", line 341, in create_instance
    data=transform_params(body))
  File "/usr/local/lib/python3.4/dist-packages/twilio/rest/resources/base.py", line 193, in request
    resp = make_twilio_request(method, uri, auth=self.auth, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/twilio/rest/resources/base.py", line 148, in make_twilio_request
    resp = make_request(method, uri, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/twilio/rest/resources/base.py", line 115, in make_request
    resp, content = http.request(url, method, headers=headers, body=data)
  File "/usr/local/lib/python3.4/dist-packages/httplib2/__init__.py", line 1292, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/usr/local/lib/python3.4/dist-packages/httplib2/__init__.py", line 1042, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/usr/local/lib/python3.4/dist-packages/httplib2/__init__.py", line 966, 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

What should I do? Is this due to the security causes of PythonAnywhere? Please tell me how I should proceed. I am using the lates version of Twilio.

If you're using a free PythonAnywhere account, you'll need to tell the Twilio library to use a proxy. I think the right way to do that is to put code like this in your script, before you try to connect to Twilio:

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

host, port = urlparse(os.environ["http_proxy"]).netloc.split(":")
Connection.set_proxy_info(
    host,
    int(port),
    proxy_type=PROXY_TYPE_HTTP,
)

Hello Giles, No, it doesn't work. I pasted the code in my script but it doesn't work. It's still showing the same error.

Traceback (most recent call last):
  File "/home/dhruvsomani/sms.py", line 22, in <module>
    body="Trialing if that's the right word.",
  File "/usr/local/lib/python3.4/dist-packages/twilio/rest/resources/messages.py", line 122, in create
    return self.create_instance(kwargs)
  File "/usr/local/lib/python3.4/dist-packages/twilio/rest/resources/base.py", line 341, in create_instance
    data=transform_params(body))
  File "/usr/local/lib/python3.4/dist-packages/twilio/rest/resources/base.py", line 193, in request
    resp = make_twilio_request(method, uri, auth=self.auth, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/twilio/rest/resources/base.py", line 148, in make_twilio_request
    resp = make_request(method, uri, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/twilio/rest/resources/base.py", line 115, in make_request
    resp, content = http.request(url, method, headers=headers, body=data)
  File "/usr/local/lib/python3.4/dist-packages/httplib2/__init__.py", line 1292, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/usr/local/lib/python3.4/dist-packages/httplib2/__init__.py", line 1042, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/usr/local/lib/python3.4/dist-packages/httplib2/__init__.py", line 966, 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

That's odd. Can I take a look at your code? We can see it from our side, but always ask permission first.

Hello Giles, Be my guest.

I don't see the proxy stuff in there right now -- perhaps you removed it after trying it? It would be good to see it in place, though.

I've added it again.

Could you try moving it above the line where you create the TwilioRestClient object?

I've moved it above the line. But still the error.

That's really odd. What do you get if you put

print(repr(Connection.proxy_info()))

...just before the creation of the TwilioRestClient, after the proxy setup code?

<httplib2.ProxyInfo object at 0x7f0f32432710>

Sorry, I though that the repr for the proxy_info would be a little more helpful. Could you try again, but with this code to print out the fields separately:

print(Connection.proxy_info().proxy_type)
print(Connection.proxy_info().proxy_host)
print(Connection.proxy_info().proxy_port)
print(Connection.proxy_info().proxy_rdns)
print(Connection.proxy_info().proxy_user)
print(Connection.proxy_info().proxy_pass)

3 proxy.server 3128 None None None

Here it is.

Interesting. Could you try upgrading httplib2 for your account? To do that, run this from a Bash console:

pip3.4 install --user --upgrade httplib2

[edit] added Python version to pip command.

Requirement already up-to-date: httplib2 in ./.local/lib/python2.7/site-packages

Did you use pip3.4? (My initial post omitted the version number, I updated it almost immediately but if you were working from the notification email from the forum you wouldn't have seen it.)

I was using pip 8.1.1 from /home/dhruvsomani/.local/lib/python2.7/site-packages (python 2.7)

Sorry, I just upgraded it.

But the error's still there.

This is really odd. It's just ignoring the proxy completely. Perhaps we can try upgrading the Twilio package? Run this from a bash console:

pip3.4 install --user --upgrade twilio

No Giles, that doesn't work.

What output do you get from

pip3.4 show twilio

...?

Here:

Metadata-Version: 2.0
Name: twilio
Version: 5.4.0
Summary: Twilio API client and TwiML generator
Home-page: https://github.com/twilio/twilio-python/
Author: Twilio
Author-email: help@twilio.com
Installer: pip
License: UNKNOWN
Location: /home/dhruvsomani/.local/lib/python3.4/site-packages
Requires: httplib2, six, pytz, pysocks
Classifiers:
  Development Status :: 5 - Production/Stable
  Intended Audience :: Developers
  License :: OSI Approved :: MIT License
  Operating System :: OS Independent
  Programming Language :: Python
  Programming Language :: Python :: 2.5
  Programming Language :: Python :: 2.6
  Programming Language :: Python :: 2.7
  Programming Language :: Python :: 3.2
  Programming Language :: Python :: 3.3
  Programming Language :: Python :: 3.4
  Programming Language :: Python :: 3.5
  Topic :: Software Development :: Libraries :: Python Modules
  Topic :: Communications :: Telephony

Should I try entering a proxy_user and password? http://twilio-python.readthedocs.org/en/latest/usage/basics.html#proxies

I tried it but it doesn't work even after that.

No, that shouldn't be necessary -- our proxy doesn't need either.

What happens if you try doing

curl http://www.google.com/

...from a Bash console?

<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content="Search the world's information, including webpages, images, videos and m ore. Google has many special features to help you find exactly what you're looking for." name="description"><meta content="noodp" name="robots"><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/logos/doodles/2016/doodle-4-google-2016-us-winner-5664555055185920-hp.jpg" itemprop="image"><meta content="http://www.google.com/logos/doodles/2016/doodle-4-google-2016-us-winner-5664555055185920-thp.png" property="og:image">###DELETED### </script></div></body></html>17:59 ~ $

It shows the source code I think. I've deleted some of it.

OK, so the proxy is definitely working for you. But the Twilio library is inexplicably ignoring it and trying to connect directly to their servers.

Hmm.

Is your code compatible with Python 2.7? If so, perhaps you could try running it using that Python version from the bash prompt:

python2.7 sms.py

Giles! It worked.

I received a message on my mobile.

Thanks.

But why did Python 2.7 work?

Hooray!

After much digging around, I finally found a comment on the wiki for httplib2 (bottom of the page) saying that its proxy support doesn't work for Python 3, "until the third-party socks module is ported to" it. The particular proxy we use on PythonAnywhere isn't a socks proxy, it's an HTTP one, but I guess they've just disabled all of the proxy stuff until the socks stuff works.

So, if you're OK using Python 2.7 to do this stuff, you can put a hashbang at the top of your script file to make it always work with that Python version:

#!/usr/bin/env python2.7

I suspect you can also get rid of all of the proxy setup code -- try removing it, and see if the script still works with Python 2.7.

Was facing the same problem. Worked without a problem. Thanks for the solution

Hi Guys i am facing same issue with flask framework, i am able to call by using "TwilioCapability", but "TwilioRestClient" client function failing with : [Errno 111] Connection refused

I tried with python2.7 also but no use.

Did you setup twilio with the correct proxy?

If twiliorestclient isn't working but other twilio functions are working with the proxy, then maybe that particular library/function that you are using isn't obeying the proxy settings.

If it's their code, then you will need to upgrade to use it. (or dig into their code base and submit a pull request to them)

could you please send me your code ? Im trying something similar

My issue was resolved. Please tell what code you want?

how did you resolve this? I have this same problem

I used Python 2.7 to resolve the error.

Twillo perfectly works with Python 2.7, and use the bellow line at the starting.

1
#!/usr/bin/env python2.7

[edit by admin: formatting]

Just for clarity: Twilio should work fine in all Python versions in paid accounts. There is a problem with Python 3 versions of their package that stops it working from free accounts.