I am new to PythonAnywhere and Twilio. I am trying to run the following in a Python 2.7 console in PythonAnywhere:

from import TwilioRestClient

account_sid = "A*****************************5"
auth_token = "6**********************************8"
client = TwilioRestClient(account_sid, auth_token)

message = client.messages.create(to="+6*************1", from_="+6***********5",
                                 body="Hello there again!")

This works when I run it locally, but not on PythonAnywhere. I get the following error message: socket.error: [Errno 111] Connection refused

I have tried changing "client.messages.create" to "client.sms.messages.create" But this has no effect (I also saw in previous posts that using Python 2.7 is good, and that I could update PythonAnywhere's version of Twilio - done that too.). The virtualenv in running in PythonAnywhere from the same folder as this code.

Please can someone tell me what I am doing wrong?

I fixed my own problem: this worked:

import os
from urlparse import urlparse
from import Connection
from import PROXY_TYPE_HTTP

host, port = urlparse(os.environ["http_proxy"]).netloc.split(":")

before the rest of the code.

Glad you got it working, and thanks for posting the code!

For anyone else who comes across this issue, here's an explanation of why it's necessary: free accounts on PythonAnywhere can only connect to external Internet sites like Twilio using a proxy. Paid accounts have direct Internet access. Normally this is all transparent to you as a developer; the libraries you use to connect to external sites look in the operating system's environment, notice that there's an http_proxy variable set there, and use it. So everything just works.

But some libraries aren't that smart, and need to be told about the proxy explicitly; the Twilio library is one of those libraries. So the code that @vikibrookes posted looks up the http_proxy variable in the OS environment, splits it into the two important parts -- the hostname for the proxy server, and the port the server is listening on -- and then tells the Twilio library to use it.

Again, this is all unnecessary if you're using a paid account, because for that you don't need to use the proxy.

Thanks for the awesome start. I actually started on a free account and upgraded because I love the service, but I ran into a "service interruption" because my code was taking advantage of the proxy being present, but not handling when it wasn't. I updated the snippet to handle both cases which now means sharing code with "freeloaders" doesn't mean breaking them, and if I can convince them to upgrade they won't have to rewrite anything. :)

  proxy = os.environ['http_proxy']
  if proxy:
    host, port = urlparse(os.environ["http_proxy"]).netloc.split(":")

Hi, Back to the original topic, just wanted to check if you know if anything changed in approach to Twilio? As I checked now its API works on requests, so judging from previous posts this should help in proper running on Python 3.

They also made some changes to package, so I couldn't find Connection class with set proxy method anywhere. I tried to play around with TwilioHttpConnection but with no luck. I might be wrong but now it may be connected with allow_redirect param going into request attributes

I'm not sure, but I think I heard that the latest twilio may work better? Try installing it into a virtualenv, and let us know if it does work?

Yeah, that's the way I did it, I've setup dedicated py3.5 venv and pip installed Twilio, then started to play around - but didn't work :/ I guess if new Twilio uses requests rather that httplib2 now it should work with python 3 as well?

Anything using requests should work, yes. What errors did you get when you tried?

I'm getting timeout:

requests.exceptions.ConnectionError: HTTPSConnectionPool(host='', port=443): Max retries exceeded with url: /2010-04-01/Accounts/AC83d6a9d514502cbbac6acb5a61317a37/Messages.json (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f993042eac8>: Failed to establish a new connection: [Errno 111] Connection refused',)) 2017-04-24 23:14:44,009 : File "/home/username/pyvenvs/lib/python3.5/site-packages/hug/", line 406, in api_auto_instantiate 2017-04-24 23:14:44,009 : return module.hug_wsgi(args, *kwargs)

Interesting. The important part of that is the Failed to establish a new connection: [Errno 111] Connection refused, which normally means that the library you're using is ignoring the proxy settings. What's the hug module? It doesn't look like part of the Twilio API, at least as I remember it...?

Hug is a rest framework (see I use to handle the http routing for whole app. You think it goes that deep?

As an exercise I tried copying Twilio package into my app directory and manually overwritten all methods in it to have their default attribute allow_redirect to True (it's False in original package) - no changes, still error 111

I tried:


And print actually shows proxy:port

I think the allow_redirect might be a red herring -- it's probably not related to the proxy.

Do you have some sample code that I could run that triggers the problem? I wouldn't need your Twilio credentials (because the problem is happening before it's connected to their servers), but some code to play with and the output of pip show twilio would be useful.

Hi there, we now have a specific help page on setting up twilio to use the pythonanywhere proxy (for free accounts)

I got exactly this same problem, after several hours of trying lot of solutions that didn't work. I just uninstalled the twilio 6.12.1 and installed 5.7.12 and all worked, don't forget to change from import Client to from import TwilioRestClient and change from client = Client(account_sid, auth_token) to client = TwilioRestClient(account_sid, auth_token)

great! thanks for reporting back!