尝试在扭曲的中间代理服务器中读取标头。
在这里你可以看到简单的扭曲服务器(从stackoverflow中被盗,是的......)。它很好用,但我需要根据请求标头切换代理。无法理解我在哪里可以获得标题。它使用硬编码的代理服务器,但其想法是根据请求切换代理。
有什么想法吗?谢谢你的时间。 这是代码:
#!/usr/bin/env python
LISTEN_PORT = 8080
SERVER_PORT = 3128
SERVER_ADDR = "89.40.127.96"
from twisted.internet import protocol, reactor
class ServerProtocol(protocol.Protocol):
def __init__(self):
self.buffer = None
self.client = None
def connectionMade(self):
factory = protocol.ClientFactory()
factory.protocol = ClientProtocol
factory.server = self
reactor.connectTCP(SERVER_ADDR, SERVER_PORT, factory)
# Client => Proxy
def dataReceived(self, data):
if self.client:
self.client.write(data)
else:
self.buffer = data
# Proxy => Client
def write(self, data):
self.transport.write(data)
class ClientProtocol(protocol.Protocol):
def connectionMade(self):
self.factory.server.client = self
self.write(self.factory.server.buffer)
self.factory.server.buffer = ''
# Server => Proxy
def dataReceived(self, data):
self.factory.server.write(data)
# Proxy => Server
def write(self, data):
if data:
self.transport.write(data)
def main():
factory = protocol.ServerFactory()
factory.protocol = ServerProtocol
reactor.listenTCP(LISTEN_PORT, factory)
print "server"
reactor.run()
if __name__ == '__main__':
main()