wp.​​uploadFile xmlrpc 来自 python 编码 base64

发布于 2025-01-06 03:45:56 字数 1280 浏览 4 评论 0原文

我正在尝试在 WordPress 上使用 xmlrpc 上传文件。我已经使用 php 完成了此操作,但这次我必须使用 python,但有些东西不起作用。

更具体地说,执行此操作的方法是调用 xmlrpc 函数 wp.uploadFile,该函数在 Codex 中进行了解释 http://codex.wordpress.org/XML-RPC_wp#wp.uploadFile 或 metaWeblog.newMediaObject。问题是编码。在 php 中,我使用了一个正在做肮脏工作的类。即 ixr_base64 类显然做到了这一点。在Python中,我尝试导入base64lib并使用它的b64encode函数,但它不起作用。

更具体地说,这是我正在使用的 python 代码:

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

它没有解决问题。 WordPress 端无法正确解码。现在,我知道这不是 WordPress 的错,因为我之前用 php 做过这个,并且有大量的应用程序、android、ios、桌面和 web 使使用 xmlrpc 进行文件上传成为可能。

根据我的研究,带有base64模块的python提供了RFC 3548中指定的数据编码和解码,而带有base64_encode的php则使用RFC 2045第6.8节 在这一点上,我被困住了。我已经尝试了一切,但到目前为止没有任何效果。我刚刚在 WordPress 的媒体页面上收到损坏的文件。

如果可以的话请帮忙。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半寸时光 2025-01-13 03:45:56

好吧,答案就在 xmlrpclib 类中。
要将base64位从python发送到wordpress,您需要使用xmlrpclib类,如下所示:

base64bits = xmlrpclib.Binary(file_content)

然后您只需将base64bits变量添加到wp.uploadFile xmlrpc请求中的“bits”参数即可。

更准确地说,下面是 python 中的完整代码,说明了如何完成此操作:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文