2012-05-22 148 views
2

我正在使用python扭曲模块来截取标题。我有以下脚本:扭曲的http服务器错误

from twisted.web import proxy, http 
from twisted.internet import reactor 

g_port = 8080 #port for listening 
g_request =() # action, params, method 

class akaProxy(proxy.Proxy): 

    def dataReceived(self, data): 

     action = None 
     params = None 
     method = None 
     global g_request 

     headers = data.split("\n") 
     request = headers[0].split(" ") 
     params = headers[len(headers)-1] 

     method = request[0].lower() 
     action = request[1].lower() 

     print method, action 

     return proxy.Proxy.dataReceived(self, data) 

class ProxyFactory(http.HTTPFactory): 
    protocol = akaProxy 


factory = ProxyFactory() 
reactor.listenTCP(g_port, factory) 
reactor.run() 

当我把它用localproxy网页浏览器上运行(this URL)给我这个错误:

Unhandled Error 
Traceback (most recent call last): 
    File "test2.py", line 33, in <module> 
    reactor.run() 
    File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1169, in run 
    self.mainLoop() 
    File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1181, in mainLoop 
    self.doIteration(t) 
    File "/usr/lib/python2.7/dist-packages/twisted/internet/pollreactor.py", line 167, in doPoll 
    log.callWithLogger(selectable, _drdw, selectable, fd, event) 
--- <exception caught here> --- 
    File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 84, in callWithLogger 
    return callWithContext({"system": lp}, func, *args, **kw) 
    File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 69, in callWithContext 
    return context.call({ILogContext: newCtx}, func, *args, **kw) 
    File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 118, in callWithContext 
    return self.currentContext().callWithContext(ctx, func, *args, **kw) 
    File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 81, in callWithContext 
    return func(*args,**kw) 
    File "/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.py", line 599, in _doReadOrWrite 
    self._disconnectSelectable(selectable, why, inRead) 
    File "/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.py", line 260, in _disconnectSelectable 
    selectable.readConnectionLost(f) 
    File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 257, in readConnectionLost 
    self.connectionLost(reason) 
    File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 433, in connectionLost 
    Connection.connectionLost(self, reason) 
    File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 277, in connectionLost 
    protocol.connectionLost(reason) 
    File "/usr/lib/python2.7/dist-packages/twisted/web/http.py", line 455, in connectionLost 
    self.handleResponseEnd() 
    File "/usr/lib/python2.7/dist-packages/twisted/web/proxy.py", line 88, in handleResponseEnd 
    self.father.finish() 
    File "/usr/lib/python2.7/dist-packages/twisted/web/http.py", line 866, in finish 
    "Request.finish called on a request after its connection was lost; " 
exceptions.RuntimeError: Request.finish called on a request after its connection was lost; use Request.notifyFinish to keep track of this. 

但它继续工作,任何人都可以解释错误以及如何能我修复它?

回答

1

此消息出现在如下描述的情况中:您有一些处理http请求的代码,并且在http连接丢失之前,请求的finish方法没有被调用(意思是现在有没有办法将结果发送给请求者)。

你可能不关心这种情况。如果你不这样做,那么当你的请求的_disconnected属性为真时,你应该在你的代码中做些什么来保持Request.finish方法不被调用。那,或者只是忽略错误。

+0

所以这不是由服务器中的一些错误引起的问题,而是客户端很快断开连接?我通过在加载favicon(直接通过url)时按住浏览器上的F5来获得这些例外。服务器是否进入损坏状态,还是从此干净地恢复? –

+0

本身扭曲不会进入损坏状态,但如果您的连接始终成功完成,那么_your_服务器可能会做出无效假设。 –