蟒蛇:通过海报发布stringIO内的数据? [英] python : post data within stringIO through poster?
问题描述
params = {'file': open("test.txt", "rb"), 'name': 'upload test'}
datagen, headers = poster.encode.multipart_encode(params)
request = urllib2.Request(upload_url, datagen, headers)
result = urllib2.urlopen(request)
我使用海报库来发布HTTP.它运作良好.我对此感到满意.
I use poster library to POST for HTTP. It works well. I'm satisfied with that.
但是我想尝试一下.如您在上面看到的,要发送文件数据,我必须打开一个文件.但是,有没有办法不制作一个真实的文件呢?我们可以使用STREAM(如StringIO
)来处理文件之类的数据,对吗?但是,我对poster
并不了解.因此,我想知道将STREAM与poster
一起使用的方法.
But I want to try something. As you see above, to send file data, I have to OPEN a file. But is there any way not to make a real file to do that? We can use a STREAM, like StringIO
, to deal with data like a file, right? But, I don't know about poster
deeply. So, I want to know the method to use a STREAM with poster
.
实际上,我尝试过发布图像数据.我在下面写了这个
Actually, I tried to POST image data. I wrote this below
from PyQt4 import QtCore, QtGui
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2, os
register_openers()
app = QtGui.QApplication(sys.argv)
pixmap = QtGui.QPixmap("c:/test_img.png")
byte_array = QtCore.QByteArray()
buffer = QtCore.QBuffer(byte_array)
buffer.open(QtCore.QIODevice.WriteOnly)
pixmap.save(buffer, "PNG")
from cStringIO import StringIO
datagen, headers = multipart_encode({"image": StringIO(str(byte_array.toBase64()))})
request = urllib2.Request(upload_url, datagen, headers)
_rnt = urllib2.urlopen(request)
但是,我收到此错误:
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
_rnt = urllib2.urlopen(request)
File "C:\Python26\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "C:\Python26\lib\urllib2.py", line 397, in open
response = meth(req, response)
File "C:\Python26\lib\urllib2.py", line 510, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python26\lib\urllib2.py", line 435, in error
return self._call_chain(*args)
File "C:\Python26\lib\urllib2.py", line 369, in _call_chain
result = func(*args)
File "C:\Python26\lib\urllib2.py", line 518, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 500: Internal Server Error
推荐答案
file
参数是您传递文件对象的位置.那么,如果您改为传递类似文件的对象会发生什么呢?
The file
param is where you pass the file object. So, what happens if you pass a file-like object instead?
>>> params = {'file': cStringIO.StringIO('upload test data'), 'name': 'upload test'}
>>> datagen, headers = poster.encode.multipart_encode(params)
>>> headers
{'Content-Length': '317', 'Content-Type': 'multipart/form-data; boundary=0c56082b1e134424a918b2b083391467'}
看起来很有效.
文档说什么?>
What does the documentation say?
值可以是字符串参数值,也可以是用作参数值的类似文件的对象.类似于文件的对象必须支持.read()和.fileno()或同时支持.seek()和.tell().
Values are either strings parameter values, or file-like objects to use as the parameter value. The file-like objects must support .read() and either .fileno() or both .seek() and .tell().
因此,可以使用StringIO
对象,因为它们支持seek()
和tell()
.
So, you can use StringIO
objects because they support seek()
and tell()
.
但是您不必这样做.您应该能够只使用原始字符串.让我们尝试一下,看看:
But you don't have to. You should be able to just use the raw string. Let's try it and see:
>>> params = {'file': 'upload test data', 'name': 'upload test'}
>>> datagen, headers = poster.encode.multipart_encode(params)
>>> headers
{'Content-Length': '317', 'Content-Type': 'multipart/form-data; boundary=0c56082b1e134424a918b2b083391467'}
看看,文档是正确的.
这篇关于蟒蛇:通过海报发布stringIO内的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!