如何使用 Python 向 Twilio API 提供代理信息 [英] How to provide proxy information to Twilio API with Python

查看:92
本文介绍了如何使用 Python 向 Twilio API 提供代理信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来 Twilio API 已更新,他们不再能够从 twilio.rest.resources import Connection 导入 这意味着向您展示如何设置代理的文档/答案Connection 类不再起作用.

It looks like the Twilio API was updated and they no longer have the ability to import from twilio.rest.resources import Connection which means that the docs/answers that show you how to set the proxy with the Connection class no longer work.

即:

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
)`

不起作用,因为没有要从中导入的资源类.

Does not work, since there is no resources class to import from.

使用 Python 3 和 Twilio 的最新代码为 Twilio Python 库设置代理信息的正确方法是什么?

Github 上记录了一个关于此的问题,但还没有具体的解决方案 https://github.com/twilio/twilio-python/issues/327

There is an issue logged on Github regarding this but it doesn't have a concrete solution yet https://github.com/twilio/twilio-python/issues/327

推荐答案

Twilio 开发人员布道者在这里.

Twilio developer evangelist here.

正如 GitHub 上的问题所说,我们已从 urllib2 切换到请求,但并没有完全提供默认 TwilioHttpClient 中的所有选项,例如代理.该问题还建议您将 HttpClient 子类化以自己添加代理.

As the issue on GitHub says, we have switched from urllib2 to Requests but not quite made available all the options, like proxies, in the default TwilioHttpClient. The issue also suggests that you subclass HttpClient to add in the proxy yourself.

据我所知,你可以复制大部分现有的TwilioHttpClient 将代理添加到会话对象.像这样:

As far as I can see, you can just copy the majority of the existing TwilioHttpClient adding the proxies to the session object. Like so:

from requests import Request, Session

from twilio.http import HttpClient, get_cert_file
from twilio.http.response import Response


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://x.x.x.x:yy"
                          }

        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'))

注意request方法中间对session.proxys的调用

然后,当您实例化 Client 时,包含新的 ProxiedTwilioHttpClient.

Then, when you instantiate your Client, include your new ProxiedTwilioHttpClient.

from twilio.rest import Client
from proxied_twilio_http_client import ProxiedTwilioHttpClient

client = Client(account_sid, auth_token, http_client=ProxiedTwilioHttpClient())

让我知道这是否有帮助.

Let me know if that helps at all.

这篇关于如何使用 Python 向 Twilio API 提供代理信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆