Forums

Using Twilio and Flask with python anywhere

Hi,

I'm trying to build a twilio web app api but I am having trouble sending sms by the Twilio REST API. When trying to POST I get error: [Errno 111] Connection refused. I'm on a free account but I believe that twilio should be whitelisted for it? It all works fine locally but fails when I move it to python anywhere. Even this simple program seems to fail .

@app.route('/')
def hello_world():
    from twilio.rest import TwilioRestClient

    # Your Account Sid and Auth Token from twilio.com/user/account
    account_sid = ***** #I've stared this out
    auth_token = ***** 
    client = TwilioRestClient(account_sid, auth_token)

    message = client.messages.create(body="Jenny please?! I love you <3",
        to="+******",
        from_="+*****")
    print message.sid

Any help would be greatly appreciated.

[edit by admin: code formatting]

Are you using the build-in Twilio libaries? Or did you install a different version? There was an old version that had a bug, but the new one should work fine.

I have installed the new version using pip. Here is my traceback as well if that helps.

 2015-03-24 13:35:21,615 :Exception on /webapi/masssms [POST]
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1360, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/group15/.local/lib/python2.7/site-packages/flask_cors/__init__.py", line 279, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1358, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1344, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/group15/.local/lib/python2.7/site-packages/flask_cors/__init__.py", line 168, in wrapped_function
resp = make_response(f(*args, **kwargs))
File "/home/group15/mysite/flask_app.py", line 67, in sendSms
body=request.json.get("message"))
File "/home/group15/.local/lib/python2.7/site-packages/twilio/rest/resources/messages.py", line 122, in create
return self.create_instance(kwargs)
File "/home/group15/.local/lib/python2.7/site-packages/twilio/rest/resources/base.py", line 341, in create_instance
data=transform_params(body))
File "/home/group15/.local/lib/python2.7/site-packages/twilio/rest/resources/base.py", line 193, in request
resp = make_twilio_request(method, uri, auth=self.auth, **kwargs)
File "/home/group15/.local/lib/python2.7/site-packages/twilio/rest/resources/base.py", line 148, in make_twilio_request
resp = make_request(method, uri, **kwargs)
File "/home/group15/.local/lib/python2.7/site-packages/twilio/rest/resources/base.py", line 115, in make_request
resp, content = http.request(url, method, headers=headers, body=data)
File "/home/group15/.local/lib/python2.7/site-packages/httplib2/__init__.py", line 1593, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "/home/group15/.local/lib/python2.7/site-packages/httplib2/__init__.py", line 1335, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
 File "/home/group15/.local/lib/python2.7/site-packages/httplib2/__init__.py", line 1257, in _conn_request
conn.connect()
File "/home/group15/.local/lib/python2.7/site-packages/httplib2/__init__.py", line 1060, in connect
raise socket.error, msg
error: [Errno 111] Connection refused

That's very strange. It looks like it's ignoring the proxy settings we provide to let free users use sites on the whitelist. Let me take a closer look at the Twilio code and get back to you.

Try putting this code at the top of your module (outside any views). It should force it to use the correct proxy.

import os
from urlparse 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)

Let us know if that helps.

I should say -- you'll need to reload your web app (from the "Web" tab) after adding that code.

That's fixed it! Thank you very much for your help.

Excellent, thanks for confirming! I wonder when that changed, you never used to need to configure the proxy manually.

Hello,

I get this error

ImportError: No module named 'urlparse'

pip3 install --user urlparse gives error

No matching distribution found for urlparse

HELP

Sorry, Giles's example was for Python 2. In Python 3, urlparse is renamed to urllib.parse. So change that line to:

from urllib.parse import urlparse

changed the import line to

from urllib.parse import urlparse

but i still get the same error

Can you confirm what the error is that you're getting now?

One thing it occurs to me that you should check -- did you pip install twilio for python 3 or for python 2? In your post above, it looks like python 2. Run a new install just to make sure?

pip3.4 install --user --upgrade twilio

And then make sure that your test code and your web app are all running with Python 3.4?

i did the same it works for a day .............. but now when i doing this my sms get sent but in respose i am getting 500 error

Look in your error log to determine what is causing the error.