图片从Python或cURL上传到Tumblr? [英] Image uploading from Python or cURL to Tumblr?

查看:159
本文介绍了图片从Python或cURL上传到Tumblr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,涉及从Python上传图片到tumblr。我有运气使用Tumblr的API( http://www.tumblr.com/docs/en / api )在做常规的文本,但图像上传一直给我麻烦。他们的服务器返回的错误消息仅限于告诉我有一个错误上传照片,这是没有帮助。

I'm working on a project that involves uploading an image to tumblr from Python. I've had luck using Tumblr's API( http://www.tumblr.com/docs/en/api ) in doing regular text-posts, but image uploads have been giving me trouble. The error messages their server returns have been limited to just telling me that there was an "Error Uploading Photo", which has been less than helpful.

由于他们的API似乎基于使用标准HTTP POST操作,我知道有一种方法来做到这一点。不幸的是,我没有取得任何进展了几天,我决定诉诸于打扰你们。

Since their API seems to be based in using standard HTTP POST operations, I know that there has to be a way to do this. Unfortunately, I haven't made any progress for a couple of days, and I've decided to resort to bothering you guys about it.

我试过使用curl,和python的库:httplib,urllib,urllib2和一个名为urllib2_file的第三方库( http:// fabien .seisen.org / python / urllib2_file / )。我很失望,我没有让他们工作 - 但我愿意尝试其他额外的终端应用程序,你可以想出来。

I have tried using curl, and python's libraries: httplib, urllib, urllib2, and a third party library called urllib2_file (http://fabien.seisen.org/python/urllib2_file/). I'm frustrated that I haven't gotten them to work-- but I'm willing to try other additional terminal apps you can come up with.

每种方法精简与简单的文本帖子,但每个人似乎没有正确完成照片上传。

Each method works fine with simple text posts, but each one of them doesn't seem to get the photo uploading done properly.

这是我的语法与urllib2_file。因为urllib2不支持'multipart / form-data'方法来上传数据,我使用urllib2_file来添加这个功能,但是我还是不能使用它。 tumblr api说他们的服务器接受multipart / form-data以及用于上传文件的'normal post'方法。

Here's my syntax for doing it with urllib2_file. Since urllib2 doesn't support 'multipart/form-data' methods for uploading data, I'm using urllib2_file to add that functionality-- but I haven't been able to get it to work. The tumblr api says that their servers accept multipart/form-data as well as the 'normal post' method for uploading files. I'd be happy if either worked.

import urllib, urllib2, urllib2_file

url = "http://www.tumblr.com/api/write"
values1 = {     'email':'EMAIL',
                'password':'PASSWORD',
                'type':'regular',
                'title':'Pythons urllib2',
                'body':'its pretty nice.  Not sure how to make it upload stuff yet, though. Still getting some "error uploading photo" errors...  So unhelpful.'}
values2 = {     'email':'EMAIL',
                'password':'PASSWORD',
                'type':'photo',
                'data': open('../data/media/pics/2009/05-14/100_1167.JPG'),
                'caption':'Caption'}
data = urllib.urlencode(values2)
print "just before defining the request"
req = urllib2.Request(url,data)
print "just before doing the urlopen."
#response = urllib2.urlopen(req)
try:
        response = urllib2.urlopen(req)
except urllib2.URLError, e:
        print e.code
        print e.read()
print "figure out how to handle .read() properly"
#the_page = response.read()
#print the_page
print "done"

这将是理想的方式,如果它工作,因为使用字典定义字段非常容易,我可以让它看起来更清洁在未来。

This would be the ideal way if it worked since using dictionaries to define the fields is really easy and I could make it look much cleaner in the future.

任何建议如何解决可能会出问题将不胜感激。在这一点上,我不知道如何学习什么可能会出错。我希望我有注意跨度的http RFC。

Any advice on how to troubleshoot what could be going wrong would be appreciated. At this point I don't know how to learn what could be going wrong. I wish I had the attention span for the http RFC.

我一直在考虑嗅探我的电脑之间的数据包服务器 - 但反向工程HTTP可能过分。

I've been considering sniffing the packets between my computer at the server-- but reverse-engineering HTTP might be overkill.

谢谢!

推荐答案

                 'data': open('../data/media/pics/2009/05-14/100_1167.JPG'),

看起来你只是传递一个文件对象..添加 .read()

Looks like you're just passing in a file object .. add a .read() there

这篇关于图片从Python或cURL上传到Tumblr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆