问题描述
我正在尝试在 pythonanywhere 中使用 Twilio,我知道我需要一个代理才能使其工作.我的代码如下所示:
I am trying to use Twilio in pythonanywhere and I know I need a proxy to make it work. My code looks like this:
class ProxiedTwilioHttpClient(HttpClient):
"""
General purpose HTTP Client for interacting with the Twilio API
"""
def request(self, method, url, params=None, data=None, headers=None, auth=None, timeout=None,
allow_redirects=False):
session = Session()
session.verify = get_cert_file()
session.proxies = {
"https" : "https://52.14.161.178:3128"
}
request = Request(method.upper(), url, params=params, data=data, headers=headers, auth=auth)
prepped_request = session.prepare_request(request)
response = session.send(
prepped_request,
allow_redirects=allow_redirects,
timeout=timeout,
)
return Response(int(response.status_code), response.content.decode('utf-8'))
def send_sms(phone, content):
client = Client(api_key, api_secret, account_sid, http_client=ProxiedTwilioHttpClient())
message = client.messages.create(
to=phone,
from_="+19999999999", #of course I use the correct one
body=content)
return(message.sid)
但是它返回以下错误:
.virtualenvs/sms/local/lib/python2.7/site-packages/requests/adapters.py",
line 502, in send raise ProxyError(e, request=request) requests.exceptions.ProxyError:
HTTPSConnectionPool(host='api.twilio.com', port=443):
Max retries exceeded with url: /2010-04-01/Accounts/XXXXXXXXX/Messages.json (Caused by ProxyError('Cannot connect to proxy.',
NewConnectionError('<urllib3.connection.Verif iedHTTPSConnection object at 0x7fa41a55e090>:
Failed to establish a new connection: [Errno 111] Connection refused',)))
我正在使用以下似乎适用于其他人的答案:https://stackoverflow.com/a/43608637/7298530
I am using the following answer that seems to work for others: https://stackoverflow.com/a/43608637/7298530
我该如何解决?
推荐答案
您指定您的代码应在 https://52.14.161.178:3128
使用代理.这不适用于 PythonAnywhere,您需要使用该服务提供的代理.要找出要使用的地址,请启动 Bash 控制台并运行
You're specifying that your code should use a proxy at https://52.14.161.178:3128
. That won't work on PythonAnywhere, you need to use the proxy that the service provides. To find out what address to use for that, start a Bash console and run
echo $http_proxy
[2018 编辑] 我们现在有一个关于 让 twilio 与 pythonanywhere 代理一起工作的特定页面一个>
[2018 edit] We now have a specific page on getting twilio to work with the pythonanywhere proxy
这篇关于无法向 twilio pythonanywhere 提供代理信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!