如何取消来自 WRITEFUNCTION 的curl 请求?

发布于 2024-10-18 21:22:04 字数 878 浏览 0 评论 0原文

我在 python 中有一个curl 请求,它将大量数据输出到写入函数(CURLOPT_WRITEFUNCTION)。如果满足特定条件,我希望能够取消来自 writefunction 的curl 请求。我尝试过使用 ch.close(),但错误表明它在执行时无法关闭请求。还有其他方法可以让它从 writefunction 中停止吗?这是我的代码:

    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 :(

I have a curl request in python which outputs a large amount of data to a writefunction (CURLOPT_WRITEFUNCTION). I want to be able to cancel the curl request from the writefunction if a certain condition is met. I've tried using ch.close(), but that errors saying that it cant close a request while its being performed. Any other way to get it to stop from a writefunction? Heres my code:

    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 :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

又爬满兰若 2024-10-25 21:22:04

如果从 write 函数返回的数字不等于它认为正在写入的数字,pycurl 会引发错误,因此返回 -1 或在 write 函数内引发异常将导致它引发 pycurl.error。请注意,返回“None”被解释为“所有字节已写入”。

>>> class Writer:
...   def __init__(self):
...     self.count = 0
...   def write(self, data):
...     print "Write called", len(data)
...     return -1
...
>>> curl = pycurl.Curl()
>>> writer = Writer()
>>> curl.setopt(pycurl.WRITEFUNCTION, writer.write)
>>> curl.setopt(pycurl.URL, "file:///some_big_file.txt")
>>> curl.perform()
Write called 16383
pycurl.error: invalid return value for write callback -1 16383
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
pycurl.error: (23, 'Failed writing body (0 != 16383)')

pycurl raises an error if the number returned from your write function isn't equal to the number it thinks it's writing, so returning -1 or raising an exception inside your write function will cause it to raise pycurl.error. Note that returning 'None' is interpreted as 'all bytes written'.

>>> class Writer:
...   def __init__(self):
...     self.count = 0
...   def write(self, data):
...     print "Write called", len(data)
...     return -1
...
>>> curl = pycurl.Curl()
>>> writer = Writer()
>>> curl.setopt(pycurl.WRITEFUNCTION, writer.write)
>>> curl.setopt(pycurl.URL, "file:///some_big_file.txt")
>>> curl.perform()
Write called 16383
pycurl.error: invalid return value for write callback -1 16383
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
pycurl.error: (23, 'Failed writing body (0 != 16383)')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文