0

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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • HTTP 508 is a server-side error related to WebDAV (where you can process a file-like tree and specifying `Depth: infinity` led to an infinite loop). What is Wordpress using this for? – Martijn Pieters Apr 27 '19 at 17:17
  • Ah, looks like they recast this as a [resource limit is reached](https://stackoverflow.com/questions/20040307/how-to-fix-the-508-resource-limit-is-reached-error-in-wordpress) error. – Martijn Pieters Apr 27 '19 at 17:18
  • @MartijnPieters, thats right but how can i solve wasting sent contents , whene this error rised ? –  Apr 27 '19 at 17:20
  • @MartijnPieters, for exaple on this situation xml-rpc can return this error to me and i write a try and except whene resource limit is reached will be rised and trying post a agin ? –  Apr 27 '19 at 17:23
  • I'm looking at the wordpress_xmlrpc library, it's just a thin wrapper around [`xmlrpc.client`](https://docs.python.org/3/library/xmlrpc.client.html#module-xmlrpc.client), and on HTTP errors a [`ProtocolError` exception](https://docs.python.org/3/library/xmlrpc.client.html#xmlrpc.client.ProtocolError) is raised. So you'd have to catch that exception. – Martijn Pieters Apr 27 '19 at 17:25

1 Answers1

0

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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343