- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
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/
我正在使用 python 的 csv.DictReader 但我正在用这样的字符串初始化它: dict_reader = csv.DictReader(StringIO.StringIO(some_s
我已经用谷歌搜索并在 SO 上搜索这些缓冲区模块之间的区别。不过,我还是不是很明白,我觉得我看的一些帖子已经过时了。 在 Python 2.7.11 中,我使用 r = requests.get(ur
在我的模型中,我想通过覆盖保存方法来格式化图像字段 我在我的模型中做到了这一点 from PIL import Image as Img from io import StringIO from dj
我正在使用 Boost 的序列化库序列化大量数据以通过网络发送。 Boost 的序列化库需要一个中间文件来进行序列化和反序列化。传输的数据量使得执行所有这些操作变得很尴尬。将事情保存在内存中会好得多。
StringIO 是我们在从文本中读取 Pandas 数据帧时使用的类似文件的字符串缓冲区对象,例如"How to create a Pandas DataFrame from a string?"
我在使用这两个类时得到了不同的结果。 In [15]: StringIO.StringIO().write(u'\u2222') In [16]: cStringIO.StringIO().write
所以,我关注了this question为了得到一些声音播放 Music21 ,这是代码: from music21 import * import random def main(): # Set
显然,我是 Python 的新手。 我想在下面的代码中使用 StringIO :提取 example.xml import os os.chdir('d:/py/xml/') from lxml i
我只是无法让 StreamHandler 与 StringIO 一起工作以接受来自进程的日志记录。奇怪的是,到 stdout 的流工作得很好。 这是我的代码: from time import sle
我们正在使用一些我们没有源代码的编译Python代码。该代码提示用户输入,我们正在尝试自动化该部分。 基本上要求输入用户名、密码,然后根据特定情况询问一些不同的问题。我不知道编译后的函数是使用raw_
我正在使用以下代码在线检索图像: import Image import urllib2 import cStringIO url = 'http://storage.googleapis.com/b
尝试[ see it running here ]: from sys import stdout, stderr from cStringIO import StringIO from loggin
我在使用 Ruby 的 StringIO 类时观察到一些非常奇怪的行为。 在 irb 控制台中输入以下内容: 2.3.0 :002 > original_string = 'test' => "te
如何在 StringIO 中用另一个字符串替换字符串? - 我听说如果它们的长度相同是可能的。 尝试: from cStringIO import StringIO c = 'can\nhaz\nfo
我似乎得到了不同的输出: from StringIO import * file = open('1.bmp', 'r') print file.read(), '\n' print StringIO
在 Python 中,我有一个文件流,我想将它的一部分复制到 StringIO 中。我希望它尽可能快,副本最少。 但如果我这样做: data = file.read(SIZE) stream = St
我正在寻找一些 StringIO -类似的类,它允许我从我的程序的不同部分同时写入和读取。 从程序的一部分我想将字符写入(追加)到缓冲区,从另一部分我想读取它们。 StringIO 的问题如下: bu
我有 python 3.6。我想从另一个名为“run.py”的 python 文件执行名为“operation.py”的 python 文件。 在 operation.py 中,我执行 from cS
我刚刚开始使用 Django 和 Python,正在尝试构建一个照片应用程序。该脚本正在生成缩略图,我想自己做。不幸的是我不明白 StringIO() 正在做什么。在这种情况下,Python 文档对我
我正在尝试使用将 rml 转换为 pdf, import cStringIO buf = cStringIO.StringIO() rml2pdf.go(rml, outputFileName=buf
我是一名优秀的程序员,十分优秀!