Forums

Receiving an error when I try to use the Twilio Module

My code is as follows

from twilio.rest import TwilioRestClient #programmable text service module

client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)

client.messages.create(
        to="*****",
        from_="+*****",
        body="text",
        )  #sending actual message

For obvious reasons I have removed the phone numbers as well as the Account_SID and AUTH_TOKEN variables, but rest assured all of that is in my code. The error I recieve is ConnectionRefusedError: [Errno 111] Connection refused. And my entire traceback is shown below. I can't seem to find a solution; the code runs fine locally, but throws this error on PA. I'm also very new to this site so I apologize if I'm missing something.

The traceback

Traceback (most recent call last):
  File "/home/ectobiologist7/day_counter_summer.py", line 44, in <module>
    carp()
  File "/home/ectobiologist7/day_counter_summer.py", line 25, in carp
    megusta()
  File "/home/ectobiologist7/day_counter_summer.py", line 40, in megusta
    body="text",
  File "/usr/local/lib/python3.3/dist-packages/twilio/rest/resources/messages.py", line 122, in create
    return self.create_instance(kwargs)
  File "/usr/local/lib/python3.3/dist-packages/twilio/rest/resources/base.py", line 341, in create_instance
    data=transform_params(body))
  File "/usr/local/lib/python3.3/dist-packages/twilio/rest/resources/base.py", line 193, in request
    resp = make_twilio_request(method, uri, auth=self.auth, **kwargs)
  File "/usr/local/lib/python3.3/dist-packages/twilio/rest/resources/base.py", line 148, in make_twilio_request
    resp = make_request(method, uri, **kwargs)
  File "/usr/local/lib/python3.3/dist-packages/twilio/rest/resources/base.py", line 115, in make_request
    resp, content = http.request(url, method, headers=headers, body=data)
  File "/usr/local/lib/python3.3/dist-packages/httplib2/__init__.py", line 1272, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/usr/local/lib/python3.3/dist-packages/httplib2/__init__.py", line 1026, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/usr/local/lib/python3.3/dist-packages/httplib2/__init__.py", line 963, in _conn_request
    conn.connect()
  File "/usr/lib/python3.3/http/client.py", line 1198, in connect
    self.timeout, self.source_address)
 File "/usr/lib/python3.3/socket.py", line 435, in create_connection
    raise err
  File "/usr/lib/python3.3/socket.py", line 426, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

Thanks in advance for helping!

It could be that that version of the twilio package doesn't work with our proxy. Try creating a virtualenv and installing twilio into that, so you get the latest version?

mkvirtualenv --python=/usr/bin/python3.4 newtwilio
pip install twilio

More info here: https://www.pythonanywhere.com/wiki/Virtualenvs

I apologize, but I'm not exactly sure where I should put those commands to establish the virtualenv, could you please explain if you want? I see the commands for the virtualenv's setup on the info page, but not where to put them.

Ah, sorry, you would run those from a Bash console, and then use that same console to run your scripts...

Thank you, it seems to be working so far! One last question, so to run the script itself, do I just type in the file name? Like for example, "file.py"? Thank you, sorry for my inexperience.

Never mind, I tried that and it did not work, can you tell me how one would do it? I'm sorry.

Never mind once again, I figured out how to do it, but the same error is thrown when I run it in the bash console, after getting the newest version of Twilio.

Hi there -- sorry for the slow reply on this issue!

Try the solution we suggested in this forum post -- that should fix it.

Hi Giles! I'm glad you replied at all, honestly. I actually have seen that post before, but I'm not sure what it means by put that at the top of the module. Do I place it in my existing code that I have on the site (and if so, do I just place it in the part I listed above?), or do I actually place it in the files of the Twilio module itself? I don't think this would work because my local files have no effect on the site's version, and so I think you might mean the copy of the module stored on the site, which I'm not sure how to access. I apologize if I'm missing something obvious, I'm really new to this site. Thanks!

Ok, so I just tossed it into the code above and it threw an error that urlparse does not exist. And I wondered for a second but I realize that I'm using Python 3.3 and I can't believe I didn't realize that at first! In Python 3.3, urlparse became urllib.parse! I'm not sure how the code in the other thread would be written in Python 3, though. Would you know by chance?

Alright, so I fixed that on my own: the only change needed is "from urlparse import urlparse" changed to "from urllib.parse import urlparse" but the thing is that the same error is thrown.

My only guess is that the code needs to go somewhere that isn't within the code I have displayed above. I can move it somewhere else if needed, but I have no idea where.

Alright I've managed to find an alternative to Twilio called Plivo, and when I use that module it works 100%, so I will just use that module instead and the issue is not an issue anymore. Thanks anyway for all the help.

well, glad it now works, one way or another!