2010-08-04 68 views
1

我一直在运行此代码(来自:http://blog.somethingaboutcode.com/?p=155):扭曲的Web代理

from twisted.internet import reactor 
from twisted.web import http 
from twisted.web.proxy import Proxy, ProxyRequest, ProxyClientFactory, ProxyClient 
from ImageFile import Parser 
from StringIO import StringIO 

class InterceptingProxyClient(ProxyClient): 
    def __init__(self, *args, **kwargs): 
     ProxyClient.__init__(self, *args, **kwargs) 
     self.image_parser = None 

    def handleHeader(self, key, value): 
     if key == "Content-Type" and value in ["image/jpeg", "image/gif", "image/png"]: 
      self.image_parser = Parser() 
     if key == "Content-Length" and self.image_parser: 
      pass 
     else: 
      ProxyClient.handleHeader(self, key, value) 

    def handleEndHeaders(self): 
     if self.image_parser: 
      pass #Need to calculate and send Content-Length first 
     else: 
      ProxyClient.handleEndHeaders(self) 

    def handleResponsePart(self, buffer): 
     print buffer 
     if self.image_parser: 
      self.image_parser.feed(buffer) 
     else: 
      ProxyClient.handleResponsePart(self, buffer) 

    def handleResponseEnd(self): 
     if self.image_parser: 
      image = self.image_parser.close() 
      try: 
       format = image.format 
       image = image.rotate(180) 
       s = StringIO() 
       image.save(s, format) 
       buffer = s.getvalue() 
      except: 
       buffer = "" 
      ProxyClient.handleHeader(self, "Content-Length", len(buffer)) 
      ProxyClient.handleEndHeaders(self) 
      ProxyClient.handleResponsePart(self, buffer) 
     ProxyClient.handleResponseEnd(self) 

class InterceptingProxyClientFactory(ProxyClientFactory): 
    protocol = InterceptingProxyClient 

class InterceptingProxyRequest(ProxyRequest): 
    protocols = {'http': InterceptingProxyClientFactory} 
    ports = {"http" : 80} 

class InterceptingProxy(Proxy): 
    requestFactory = InterceptingProxyRequest 

factory = http.HTTPFactory() 
factory.protocol = InterceptingProxy 

reactor.listenTCP(8000, factory) 
reactor.run() 

每当我得到这个,去127.0.0.1:8000我得到这个:

Traceback (most recent call last): 
    File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\python\log.py", 
line 84, in callWithLogger 
    return callWithContext({"system": lp}, func, *args, **kw) 
    File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\python\log.py", 
line 69, in callWithContext 
    return context.call({ILogContext: newCtx}, func, *args, **kw) 
    File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\python\context.p 
y", line 59, in callWithContext 
    return self.currentContext().callWithContext(ctx, func, *args, **kw) 
    File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\python\context.p 
y", line 37, in callWithContext 
    return func(*args,**kw) 
--- <exception caught here> --- 
    File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\internet\selectr 
eactor.py", line 146, in _doReadOrWrite 
    why = getattr(selectable, method)() 
    File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\internet\tcp.py" 
, line 460, in doRead 
    return self.protocol.dataReceived(data) 
    File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\protocols\basic. 
py", line 251, in dataReceived 
    why = self.lineReceived(line) 
    File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\web\http.py", li 
ne 1573, in lineReceived 
    self.allContentReceived() 
    File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\web\http.py", li 
ne 1641, in allContentReceived 
    req.requestReceived(command, path, version) 
    File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\web\http.py", li 
ne 807, in requestReceived 
    self.process() 
    File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\web\proxy.py", l 
ine 147, in process 
    port = self.ports[protocol] 
exceptions.KeyError: '' 

每当我安装Firefox或Chrome或Opera使用本地主机上的代理:8000没有到代理做(我可以不再连接到任何页面,虽然这可能是因为它不是连接到代理)的连接。


好它仍然失败,并记录当我设置Firefox的使用本地主机代理我得到这样的输出:8000,也不要直接从网络浏览器(访问代理,如通过键入本地主机:8000在Firefox的地址栏)

2010-08-04 12:31:18-0400 [-] Log opened. 
2010-08-04 12:31:29-0400 [-] twisted.web.http.HTTPFactory starting on 8000 
2010-08-04 12:31:29-0400 [-] Starting factory <twisted.web.http.HTTPFactory inst 
ance at 0x010B3EE0> 
2010-08-04 12:33:55-0400 [-] Received SIGINT, shutting down. 
2010-08-04 12:33:55-0400 [twisted.web.http.HTTPFactory] (Port 8000 Closed) 
2010-08-04 12:33:55-0400 [twisted.web.http.HTTPFactory] Stopping factory <twiste 
d.web.http.HTTPFactory instance at 0x010B3EE0> 
2010-08-04 12:33:55-0400 [-] Main loop terminated. 

但是,当我直接访问代理服务器时,我得到了关键错误。

而且嗅探我不能; Wireshark的似乎没有嗅出本地主机的流量,如果我使用Fiddler 2它本身设置为代理服务器(所以我不再使用我的代理服务器),然后可以(因为它使用提琴手2的代理)。

+0

只是格式化的建议:不要使用[code]标签。文字前需要4个空格才能成为代码。你可以在这里复制+粘贴,选择并点击框上方的[1010101]按钮。它会自动为你做4个空格。 – avacariu 2010-08-04 05:10:26

回答

1

KeyError异常,当你通过请求到代理必须包含一个绝对URL,而不是相对一个事实直接连接造成你看。如果您的浏览器不知道它正在与代理交谈,它将请求一个URL,如/foo/bar。如果它知道它正在与代理交谈,它将会请求诸如http://example.com/foo/bar之类的内容。该http://example.com/部分很重要,因为它是唯一的办法,代理知道它应该走下车和检索。

至于为什么一旦你配置他们这样做没有火狐,Chrome,也不歌剧院将连接到代理,这是一个有点难以解释。确保你正在配置一个“HTTP代理”,而不是任何其他支持的代理。一旦你仔细检查过,你可能想使用Wireshark这样的工具来更好地了解网络层发生了什么。

这可能是连接是真的向代理服务器发出的,但是其他的错误导致它们无法完成。在这种情况下,如果未启用日志记录,则可能无法通过查看其输出来告诉代理正在接收连接。要启用日志记录,请尝试:

from sys import stdout 
from twisted.python.log import startLogging 
startLogging(stdout) 
+0

修正了它,我把代理设置设置错了;将IP地址设置为192.x.x.x或10.x.x.x,或者不管它是127.0.0.1还是localhost – Zimm3r 2010-08-11 23:14:50