PythonAnywhere Forums

Twilio

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

from twilio.rest 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 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,
)

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. :)

try:
  proxy = os.environ['http_proxy']
  if proxy:
    host, port = urlparse(os.environ["http_proxy"]).netloc.split(":")
    Connection.set_proxy_info(
    host,
    int(port),
    proxy_type=PROXY_TYPE_HTTP,
    )
except:
  pass