Skip to main content
 首页 » 编程设计

PythonAnywhere 连接被拒绝错误 : [Errno 111] Connection refused when used with Twilio

2024年10月24日9zhenyulu

我正在使用 PythonAnywhere 来运行一个简单的脚本。这是一个试验,我写了一个脚本,每小时给我发一条短信。但每次我运行我的脚本时,它都会显示错误。

我的脚本是:

from twilio.rest import TwilioRestClient 
from time import sleep 
 
client = TwilioRestClient('AC******************', '**********************') 
 
while True: 
    client.messages.create( 
        to="+1123456789", 
        from_="+1987654321", 
        body="Trial.", 
    ) 
    print('Message Sent!') 
    sleep(3600) 

错误是:

Traceback (most recent call last): 
  File "/home/dhruvsomani/sms.py", line 12, in <module> 
    body="Trial.", 
  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 341, in create_instance 
    data=transform_params(body)) 
  File "/usr/local/lib/python3.4/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.4/dist-packages/twilio/rest/resources/base.py", line 148, in make_twilio_request 
    resp = make_request(method, uri, **kwargs) 
  File "/usr/local/lib/python3.4/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.4/dist-packages/httplib2/__init__.py", line 1292, 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 1042, 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 966, 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 

我还没有找到解决这个问题的方法。我向您保证,与 Twilio 相关的详细信息是正确的。这是一个 PythonAnywhere 安全错误。请告诉我如何克服这个问题。

请您参考如下方法:

这里是 PythonAnywhere 开发人员:经过多次反复,我们终于解决了这个问题——似乎 Twilio 客户端库所依赖的 httplib2 库忽略了 Python 3.x 的代理设置。

解决方案是使用 Python 2.7。