I have a python script that posting content to a Wordpress site via the wordpress_xmlrpc
library.
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. When the xml-rpc code is trying to send a post, but the server is not available then the post is lost.
Is there any way to detect 508 errors and handle these?
When a server responds with a HTTP error code, xmlrpc.client
raises a xmlrpc.client.ProtocolError
exception. 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