是否有用于处理508错误的xml-rpc对象?

问题描述 投票:0回答:1

我有一个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错误并处理这些错误?

python wordpress xml-rpc xmlrpclib
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.