pycurl 取消传输并尝试 & 除了

发布于 2024-07-13 18:19:14 字数 187 浏览 10 评论 0原文

如何取消 pycurl 中的传输? 我曾经在 libcurl 中返回 -1 但 pycurl 似乎不喜欢那样(“pycurl.error:写入回调 -1 17 的返回值无效”)返回 0 也不起作用,我得到“错误:(23,'写入主体失败')”。 另外我如何使用 pycurl 进行尝试/除外? 我没有看到任何在线示例,也没有看到网站上的 pycurl 示例

How do i cancel a transfer in pycurl? i use to return -1 in libcurl but pycurl doesnt seem to like that ("pycurl.error: invalid return value for write callback -1 17") return 0 doesnt work either, i get "error: (23, 'Failed writing body')" . Also how do i do a try/except with pycurl? i dont see any examples online nor the pycurl examples from the site

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

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

发布评论

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

评论(1

浪推晚风 2024-07-20 18:19:14

示例代码在这里会有帮助。 从错误消息来看,并在源代码中查找它,您已经设置了一个写入回调。 我认为这是由 CURLOPT_WRITEFUNCTION 配置的,其文档说明:

返回实际的字节数
已搞定。 如果该金额不同
从传递给您的金额
函数,它会向
库,它将中止传输
并返回 CURLE_WRITE_ERROR。

pycurl 包装器代码检查该值是否介于 0 和传递给它的数字之间。 这就是为什么 -1 失败,以及为什么 0 触发 CURLE_WRITE_ERROR,引发“写入正文失败”异常。 pycurl 代码是:

 /* run callback */
 arglist = Py_BuildValue("(s#)", ptr, total_size);
 if (arglist == NULL)
     goto verbose_error;
 result = PyEval_CallObject(cb, arglist);
 Py_DECREF(arglist);
 if (result == NULL)
     goto verbose_error;

 /* handle result */
 if (result == Py_None) {
     ret = total_size;           /* None means success */
 }
 else if (PyInt_Check(result)) {
     long obj_size = PyInt_AsLong(result);
     if (obj_size < 0 || obj_size > total_size) {
         PyErr_Format(ErrorObject, "invalid return value for write callback %ld %ld", (long)obj_size, (long)total_size);
         goto verbose_error;
     }
     ret = (size_t) obj_size;    /* success */
 }
 else if (PyLong_Check(result)) {
      ... identical code for Long ...
 }
 else {
     PyErr_SetString(ErrorObject, "write callback must return int or None");
     goto verbose_error;
 }

我在 pycurl 中没有看到该函数有任何方法支持另一个返回值。 可能还有其他方法,例如设置进度回调,这似乎允许中止。

curl本身的相关代码是:

/* If the previous block of data ended with CR and this block of data is
   just a NL, then the length might be zero */
if(len) {
  wrote = data->set.fwrite_func(ptr, 1, len, data->set.out);
}
else {
  wrote = len;
}

if(CURL_WRITEFUNC_PAUSE == wrote)
  return pausewrite(data, type, ptr, len);

if(wrote != len) {
  failf(data, "Failed writing body (%d != %d)", (int)wrote, (int)len);
  return CURLE_WRITE_ERROR;
}

所以你可以看到pycurl不支持返回curl本身允许的CURL_WRITEFUNC_PAUSE。 还可以看到curl没有办法通过write回调函数支持中止。 你将不得不使用其他东西。

Example code would help here. Judging from the error message, and grepping for it in the source code, you've set up a write callback. This is configured, I think, by CURLOPT_WRITEFUNCTION, and the documentation for that says:

Return the number of bytes actually
taken care of. If that amount differs
from the amount passed to your
function, it'll signal an error to the
library and it will abort the transfer
and return CURLE_WRITE_ERROR.

The pycurl wrapper code checks that the value is between 0 and the number passed to it. That's why -1 failed, and why 0, triggers CURLE_WRITE_ERROR, raises the "failed writing body" exception. The pycurl code is:

 /* run callback */
 arglist = Py_BuildValue("(s#)", ptr, total_size);
 if (arglist == NULL)
     goto verbose_error;
 result = PyEval_CallObject(cb, arglist);
 Py_DECREF(arglist);
 if (result == NULL)
     goto verbose_error;

 /* handle result */
 if (result == Py_None) {
     ret = total_size;           /* None means success */
 }
 else if (PyInt_Check(result)) {
     long obj_size = PyInt_AsLong(result);
     if (obj_size < 0 || obj_size > total_size) {
         PyErr_Format(ErrorObject, "invalid return value for write callback %ld %ld", (long)obj_size, (long)total_size);
         goto verbose_error;
     }
     ret = (size_t) obj_size;    /* success */
 }
 else if (PyLong_Check(result)) {
      ... identical code for Long ...
 }
 else {
     PyErr_SetString(ErrorObject, "write callback must return int or None");
     goto verbose_error;
 }

I don't see any way in pycurl for this function to support another return value. There might be other ways, like setting up a progress callback, which does seem to allow aborts.

The relevant code in curl itself is:

/* If the previous block of data ended with CR and this block of data is
   just a NL, then the length might be zero */
if(len) {
  wrote = data->set.fwrite_func(ptr, 1, len, data->set.out);
}
else {
  wrote = len;
}

if(CURL_WRITEFUNC_PAUSE == wrote)
  return pausewrite(data, type, ptr, len);

if(wrote != len) {
  failf(data, "Failed writing body (%d != %d)", (int)wrote, (int)len);
  return CURLE_WRITE_ERROR;
}

so you can see that pycurl does not support returning the CURL_WRITEFUNC_PAUSE which curl itself allows. You can also see that curl has no way to support aborts through the write callback function. You will have to use something else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文