Python WordPress XML-RPC and Transport

2019-07-24 07:34发布

I'm using python-wordpress-xmlrpc to create new posts on WordPress:

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

from xmlrpc.client import Transport

class SpecialTransport(Transport):
    user_agent = 'Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31'

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

post = WordPressPost()
post.title = 'My new title'
post.content = 'This is the body of my new post.'

wp.call(NewPost(post))

This has been working fine with WordPress sites using http or https. But now I've come across a WordPress site on https where this does NOT work unless I remove the transport parameter i.e.

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

What's the best way in my code to check if I need to include the transport parameter or not?

0条回答
登录 后发表回答