2
當我使用twistedmatrix ProxyClient
時,我該如何gunzip和過程響應部分?蟒蛇和扭曲的代理,如何在飛行中gunzip?
我需要檢查文本或javascript和ajax查詢/答案。是否應該使用handleResponseEnd
?
我認爲這是handleResponsePart內,但它看起來像我誤解了點什麼,這是我的骨架代碼:
from twisted.python import log
from twisted.web import http, proxy
class ProxyClient(proxy.ProxyClient):
"""Mange returned header, content here.
Use `self.father` methods to modify request directly.
"""
def handleHeader(self, key, value):
# change response header here
log.msg("Header: %s: %s" % (key, value))
proxy.ProxyClient.handleHeader(self, key, value)
def handleResponsePart(self, buffer):
# this part below do not work,
# looks like @ this moment i do not have 'Content-Encoding' or 'Content-Type'
# what am i misunderstading?
cEncoding = self.father.getAllHeaders().get('Content-Encoding', '')
cType = self.father.getAllHeaders().get('Content-Type', '')
print >> sys.stderr, 'Content-Encoding', cEncoding
print >> sys.stderr, 'Content-Type', cType
if ('text' in cType.lower() or 'javascript' in cType.lower()) and 'gzip' in cEncoding.lower():
buf = StringIO(buffer)
s = gzip.GzipFile(mode="rb", fileobj=buf)
content = s.read(len(buffer))
# here process content as it should be gunziped
proxy.ProxyClient.handleResponsePart(self, buffer)
class ProxyClientFactory(proxy.ProxyClientFactory):
protocol = ProxyClient
class ProxyRequest(proxy.ProxyRequest):
protocols = dict(http=ProxyClientFactory)
class Proxy(proxy.Proxy):
requestFactory = ProxyRequest
class ProxyFactory(http.HTTPFactory):
protocol = Proxy
從我的記錄,我有:
2013-06-11 14:07:33+0200 [ProxyClient,client] Header: Date: Tue, 11 Jun 2013 12:07:25 GMT
2013-06-11 14:07:33+0200 [ProxyClient,client] Header: Server: Apache
...
2013-06-11 14:07:33+0200 [ProxyClient,client] Header: Content-Type: text/html;charset=ISO-8859-1
...
2013-06-11 14:07:33+0200 [ProxyClient,client] Header: Content-Encoding: gzip
...
2013-06-11 14:07:33+0200 [ProxyClient,client] Header: Connection: close
因此我應該有兩個條件好!請問我錯過了什麼?
即使我不感興趣的第二種方式,即刪除請求的接受,就像這樣,是否可以這樣做: (順便說一句,它看起來像它不工作或測試的Web服務器不關心的事實,我們不希望收到的gzip編輯內容)
class ProxyRequest(proxy.ProxyRequest):
protocols = dict(http=ProxyClientFactory)
def process(self):
# removing the accept so that we do not tell "i'm ok with gzip encoded content" and should receive only not gzip-ed
self.requestHeaders.removeHeader('accept')
self.requestHeaders.removeHeader('accept-encoding')