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)
I use poster library to POST for HTTP. It works well. I’m satisfied with that.
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.
ADDED
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)
But, I receive this error:
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
The
fileparam is where you pass the file object. So, what happens if you pass a file-like object instead?Looks like it worked.
What does the documentation say?
So, you can use
StringIOobjects because they supportseek()andtell().But you don’t have to. You should be able to just use the raw string. Let’s try it and see:
Look at that, the documentation is correct.