4

我应该在哪里检查 pycurl 持久连接中的断开连接?

在我的脚本中的某个地方,连接正在死亡/超时/抛出错误,但脚本保持打开状态。我需要检测问题,以便重新启动脚本。

我们正在连接 gnip(社交媒体数据提供商)

我的代码在这里:https ://gist.github.com/3353033

我已经阅读了 libcurl 的选项并通读了 php curl_setopts 文档,因为它们也利用了 libcurl。

class Client:  
    time_start = time.time()
    content = ""
    def __init__(self,options):
        self.options = options  
        self.buffer = ""  
        self.conn = pycurl.Curl()  
        self.conn.setopt(pycurl.USERPWD, "%s:%s" % (USER, PASS))  
        self.conn.setopt(pycurl.ENCODING,'gzip')
        self.conn.setopt(pycurl.URL, STREAM_URL)  
        self.conn.setopt(pycurl.WRITEFUNCTION, self.on_receive)  
        self.conn.setopt(pycurl.FOLLOWLOCATION,1)
        self.conn.setopt(pycurl.MAXREDIRS, 5)
        self.conn.setopt(pycurl.COOKIEFILE,"cookie.txt")
        try:
            self.conn.perform()  
        except Exception,e:
            print e.message

    def on_receive(self, data):
        self.buffer += data  

        if data.endswith("\r\n") and self.buffer.strip():  
            if(self.triggered()):
                if(len(self.buffer) != 0 ):
                    try:
                        SaveThread(self.buffer).start()
                    except Exception, e:
                        print "something i commented would have told you there was an error"
                        system.exit(1) 
                self.buffer = ""

    def triggered(self):
        # First trigger based on size then based on time..
        if (len(self.buffer) > SAVE_FILE_LENGTH):
            return True
        time_end = time.time()
        if (((time_end - self.time_start) > ROLL_DURATION)):  #for the time frame 
            self.time_start=time.time()
            return True
        return False

编辑:我已经修复了要点

4

1 回答 1

0

上面的代码system.exit(1)应该是对的sys.exit(1)吧?

除此之外,您是否还有更多except可能捕获由SystemExit引发的异常的裸条款sys.exit(1)

于 2012-08-14T21:52:09.220 回答