Python metaWeblog upload a file using newMediaObject edit

파일을 읽은 이진데이터를 구조체안에 넣을때 xmlrpclib.Binary()를 써야한다.

import xmlrpclib
import mimetypes

USERID = "userid"
PASSWORD = "password"
BLOGID = 123456 # API KEY
URL = "http://yourblog.com/api"

mw = xmlrpclib.ServerProxy(URL).metaWeblog

# Open file
fp = open("test.jpg", "rb")

# Upload image
url = mw.newMediaObject(BLOGID, USERID, PASSWORD,
                {"name": fp.name, "type": mimetypes.guess_type(fp.name)[0],
                "bits": xmlrpclib.Binary(fp.read())})["url"]

print url

0 comments:

Post a Comment

Newer -> <- Older