Forums

Twilio Not Working

Hi everyone,

I just started using Pythonanywhere and I am trying to send sms using Twilio.

Here's my code:

import os
import requests
from twilio.rest import Client
from twilio.http.http_client import TwilioHttpClient

proxy_client = TwilioHttpClient()
proxy_client.session.proxies = {'https': os.environ['https_proxy']}


account_sid = 'xxxxxxxxxxxxxxxxxxxxx'
auth_token = 'xxxxxxxxxxxxxxxxxxxxx'

client = Client(account_sid, auth_token)

r = requests.get('http://api.open-notify.org/astros.json')
people = r.json()

number_iss = people['number']

Message = 'Hi Fun fact,Number of people in space right now is '+str(number_iss)

#formulate the message that will be sent
message = client.messages.create(
    to="+1xxxxxxxxxx",
    from_="+1xxxxxxxxxx",
    body=Message)

print(message.sid)

I have tried running with python 2.7 as well but that doesn't seem to work either. The error that I get is:

Traceback (most recent call last):
  File "DataMining3_Twil.py", line 26, in <module>
    body=Message)
  File "/usr/local/lib/python2.7/dist-packages/twilio/rest/api/v2010/account/message/__init__.py", line 92, in create
    data=data,
  File "/usr/local/lib/python2.7/dist-packages/twilio/base/version.py", line 205, in create
    allow_redirects=allow_redirects,
  File "/usr/local/lib/python2.7/dist-packages/twilio/base/version.py", line 47, in request
    allow_redirects=allow_redirects
  File "/usr/local/lib/python2.7/dist-packages/twilio/base/domain.py", line 46, in request
    allow_redirects=allow_redirects
  File "/usr/local/lib/python2.7/dist-packages/twilio/rest/__init__.py", line 122, in request
    allow_redirects=allow_redirects
  File "/usr/local/lib/python2.7/dist-packages/twilio/http/http_client.py", line 68, in request
    timeout=timeout,
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 622, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 513, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.twilio.com', port=443): Max retries exceeded with url: /2010-04-01/Acco
unts/AC0069f0582b99394c3e3a87d5b2371995/Messages.json (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at
 0x7f32c2f1b290>: Failed to establish a new connection: [Errno 111] Connection refused',))

Can someone please help me resolve this issue?

[edited by admin: formatting]

You don't have

client = Client(account_sid, auth_token, http_client=proxy_client)

same error using python 3.7 on python anywhere and twilio sms(trial)..please help its urgent

Are you using code that sets up the proxy, like in the example above or on this help page?

I also have similar errors too while deploying on pythonanywhere. And it works pretty well without deploying.

See https://help.pythonanywhere.com/pages/TwilioBehindTheProxy/