socks的twisted客户端端点{4,4a,5}
txsocksx的Python项目详细描述
TXSocksx
txsocksx是Twisted10.1或 更大的。该代码在github上可用:https://github.com/habnabit/txsocksx
示例
这些示例假定您熟悉如何使用Twisted endpoints。为了 简单起见,大多数示例将使用socks5。
验证
一个通过 方法参数。例如,使用用户名spam连接 密码eggs:
exampleEndpoint = SOCKS5ClientEndpoint( 'example.com', 6667, proxyEndpoint, methods={'login': ('spam', 'eggs')})
但是,这将禁用匿名身份验证。使用登录或 匿名身份验证,指定两种方法:
exampleEndpoint = SOCKS5ClientEndpoint( 'example.com', 6667, proxyEndpoint, methods={'login': ('spam', 'eggs'), 'anonymous': ()})
methodsdict必须始终从字符串映射到元组。
袜子4
socks4没有身份验证,但是有一个可配置的“用户id”,它 默认为空字符串:
exampleEndpoint = SOCKS4ClientEndpoint( 'example.com', 6667, proxyEndpoint, user='spam')
通过Tor连接到一个对象
要通过tor连接到端口6667上的example.com,需要创建 SOCKS5ClientEndpoint包装tor服务器的端点:
torServerEndpoint = TCP4ClientEndpoint(reactor, '127.0.0.1', 9050) exampleEndpoint = SOCKS5ClientEndpoint('example.com', 6667, torServerEndpoint)
从那里建立连接的过程与往常一样:
deferred = exampleEndpoint.connect(someFactory)
txsocksx不会执行任何DNS解析,因此主机名example.com 不会泄漏;tor将直接接收主机名并执行dns查找 本身。
ToR允许通过socks4或socks5进行连接,并且不希望用户ID为 使用socks4客户端时发送。
取消连接
有时一个人厌倦了等待,想中止连接尝试。为了 例如,要在10秒后中止整个连接尝试:
torServerEndpoint = TCP4ClientEndpoint(reactor, '127.0.0.1', 9050) exampleEndpoint = SOCKS5ClientEndpoint('example.com', 6667, torServerEndpoint) deferred = exampleEndpoint.connect(someFactory) reactor.callLater(10, deferred.cancel)
这是一个简单的示例;实际代码应该取消返回的IDelayedCall 当延迟触发时,reactor.callLater。代码看起来就像 这个:
torServerEndpoint = TCP4ClientEndpoint(reactor, '127.0.0.1', 9050) exampleEndpoint = SOCKS5ClientEndpoint('example.com', 6667, torServerEndpoint) deferred = exampleEndpoint.connect(someFactory) canceler = reactor.callLater(10, deferred.cancel) def cancelCanceler(result): if canceler.active(): canceler.cancel() return result deferred.addBoth(cancelCanceler)
发出http请求
Twisted的内置Agenthttp客户端不支持将 在15之前的任意端点,因此^ {TT1}$提供最大的^ {TT12}$。 兼容性。
虽然txsocksx只需要twisted 10.1,txsocksx.http需要twisted 12.1或更高。它的用法与普通用法几乎相同Agent用法:
torServerEndpoint = TCP4ClientEndpoint(reactor, '127.0.0.1', 9050) agent = SOCKS5Agent(reactor, proxyEndpoint=torServerEndpoint) deferred = agent.request('GET', 'http://example.com/')
注意proxyEndpoint参数必须作为关键字传递 争论。有第二个可选的仅关键字参数用于传递 作为^{tt18},SOCKS5ClientEndpoint的附加参数$ 构造它:
torServerEndpoint = TCP4ClientEndpoint(reactor, '127.0.0.1', 9050) agent = SOCKS5Agent(reactor, proxyEndpoint=torServerEndpoint, endpointArgs=dict(methods={'login': ('spam', 'eggs')})) deferred = agent.request('GET', 'http://example.com/')
SOCKS5Agent通过TLSWrapClientEndpoint透明地支持https。
对于twisted 15.0或更高版本的用户,SOCKS5Agent还实现 IAgentEndpointFactory。
升级到TLS
有时,一有代理协商,就想切换到说tls。 结束了。为此,有txsocksx.tls。将端点包装为 TLSWrapClientEndpoint,连接将使用tls升级到 代理协商完成后:
torServerEndpoint = TCP4ClientEndpoint(reactor, '127.0.0.1', 9050) exampleEndpoint = SOCKS5ClientEndpoint('example.com', 6667, torServerEndpoint) tlsEndpoint = TLSWrapClientEndpoint(exampleEndpoint) deferred = tlsEndpoint.connect(someFactory)
在代理上代理
由于txsocksx的可组合设计,从一个socks连接起来很简单 代理到另一个:
torServerEndpoint = TCP4ClientEndpoint(reactor, '127.0.0.1', 9050) firstProxyEndpoint = SOCKS5ClientEndpoint( 'first-proxy.example.com', 1080, torServerEndpoint) secondProxyEndpoint = SOCKS4ClientEndpoint( 'second-proxy.example.com', 1080, firstProxyEndpoint) finalHop = SOCKS5ClientEndpoint( 'example.com', 113, secondProxyEndpoint) deferred = finalHop.connect(someFactory)