【问题标题】:Is there an xml-rpc object for handling 508 errors?是否有用于处理 508 错误的 xml-rpc 对象?
【发布时间】:2019-04-27 17:13:58
【问题描述】:

我有一个 python 脚本,它通过wordpress_xmlrpc library 将内容发布到 Wordpress 网站。

这是我将捕获的内容发送到网站的一段代码:

from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts, NewPost
from wordpress_xmlrpc.methods.users import GetUserInfo
from wordpress_xmlrpc.methods.posts import EditPost

wp = Client("http://example.com/xmlrpc.php", '#####', '######')
wp.call(GetPosts())
wp.call(GetUserInfo())

post = WordPressPost()
post.title = My_title
post.content = post_content_var
post.terms_names = {'category': ['something']}
post.post_status = "publish"
post.comment_status = "open"
post.id = wp.call(NewPost(post))
print(">>>> The Post ID: ", post.id)

我的问题出在服务器端。有时,Web 服务器会出现资源不足并以 HTTP 508 错误状态进行响应。当 xml-rpc 代码尝试发送帖子,但服务器不可用时,帖子会丢失。

有什么方法可以检测到 508 错误并进行处理?

【问题讨论】:

  • HTTP 508 是与 WebDAV 相关的服务器端错误(您可以在其中处理类似文件的树并指定 Depth: infinity 导致无限循环)。 Wordpress 用它来做什么?
  • 啊,看起来他们将其改写为 resource limit is reached 错误。
  • @MartijnPieters,没错,但是当这个错误出现时,我该如何解决浪费发送的内容?
  • @MartijnPieters,例如在这种情况下,xml-rpc 可以将此错误返回给我,我会写一个尝试,除非达到资源限制并尝试发布一个 agin?
  • 我正在查看 wordpress_xmlrpc 库,它只是 xmlrpc.client 的一个薄包装,并且在 HTTP 错误时会引发 ProtocolError exception。所以你必须抓住那个异常。

标签: python wordpress xml-rpc xmlrpclib


【解决方案1】:

当服务器以 HTTP 错误代码响应时,xmlrpc.client 会引发 xmlrpc.client.ProtocolError exception。您可以捕获该异常并测试错误代码。然后您可以重试该请求,也许在等待片刻之后:

import time
from xmlrpc.client import ProtocolError

while True:
    try:
        post.id = wp.call(NewPost(post))
    except ProtocolError as pe:
        if pe.errcode != 508:
            raise
        print("Wordpress site out of resources, trying again after waiting")
        time.sleep(1)
    else:
        break

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    • 2010-09-08
    • 2017-06-29
    • 1970-01-01
    相关资源
    最近更新 更多