简体   繁体   English

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

[英]Is there an xml-rpc object for handling 508 errors?

I have a python script that posting content to a Wordpress site via the wordpress_xmlrpc library . 我有一个python脚本,通过wordpress_xmlrpc将内容发布到Wordpress站点。

This is a piece of my code that sends captured contents to the web site: 这是我的代码片段,它将捕获的内容发送到网站:

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)

My problem is with the server side. 我的问题在于服务器端。 Sometimes the web server will be out of resources and responds with a HTTP 508 error status. 有时Web服务器将缺少资源并以HTTP 508错误状态响应。 When the xml-rpc code is trying to send a post, but the server is not available then the post is lost. 当xml-rpc代码尝试发送帖子但服务器不可用时,帖子就会丢失。

Is there any way to detect 508 errors and handle these? 有没有办法检测508错误并处理这些错误?

When a server responds with a HTTP error code, xmlrpc.client raises a xmlrpc.client.ProtocolError exception . 当服务器响应HTTP错误代码时, xmlrpc.client会引发xmlrpc.client.ProtocolError异常 You can catch that exception and test for the error code. 您可以捕获该异常并测试错误代码。 You could then retry the request, perhaps after waiting a short while: 然后,您可以在等待一段时间后重试该请求:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM