简体   繁体   English

如何在Twitty Twister中启用HTTP代理?

[英]How do I go about enabling HTTP proxy in Twitty Twister?

I'm using the twittytwister module to implement a Twitter client but it looks like it's no longer being developed. 我正在使用twittytwister模块来实现Twitter客户端,但看起来好像不再在开发它了。 I need to connect to an HTTP proxy to be able to reach the internet but the module does not have such an option so I'm looking at modifying it. 我需要连接到HTTP代理才能访问Internet,但是该模块没有这样的选项,因此我正在考虑对其进行修改。

def __downloadPage(factory, *args, **kwargs):
    downloader = factory(*args, **kwargs)
    if downloader.scheme == 'https':
        from twisted.internet import ssl
        contextFactory = ssl.ClientContextFactory()
        reactor.connectSSL(downloader.host, downloader.port,
                           downloader, contextFactory)
    else:
        reactor.connectTCP(downloader.host, downloader.port,
                           downloader)
    return downloader

def getPage(url, *args, **kwargs):
    return __downloadPage(client.HTTPClientFactory, url, *args, **kwargs)

What can I do here to make it connect with my proxy? 我在这里可以做些什么使其与代理连接? Do I replace client.HTTPClientFactory with something else? 我可以用其他东西替换client.HTTPClientFactory吗?

The newer (added in Twisted 9.0.0) HTTP client API, twisted.web.client.Agent , includes support for connecting to an HTTP proxy. 较新的(在Twisted 9.0.0中添加)HTTP客户端API twisted.web.client.Agent包括对连接到HTTP代理的支持。 For example, you could write: 例如,您可以编写:

from twisted.web.client import Agent, ProxyAgent
from twisted.internet.endpoints import clientFromString
from twisted.internet import reactor

from os import environ

try:
    proxy = environ["HTTP_PROXY"]
except KeyError:
    agent = Agent(reactor)
else:
    agent = ProxyAgent(clientFromString(reactor, proxy))

See the endpoints documentation for details about the expected format of the HTTP_PROXY environment variable this example uses. 有关此示例使用的HTTP_PROXY环境变量的预期格式的详细信息,请参见端点文档

Unfortunately it looks like twittytwister uses the older HTTP client API so you'll need to port it to use Agent before you can benefit from ProxyAgent . 不幸的是,twittytwister似乎使用了较旧的HTTP客户端API,因此您必须先将其移植以使用Agent然后才能从ProxyAgent受益。 Fortunately the getPage style API is fairly limited so it shouldn't be very hard to replace it with Agent -style code. 幸运的是, getPage样式的API相当有限,因此用Agent样式的代码替换它应该不难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM