wp.uploadFile xmlrpc from python encode base64

冷暖自知 提交于 2019-12-05 06:59:51

问题


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.


回答1:


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:

base64bits = xmlrpclib.Binary(file_content)

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:

import xmlrpclib
import urllib2
from datetime import date
import time

def get_url_content(url):
        try:
            content = urllib2.urlopen(url)
            return content.read()
        except:
            print 'error! NOOOOOO!!!'
file_url = 'http://the path to your picture'
extension = file_url.split(".")
leng = extension.__len__()
extension = extension[leng-1]
if (extension=='jpg'):
    xfileType = 'image/jpeg'
elif(extension=='png'):
    xfileType='image/png'
elif(extension=='bmp'):
    xfileType = 'image/bmp'

file = get_url_content(file_url)
file = xmlrpclib.Binary(file)
server = xmlrpclib.Server('http://website.com/xmlrpc.php')
filename = str(date.today())+str(time.strftime('%H:%M:%S'))
mediarray = {'name':filename+'.'+extension, 
             'type':xfileType, 
             'bits':file, 
             'overwrite':'false'}
xarr = ['1', 'USERHERE', 'PASSWORDHERE', mediarray]
result = server.wp.uploadFile(xarr)
print result


来源:https://stackoverflow.com/questions/9301446/wp-uploadfile-xmlrpc-from-python-encode-base64

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!