Python HTTP 599:连接已关闭(Tornado)

时间:2013-04-05 09:49:34

标签: python tornado http-error connection-close

我收到了这个错误:

  

HTTP 599:连接关闭[E 130405 11:43:14 web:1031]未捕获   异常GET / networks / 1 / sensors / 1 / alarm(127.0.0.1)

执行以下代码时:

from tornado.stack_context import ExceptionStackContext

def handle_exc(*args):
print('Exception occured')
return True

@tornado.gen.engine
def check_status_changes(netid, sensid):

    como_url = "".join(['http://131.114.52:44444/ztc?netid=', str(netid), '&sensid=', str(sensid), '&start=-5s&end=-1s'])

    http_client = AsyncHTTPClient()
    response = yield tornado.gen.Task(http_client.fetch, como_url)

    if response.error is not None:
            print("Terzo")
            print response.error
            with ExceptionStackContext(handle_exc):
                response.rethrow()
    else:
        print('Handle request')

    for line in response.body.split("\n"):
                if line != "": 
                    #net = int(line.split(" ")[1])
                    #sens = int(line.split(" ")[2])
                    #stype = int(line.split(" ")[3])
                    value = int(line.split(" ")[4])
                    print value
                    yield value
                    return


class AlarmHandler(BaseHandler):
    @tornado.web.authenticated
    @tornado.web.asynchronous
    @tornado.gen.engine
    def get(self, netid, sensid):
        self.lock_tables("read", ['devices'])
        status = self.db.get("SELECT status from devices \
                          WHERE id=%s AND network_id=%s", sensid, netid)
        print("Primo")
        print status

        try:
            periodic = tornado.ioloop.PeriodicCallback(check_status_changes(netid, sensid), 5000)
            value = periodic.start()
            print("Secondo")
            print value
        except:
            print("Quarto")
            periodic.stop()
            self.finish()
            return
        if value != status['status']:

            self.lock_tables("write", ['devices'])
            self.db.execute("UPDATE devices SET status=%s \
                             WHERE id=%s AND network_id=%s", value, netid, sensid)
            self.unlock_tables()
            self.notice("Status changed")

在班级AlarmHandler中有一个名为check_status_changes的周期例程。在这个函数中,当有response.error时,我获得了标题的错误。

如何设置错误条件以便返回班级并管理情况?谢谢。

其他信息

如果我做一个龙卷风的屏幕,我会看到:

  

Primo

     

{'status':无}

     

Secondo

     

     

Terzo

     

HTTP 599:连接已关闭

     

发生异常

所以,我认为程序会在异常发生之前关闭连接!

在html consolle中我看到了这个错误:

  

文件“./wsn.py”,第226行,在check_status_changes中       for response in response.body.split(“\ n”):AttributeError:'NoneType'对象没有属性'split'

1 个答案:

答案 0 :(得分:0)

你提出异常:

if response.error:
        print("Terzo")
        print response.error
        raise Exception(response.error)
        return

这将是Uncaught Exception。您应该对此进行编码以处理异常,而不会引发异常。例如。记录错误消息并中止数据库更新。