Skip to main content
 首页 » 编程设计

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

2025年05月04日27fff_TT

我的脚本在本地运行时完美执行,但在 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 

请您参考如下方法:

来自 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。