是否有用于处理 508 错误的 xml-rpc 对象?
Is there an xml-rpc object for handling 508 errors?
我有一个 python 脚本,可以通过 wordpress_xmlrpc
library 将内容 post 发送到 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 代码尝试发送 post,但服务器不可用时 post 丢失。
有什么方法可以检测并处理 508 错误吗?
当服务器响应 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
我有一个 python 脚本,可以通过 wordpress_xmlrpc
library 将内容 post 发送到 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 代码尝试发送 post,但服务器不可用时 post 丢失。
有什么方法可以检测并处理 508 错误吗?
当服务器响应 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