Traceback:
...
resp = pool.request(method, page, fields = fields)
File "/usr/lib/python2.7/site-packages/urllib3-dev-py2.7.egg/urllib3/request.py", line 79, in request
**urlopen_kw)
File "/usr/lib/python2.7/site-packages/urllib3-dev-py2.7.egg/urllib3/request.py", line 139, in request_encode_body
**urlopen_kw)
File "/usr/lib/python2.7/site-packages/urllib3-dev-py2.7.egg/urllib3/connectionpool.py", line 415, in urlopen
body=body, headers=headers)
File "/usr/lib/python2.7/site-packages/urllib3-dev-py2.7.egg/urllib3/connectionpool.py", line 267, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/lib64/python2.7/httplib.py", line 958, in request
self._send_request(method, url, body, headers)
File "/usr/lib64/python2.7/httplib.py", line 992, in _send_request
self.endheaders(body)
File "/usr/lib64/python2.7/httplib.py", line 954, in endheaders
self._send_output(message_body)
File "/usr/lib64/python2.7/httplib.py", line 812, in _send_output
msg += message_body
UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 181: ordinal not in range(128)
urllib3
filepost.py:
def encode_multipart_formdata(fields, boundary=None):
...
body = BytesIO()
...
return body.getvalue(), content_type
request.py:
def request(self, method, url, fields=None, headers=None, **urlopen_kw):
...
else:
return self.request_encode_body(method, url, fields=fields,
headers=headers,
**urlopen_kw)
def request_encode_body(self, method, url, fields=None, headers=None,
encode_multipart=True, multipart_boundary=None,
**urlopen_kw):
...
if encode_multipart:
body, content_type = encode_multipart_formdata(fields or {},
boundary=multipart_boundary)
...
headers = headers or {}
headers.update({'Content-Type': content_type})
return self.urlopen(method, url, body=body, headers=headers,
**urlopen_kw)
HTTPLIB
httplib.py:
def _send_request(self, method, url, body, headers):
...
if body and ('content-length' not in header_names):
self._set_content_length(body)
...
def _set_content_length(self, body):
thelen = None
try:
thelen = str(len(body))
except TypeError, te:
try:
thelen = str(os.fstat(body.fileno()).st_size)
except (AttributeError, OSError):
if self.debuglevel > 0: print "Cannot stat!!"
if thelen is not None:
self.putheader('Content-Length', thelen)
...
def _send_output(self, message_body=None):
...
if isinstance(message_body, str):
msg += message_body
message_body = None
self.send(msg)
if message_body is not None:
self.send(message_body)
...
def send(self, data):
...
blocksize = 8192
if hasattr(data,'read') and not isinstance(data, array):
if self.debuglevel > 0: print "sendIng a read()able"
datablock = data.read(blocksize)
while datablock:
self.sock.sendall(datablock)
datablock = data.read(blocksize)
else:
self.sock.sendall(data)
My script.py:
from io import BytesIO
data = BytesIO()
print type(data.getvalue()
$./ script.py
< type 'str' >
:
from urllib3 import HTTPConnectionPool, filepost
from io import BytesIO, SEEK_SET
pool = HTTPConnectionPool('web')
archive_name = '/var/archives/mock.tar.gz'
fields = {'uploadedfile' : (archive_name, open(archive_name).read())}
value, content_type = filepost.encode_multipart_formdata(fields)
headers = {'Content-Type' : content_type, 'Content-Length' : len(value)}
data = BytesIO()
data.write(value)
data.seek(0, SEEK_SET)
resp = pool.urlopen('POST', '/upload/uploader.php', data, headers)
print resp.status
$./Upload.py
200
/var/www/upload # ls
mock.tar.gz upload.html upload.php