【问题标题】:detect disconnect persistant curl connection检测断开持久卷曲连接
【发布时间】:2012-08-11 05:52:30
【问题描述】:

我应该在哪里检查 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

编辑:我已经修正了要点

【问题讨论】:

  • 您的要点没有正确缩进。我还建议您在此处发布您的来源。
  • @MikeSteder 我相信我已经修正了要点并在这里复制了它。谢谢!

标签: python curl


【解决方案1】:

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

除此之外,您是否还有更多裸露的except 子句可能会捕获sys.exit(1) 引发的SystemExit 异常?

【讨论】:

  • 我现在不看脚本,但我确信这不是问题。我们实际上已经找到了一个解决方案,并计划在第二天左右发布它作为答案。问题的要点是网络连接问题是一个问题。如果该行被丢弃,那么我们的提供者认为它仍然应该沿着管道发送数据,并且它从缓冲区写入错误中断开连接,而在我们这一端,我们正在从一个空缓冲区读取并且不知道连接已死.我们使用了几个低数据速度/超时选项来克服这个问题并在低数据时断开连接。
  • 您好,我们在使用 Gnip 时遇到了类似的问题。您能否详细说明您的解决方案?我们的代码看起来和你的非常相似。如何检测低数据速度?
  • @DeepKapadia 设置 LOW_SPEED_LIMIT 和 LOW_SPEED_TIME 选项,然后当网络流量低于这些限制时,您将收到错误 (28)(请参见此处:curl.haxx.se/libcurl/c/CURLOPT_LOW_SPEED_LIMIT.html
猜你喜欢
  • 2014-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-09
  • 1970-01-01
  • 2018-10-15
相关资源
最近更新 更多