My script executes perfectly when I run it locally, but when I execute it on Python Anywhere, it throws the following error:
Traceback (most recent call last):
File "/home/ectobiologist7/desiderius.py", line 68, in <module>
asendoff()
File "/home/ectobiologist7/desiderius.py", line 64, in asendoff
body=billiam,
File "/usr/local/lib/python3.4/dist-packages/twilio/rest/resources/messages.py", line 122, in create
return self.create_instance(kwargs)
File "/usr/local/lib/python3.4/dist-packages/twilio/rest/resources/base.py", line 365, in create_instance
data=transform_params(body))
File "/usr/local/lib/python3.4/dist-packages/twilio/rest/resources/base.py", line 200, in request
resp = make_twilio_request(method, uri, auth=self.auth, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/twilio/rest/resources/base.py", line 152, in make_twilio_request
resp = make_request(method, uri, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/twilio/rest/resources/base.py", line 117, in make_request
resp, content = http.request(url, method, headers=headers, body=data)
File "/usr/local/lib/python3.4/dist-packages/httplib2/__init__.py", line 1314, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "/usr/local/lib/python3.4/dist-packages/httplib2/__init__.py", line 1064, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/usr/local/lib/python3.4/dist-packages/httplib2/__init__.py", line 987, in _conn_request
conn.connect()
File "/usr/lib/python3.4/http/client.py", line 1223, in connect
super().connect()
File "/usr/lib/python3.4/http/client.py", line 834, in connect
self.timeout, self.source_address)
File "/usr/lib/python3.4/socket.py", line 512, in create_connection
raise err
File "/usr/lib/python3.4/socket.py", line 503, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
Now, I've combed through forums both on Stack Overflow and on Python anywhere itself, and none of the solutions I've found have succeeded. One suggestion was that the script wasn't working because a website I was trying to access wasn't on the whitelist, and this is not the case. I tried using a bit of code I found to force PA to use a different proxy, as suggested by a site dev, and that did not work. I also saw the dev recommending that people with this problem instead use Python 2.7. I did that, and still nothing changed. Are there any other possible solutions? Thanks in advance! My code is below.
from twilio.rest import TwilioRestClient
from datetime import *
import urllib.request
from bs4 import BeautifulSoup
from random import randint
import ast
#use julian date to calculate days left till summer
# put your own credentials here
def dateelle():
#use datetime to state date in Month Day, Year format.
todaylist = date.today().timetuple()
monthdict = {1:"January ", 2:"February ", 3:"March ", 4:"April ", 5:"May ", 6:"June ", 7:"July ", 8:"August ", 9:"September ", 10:"October ", 11:"November ", 12:"December "}
month = monthdict[todaylist[1]]
day = todaylist[2]
year = todaylist[0]
ultidate = str(month) + str(day) + ", " + str(year) + " "
return ultidate
def temple():
url = 'http://api.openweathermap.org/data/2.5/weather?id=5429522&APPID=0f25c0515e643bc2427f665f88a4d3eb'
headers = {}
headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36'
tri1 = urllib.request.Request(url, headers=headers)
tri2 = urllib.request.urlopen(tri1)
quell = BeautifulSoup(tri2)
tagwithtemp = int(ast.literal_eval(quell.text)['main']['temp'] * (9/5) - 459.67)
return tagwithtemp
def daysleft():
todayy = date.today().timetuple()[7]
allthatremains = 147 - todayy
return allthatremains
comp = {0:" You look nice today.",
1:" Have a good day.",
2:" Today will be a great day.",
3:" You are an important person who means much to the world.",
4: " Someone's day today will be brightened by your presence.",
5: " Despite the amount of people in the world, your existence matters. :)",
6: " There may be billions of people on this planet, but you still make a difference."}
billiam = "Good morning! Today is " + dateelle() + "and it is " + str(temple()) + " degrees." + comp[randint(0,6)] + " " + str(daysleft()) + " days left until summer!"
def asendoff():
global billiam
ACCOUNT_SID = "***" #the SID, AUTH token, and my phone number are censored
AUTH_TOKEN = "***"
nummer = ["***"]
client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)
for lexis in nummer:
client.messages.create(
to=lexis,
from_="+15026255861",
body=billiam,
)
if date.today().timetuple()[6] != 6 and date.today().timetuple()[6] != 7:
asendoff()
else:
pass
All external connections from PythonAnywhere free accounts are proxied to prevent abuse.
You need to set the proxy for the connection like this:
from twilio.rest.resources import Connection
from twilio.rest.resources.connection import PROXY_TYPE_HTTP
Connection.set_proxy_info(
"proxy.server",
3128,
proxy_type=PROXY_TYPE_HTTP
)
However, in Python 3, httplib2 (and therefore twilio-python) ignores the proxy settings (see the bottom of this page) and so tries to make a direct connection to Twilio which is blocked.