为什么我的使用twilio的脚本在本地完美运行,但在Python Anywhere上抛出错误?

时间:2016-04-14 02:09:29

标签: python python-3.x twilio pythonanywhere

当我在本地运行脚本时,我的脚本执行完美,但是当我在Python Anywhere上执行它时,它会抛出以下错误:

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

现在,我已经在Stack Overflow和Python本身的论坛上进行了梳理,而且我发现的解决方案都没有成功。一个建议是脚本不起作用,因为我试图访问的网站不在白名单上,而事实并非如此。我尝试使用我发现的一些代码强制PA使用不同的代理,如网站开发人员所建议的那样,但是没有用。我也看到开发人员建议有这个问题的人使用Python 2.7。我这样做了,但仍然没有改变。还有其他可能的解决方案吗?提前致谢!我的代码如下。

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

2 个答案:

答案 0 :(得分:4)

代理来自PythonAnywhere免费帐户的所有外部连接以防止滥用。

您需要为此连接设置代理:

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
)

但是,在Python 3中,httplib2(以及twilio-python)忽略代理设置(请参阅this page的底部),因此尝试直接连接到被阻止的Twilio。

答案 1 :(得分:1)

Twilio开发者传道者在这里。

previous reports表示PA有旧版本的库。几个月前,我们在库和API中设置了pretty significant change来强制执行SSL。

如果您使用的是最新版本的库,则不应对您进行任何更改。从您发布的错误中,我无法确切地知道PA正在使用哪个版本的库,但是进一步挖掘,用户似乎suggest you need a proxy

我的建议是略微改变你的方法,也许将其部署到Heroku(它是免费的,你将能够使用你想要的确切库版本)。这样,如果您发现它在那里工作,您可以联系PA并检查他们拥有哪个库版本,您将能够告诉他们是否需要更新。

希望这有助于你