gpt4 book ai didi

python : post data within stringIO through poster?

转载 作者:太空宇宙 更新时间:2023-11-03 15:22:13 25 4
gpt4 key购买 nike

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 进行 POST。它运作良好。我对此很满意。

但我想尝试一下。正如您在上面看到的,要发送文件数据,我必须打开一个文件。但是有没有办法不制作一个真实的文件来做到这一点?我们可以使用 STREAM,比如 StringIO,像处理文件一样处理数据,对吧?但是,我对 poster 了解不深。所以,我想知道将 STREAM 与 poster 一起使用的方法。

已添加

实际上,我尝试过 POST 图像数据。我在下面写了这个

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 参数是您传递文件对象的地方。那么,如果您改为传递类文件对象会怎样?

>>> 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'}

看起来成功了。

什么是the documentation说?

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()

但你不必这样做。您应该能够只使用原始字符串。让我们试试看:

>>> 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'}

看看那个,文档是正确的。

关于 python : post data within stringIO through poster?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13354589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com