PythonAnywhere Forums


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.