简体   繁体   中英

i can't post from python to my wordpress site by wordpress_xmlrpc i get ExpatError: not well-formed (invalid token): line 1, column 222

I can't post from python to my wordpress site by wordpress_xmlrpc I get ExpatError: not well-formed (invalid token): line 1, column 222

when I am trying to run this code

from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts, NewPost
from wordpress_xmlrpc.methods.users import GetUserInfo

wp = Client('http://mysite.wordpress.com/xmlrpc.php', 'username', 'password')

I got this error ExpatError: not well-formed (invalid token): line 1, column 222

I was replying to you onGitHub , and I figured it would be better for the other subscribers if we moved this off GitHub since it appears to be a WordPress issue.

Per Max, this appears to be specific to your WordPress sites. Since we don't have access to your websites, we can't tell you specifically what's going on. Per our conversation, here are the usual troubleshooting tips:

  1. Disable your plugins and swap to a default theme, then try to connect
  2. If it works then, reenable the theme & check
  3. If it still works, reenable 1 plugin & check again.
  4. Repeat step 3 for all of your plugins.

On GitHub, you mentioned that the issue persists even when you disable all your plugins. Have you tried swapping over to a default them (like WordPress 2017) and trying it again?

I know you don't want to post sensitive info about your website here, but can you let us know a few things?

  • Your WordPress version
  • The plugins on your site
  • What theme you're using

Hopefully this can help us narrow down the issue.

Apparently the problem is that XMLRPC for Wordpress is returning the XML encoded as what Python calls "utf8-sig" instead of "utf8", and xmlrpc.client.Transport doesn't check for an initial BOM, which causes the failure.

I solved the issue monkeypatching Transport.parse_response as follows:

from xmlrpc import client
from xmlrpc.client import GzipDecodedResponse

def parse_response_(self, response):
    # Monkeypatching of xmlrpc
    # xmlrpc.client -> Transport.parse_response doesn't check for starting BOM
    # Code lifted directly from xmlrpc.client except as noted

    # read response data from httpresponse, and parse it
    # Check for new http response object, otherwise it is a file object.
    if hasattr(response, 'getheader'):
        if response.getheader("Content-Encoding", "") == "gzip":
            stream = GzipDecodedResponse(response)
        else:
            stream = response
    else:
        stream = response

    p, u = self.getparser()

    start = True # added
    bom = b'\xef\xbb\xbf\xef\xbb\xbf' # added
    while 1:
        data = stream.read(1024)
        if not data:
            break
        if self.verbose:
            print("body:", repr(data))

        # start addition
        if start:
            if data.startswith(bom):
                data = data[len(bom):]
                start = False
        # end addition

        p.feed(data)

    if stream is not response:
        stream.close()
    p.close()

    return u.close()

client.Transport.parse_response = parse_response_

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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