"AAaarg" ! Please HeLp !!!
“AAaarg”!请帮助! ! !
Here is what I am trying to do ...
这就是我想做的……
I have a Django site site1
, which needs to access the API of another service site2
. However, in order to do that, site1
needs to use its own login credentials and stuff ...
我有一个Django站点site1,它需要访问另一个服务site2的API。然而,为了做到这一点,site1需要使用它自己的登录凭证和其他东西……
Therefore I have written a small Django app, which duplicates urls of site2
, but under the hood, uses httplib2
to transmit the request almost identically (just authenticating and stuff). It works great in most of cases and it actually used to work great before for all cases (I don't really know what broke it, possibly the update Python 2.6 -> 2.7).
因此,我编写了一个小型Django应用程序,它复制了site2的url,但是在后台,使用httplib2几乎相同地传输请求(只是进行身份验证之类的)。它在大多数情况下都很好用,实际上以前在所有情况下都很好用(我不知道是什么破坏了它,可能是升级版Python 2.6 -> 2.7)。
In order to transmit the POST/PUT data as is, I get it with :
为了传输POST/PUT数据,我用:
post_data = request.raw_post_data
And then send it with httplib2 :
然后用httplib2发送:
response, content = c.request(
url,
method,
post_data,
headers=headers,
)
Problem occurs when posting multipart-data, containing binary data such as an image. httplib
(on top of which httplib2
is built) when building the request string, tries to concatenate my post_data
with some generated headers and stuff. And it seems like request.raw_post_data
is string
type while the generated stuff are unicode
. Therefore, it tries to decode my post_data
(which contains binary data) and freaks out !!!
当发布包含二进制数据(如图像)的多部分数据时,会出现问题。httplib(在httplib2之上)在构建请求字符串时,尝试将我的post_data与一些生成的header和其他东西连接起来。这似乎是一种要求。raw_post_data是字符串类型,而生成的内容是unicode。因此,它尝试解码我的post_data(其中包含二进制数据)并崩溃!
c.f. httplib
line 807 :
c.f. httplib第807行:
if isinstance(message_body, str):
msg += message_body
Here is an extract of message_body
(gotten with request.raw_post_data
) :
下面是message_body的摘录(通过request.raw_post_data获得):
'-----------------------------697585321193462802080194682\r\nContent-Disposition: form-data; name="_method"\r\n\r\nPUT\r\n-----------------------------697585321193462802080194682\r\nContent-Disposition: form-data; name="jpegPhoto"; filename="crap.jpg"\r\nContent-Type: image/jpeg\r\n\r\n\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00H\x00H\x00\x00\xff\xfe\x00\x13Created with GIMP\xff\xdb\x00C\x00\x05\x03\x04\x04\x04\x03\x05\x04\x04\x04
Here is the content of msg
:
msg的内容如下:
u'POST /user/spiq/?username=spiq HTTP/1.1\r\nContent-Length: 40307\r\naccept-language: en-us,en;q=0.5\r\naccept-encoding: gzip, deflate\r\nhost: localhost:8000\r\naccept: application/json\r\nuser-agent: Mozilla/5.0 (X11; Linux x86_64; rv:5.0) Gecko/20100101 Firefox/5.0\r\naccept-charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\nconnection: keep-alive\r\nreferer: \r\ncookie: csrftoken=d9a3e014e5e366ee435b27ae7fc122af; sessionid=d5492a8d640e346b8ca56fa87e5cc439\r\ncontent-type: multipart/form-data\r\n\r\n'
So basically it is doomed ...
所以基本上这是注定的…
Any idea how should I proceed ? Can I turn my post_data
to unicode
without decoding it ?
你知道我该怎么做吗?我可以将post_data转换为unicode而不解码它吗?
1 个解决方案
#1
1
From this: http://bugs.python.org/issue11898#msg138059 it looks like you might be okay if you ensure that the url
argument is coerced into a str
.
从这里:http://bugs.python.org/e11898 #msg138059看起来,如果您确保url参数被强制转换为一个str,您可能会感觉良好。
#1
1
From this: http://bugs.python.org/issue11898#msg138059 it looks like you might be okay if you ensure that the url
argument is coerced into a str
.
从这里:http://bugs.python.org/e11898 #msg138059看起来,如果您确保url参数被强制转换为一个str,您可能会感觉良好。