I’m trying to upload a file using xmlrpc on wordpress. I have done this using php but this time I must use python and something is not working.
To be more specific, the way to do this is by calling the xmlrpc function wp.uploadFile that is explained in the codex here http://codex.wordpress.org/XML-RPC_wp#wp.uploadFile or metaWeblog.newMediaObject. The problem is the encoding. from php I used a class that was doing the dirty work. namely ixr_base64 class that aparently did the trick. In python I tried importing base64lib and using it’s b64encode function, but it did not work.
To be even more specific, here’s the python code I’m using:
import xmlrpclib
import urllib2
import base64
def get_url_content(url):
try:
content = urllib2.urlopen(url)
return content.read()
except:
print 'error!'
file = get_url_content('http://www.legaljuice.com/Fly.jpg')
file = base64.b64decode(file)
server = xmlrpclib.Server('http://localhost/xmlrpc.php')
xarr = ['1', 'admin', 'pass', {'name':'sssaaa.jpg', 'type':'image/jpeg', 'bits':file, 'overwrite':'true'}]
result = server.metaWeblog.newMediaObject(xarr)
print result
It’s not doing the trick. It’s not decoding properly on wordpress’s end. Now, I know it’s not wordpress’s fault because I did this before with php and there are a ton of apps, android, ios, desktop and web that make this fileupload with xmlrpc possible.
From what I’ve researched, python with base64 module provides data encoding and decoding as specified in RFC 3548 while php with base64_encode is using RFC 2045 section 6.8
At this point, I am stuck. I’ve tried everything but nothing works so far. I just get corrupted files on my media page in wordpress.
please help if you can.
ok, the answer lies in the xmlrpclib class.
To send base64 bits to wordpress from python you need to use the xmlrpclib class like so:
then you just add the base64bits variable to the ‘bits’ parameter in your wp.uploadFile xmlrpc request.
to be a little more exact, here’s the complete code in python of how this should be done: