我在 python 中有一个 curl 请求,它将大量数据输出到 writefunction (CURLOPT_WRITEFUNCTION)。如果满足某个条件,我希望能够取消来自 writefunction 的 curl 请求。我尝试过使用 ch.close(),但错误提示它在执行请求时无法关闭请求。还有其他方法可以让它停止写入功能吗?这是我的代码:
self.ch = pycurl.Curl()
self.ch.setopt(pycurl.URL, file_url)
self.ch.setopt(pycurl.CONNECTTIMEOUT, 60)
self.ch.setopt(pycurl.WRITEFUNCTION, self.WriteWrapper)
self.ch.setopt(pycurl.HEADERFUNCTION, self.ParseHeaders)
self.ch.setopt(pycurl.FOLLOWLOCATION, True)
self.ch.setopt(pycurl.COOKIE, cookies[file_host])
self.ch.setopt(pycurl.HTTPHEADER, self.headers_received)
self.ch.perform()
self.ch.close()
def do_POST(self):
return self.do_GET()
def WriteWrapper(self, data):
if self.do_curl_request:
try:
return self.wfile.write(data)
except:
self.ch.close() # This doesn't work :(