[英]Python twisted proxy to send 2 requests
我如何处理此代码,以便能够发送2个单独的请求。 请求将按以下顺序进行:
请求1:
HEAD http://google.com
Host: google.com
...等待来自Google服务器的回复...
Request2:
GET http://yahoo.com HTTP/1.1
User-Agent: mozilla
Accept: */*
从浏览器发送的第二个请求,而所有请求的第一个请求是静态的...
我要修改的代码是:
from twisted.web import proxy, http
class SnifferProxy(proxy.Proxy):
def allContentReceived(self):
print "Received data..."
print "method = %s" % self._command
print "action = %s" % self._path
print "ended content manipulation\n\n"
return proxy.Proxy.allContentReceived(self)
class ProxyFactory(http.HTTPFactory):
protocol = SnifferProxy
if __name__ == "__main__":
from twisted.internet import reactor
reactor.listenTCP(8080, ProxyFactory())
reactor.run()
扭曲的代理将连接到另一个外部代理。不胜感激。
我认为您可以通过将对Proxy.allContentReceived
方法的调用添加为使用Agent的HEAD请求的回调来获得所需的内容。
from twisted.internet import reactor from twisted.web import proxy, http
from twisted.web.client import Agent
from twisted.web.http_headers import Headers
agent = Agent(reactor)
class SnifferProxy(proxy.Proxy):
def allContentReceived(self):
def cbHead(result):
print "got response for HEAD"
def doProxiedRequest(result):
proxy.Proxy.allContentReceived(self)
# I assumed self._path, but it looks OP wants to do the
# HEAD request to the same path always
PATH = "http://foo.bar"
d = agent.request(
'HEAD', PATH, Headers({'User-Agent': ['twisted']}), None)
d.addCallback(cbHead)
d.addCallback(doProxiedRequest)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.