Python短信Twilio脚本必须经过代理墙

问题描述:

我今天开始使用Twilio for Python3,并且发现甚至难以获得第一个基本脚本。Python短信Twilio脚本必须经过代理墙

send_sms.py

from twilio.rest import Client 

# Your Account SID from twilio.com/console 
account_sid = "your_account_sid" 
# Your Auth Token from twilio.com/console 
auth_token = "your_auth_token" 

client = Client(account_sid, auth_token) 

message = client.messages.create(
    to="+15558675309", 
    from_="+15017250604", 
    body="Hello from Python!") 

print(message.sid) 

send_sms.py是,Twilio “入门” 面板问我考出的第一个程序。问题是我拥有一个包含用户名,密码和代理站点的代理网络。没有这个连接,我得到ConnectionRefusedError。经过几次测试,我意识到无法连接到代理网络意味着失败的程序。我知道使用ProxiedTwilioHttpClient()作为可能的解决方案,但绝对不知道如何实现它。

我要求的是如何将代理网络身份验证添加到上述代码示例。我可以将该样本用于其他特定程序的未来参考。如果您有任何问题,请随时在下面添加评论。

谢谢。

的窍门是在自定义HttpClient的传递,就像这样:

import os 

from requests import Session 

from twilio.rest import Client 
from twilio.http.http_client import HttpClient 

proxy_server = 'http://user:[email protected]:80' 

ProxySession = Session() 
ProxySession.proxies.update({'http': os.environ.get('http_proxy', proxy_server)}) 

ProxyClient = HttpClient() 
ProxyClient.session = ProxySession 

account_sid = 'xxx' 
account_token = 'xxx' 

client = Client(account_sid, account_token, http_client=ProxyClient) 
+0

好和客户端后,我可以写代码的其余部分在我的问题? –

+0

是的,试试吧。 –

+0

我今晚不能睡觉,因为我实际上即将睡觉,但如果遇到问题,我可以与您联系吗?如果没有问题发生,我只需点击复选标记。 –