Python POST二进制数据

时间:2013-01-16 18:17:29

标签: python rest post urllib2 redmine

我正在编写一些与redmine接口的代码,我需要上传一些文件作为过程的一部分,但我不知道如何从包含二进制文件的python中执行POST请求。

我正在尝试模仿命令here

curl --data-binary "@image.png" -H "Content-Type: application/octet-stream" -X POST -u login:password http://redmine/uploads.xml

在python(下面)中,但它似乎不起作用。我不确定问题是否与编码文件有关,或者标题是否有问题。

import urllib2, os

FilePath = "C:\somefolder\somefile.7z"
FileData = open(FilePath, "rb")
length = os.path.getsize(FilePath)

password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, 'http://redmine/', 'admin', 'admin')
auth_handler = urllib2.HTTPBasicAuthHandler(password_manager)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
request = urllib2.Request( r'http://redmine/uploads.xml', FileData)
request.add_header('Content-Length', '%d' % length)
request.add_header('Content-Type', 'application/octet-stream')
try:
    response = urllib2.urlopen( request)
    print response.read()
except urllib2.HTTPError as e:
    error_message = e.read()
    print error_message

我可以访问服务器,看起来像编码错误:

...
invalid byte sequence in UTF-8
Line: 1
Position: 624
Last 80 unconsumed characters:
7z¼¯'ÅÐз2^Ôøë4g¸R<süðí6kĤª¶!»=}jcdjSPúá-º#»ÄAtD»H7Ê!æ½]j):

(further down)

Started POST "/uploads.xml" for 192.168.0.117 at 2013-01-16 09:57:49 -0800
Processing by AttachmentsController#upload as XML
WARNING: Can't verify CSRF token authenticity
  Current user: anonymous
Filter chain halted as :authorize_global rendered or redirected
Completed 401 Unauthorized in 13ms (ActiveRecord: 3.1ms)

4 个答案:

答案 0 :(得分:49)

基本上你所做的是正确的。查看您链接到的redmine文档,似乎url中点后面的后缀表示发布数据的类型(JSON为.json,XML为.xml),这与您获得的响应一致 - Processing by AttachmentsController#upload as XML。我想也许文档中存在错误并发布二进制数据,您应该尝试使用http://redmine/uploads网址而不是http://redmine/uploads.xml

顺便说一下,我强烈推荐非常好的非常受欢迎的Requests库,用于Python中的http。它比标准库(urllib2)中的要好得多。它也支持身份验证,但为了简洁,我在这里略过了它。

import requests

data = open('./x.png', 'rb').read()
res = requests.post(url='http://httpbin.org/post',
                    data=data,
                    headers={'Content-Type': 'application/octet-stream'})

# let's check if what we sent is what we intended to send...
import json
import base64

assert base64.b64decode(res.json()['data'][len('data:application/octet-stream;base64,'):]) == data

更新

要找出为什么这适用于请求而不是urllib2,我们必须检查发送内容的差异。为了看到这一点,我将流量发送到在端口8888上运行的http代理(Fiddler):

使用请求

import requests

data = 'test data'
res = requests.post(url='http://localhost:8888',
                    data=data,
                    headers={'Content-Type': 'application/octet-stream'})

我们看到了

POST http://localhost:8888/ HTTP/1.1
Host: localhost:8888
Content-Length: 9
Content-Type: application/octet-stream
Accept-Encoding: gzip, deflate, compress
Accept: */*
User-Agent: python-requests/1.0.4 CPython/2.7.3 Windows/Vista

test data

并使用urllib2

import urllib2

data = 'test data'    
req = urllib2.Request('http://localhost:8888', data)
req.add_header('Content-Length', '%d' % len(data))
req.add_header('Content-Type', 'application/octet-stream')
res = urllib2.urlopen(req)

我们得到了

POST http://localhost:8888/ HTTP/1.1
Accept-Encoding: identity
Content-Length: 9
Host: localhost:8888
Content-Type: application/octet-stream
Connection: close
User-Agent: Python-urllib/2.7

test data

我认为没有任何差异可以保证您观察到的不同行为。已经说过,http服务器检查User-Agent标头并根据其值改变行为并不罕见。尝试逐个更改请求发送的标头,使其与urllib2发送的标头相同,并查看它何时停止工作。

答案 1 :(得分:2)

这与格式错误的上传无关。 HTTP错误明确指定401未授权,并告诉您CSRF令牌无效。尝试通过上传发送有效的CSRF令牌。

更多关于csrf令牌的信息:

  

What is a CSRF token ? What is its importance and how does it work?

答案 2 :(得分:0)

你需要添加Content-Disposition标头,像这样smth(尽管我在这里使用了mod-python,但原理应该是相同的):

request.headers_out['Content-Disposition'] = 'attachment; filename=%s' % myfname

答案 3 :(得分:0)

您可以使用unirest,它提供了发布请求的简便方法。 `

import unirest

def callback(response):
 print "code:"+ str(response.code)
 print "******************"
 print "headers:"+ str(response.headers)
 print "******************"
 print "body:"+ str(response.body)
 print "******************"
 print "raw_body:"+ str(response.raw_body)

# consume async post request
def consumePOSTRequestASync():
 params = {'test1':'param1','test2':'param2'}

 # we need to pass a dummy variable which is open method
 # actually unirest does not provide variable to shift between
 # application-x-www-form-urlencoded and
 # multipart/form-data

 params['dummy'] = open('dummy.txt', 'r')
 url = 'http://httpbin.org/post'
 headers = {"Accept": "application/json"}
 # call get service with headers and params
 unirest.post(url, headers = headers,params = params, callback = callback)


# post async request multipart/form-data
consumePOSTRequestASync()

`

您可以在http://stackandqueue.com/?p=57

查看完整示例