質問
していましたコードから: http://blog.somethingaboutcode.com/?p=155 ):
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: ''
Whenever Iセットアップfirefoxまたはchromeやオペラのプロキシにlocalhost:8000ありませんの接続にプロキシ(できないのいずれかに接続しますページがわかっていないので接続にプロキシ).
Okでも失敗し、ログ取得しますこの出力時に設定しますfirefox用のプロキシでlocalhost:8000んの代理人から直接ウェブブラウザなどによるタイピングlocalhost:8000firefoxのアドレスバー)
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な白のlocalhost交通を利用した場合、フィドラーズ2では、委任状(いなくなっており自分のプロキシサーバーとしての作品であるため、使用フィドラーズ2のプロキシ).
解決
の KeyError
例外すときに直結することによる請求を代理人が含まれている必要がありま絶対URLではなく相対です。ブラウザをご利用の場合はないかと話を代理とすることURLのように /foo/bar
.なんと話しているプロキシではなく何らかの要求など http://example.com/foo/bar
.の http://example.com/
部分が重要なのは、プロキシ何本イベントは終了いたしました。がもっとも取得対象です。
しかし、Firefox、Chrome、オペラまでの接続にプロキシは一度設定していることの少し硬く伝えできたらと考えております。していることの確認を設定するHTTPプロキシではないが、その他の種類のプロキシ対応します。お客さま人数小児-幼児にダブルチェックをするためにツールを使うWiresharkを見て何が起こっているのかをネットワーク層である。
まれてくる可能性があることで、接続さんのプロキシが何かがおかしいことを防止してから完了.この場合、ロギングを有効にしない場合があります。かどうかを知ることができる代理人は受信の接続だけを見ていて、その出力に出力します。ロギングを有効にするために、:
from sys import stdout
from twisted.python.log import startLogging
startLogging(stdout)