【问题标题】:Python SMS Twilio Script Having to Go Through Proxy WallPython SMS Twilio 脚本必须通过代理墙
【发布时间】:2017-06-29 03:40:02
【问题描述】:

我今天开始在 Python3 中使用 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() 作为一种可能的解决方案,但完全不知道如何实现它。

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

谢谢。

【问题讨论】:

    标签: python python-3.x twilio twilio-api


    【解决方案1】:

    诀窍是传入一个自定义的 HttpClient,如下所示:

    import os
    
    from requests import Session
    
    from twilio.rest import Client
    from twilio.http.http_client import HttpClient
    
    proxy_server = 'http://user:pass@proxy.example.com: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)
    

    【讨论】:

    • 好的,在客户端之后我可以在我的问题中编写其余代码?
    • 是的,试试看。
    • 今晚我不能,因为我实际上正要上床睡觉,但如果我遇到问题,可以联系你吗?如果没有问题发生,我会打勾。
    • 你好,布尔汉。这是我得到的错误:github.com/Jrachman/debug/blob/master/errorsend_sms.
    猜你喜欢
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    • 1970-01-01
    相关资源
    最近更新 更多