python版本是2.7,使用urllib2将文件推送至微信,在上传文件时,碰到这样一个问题:上传未经压缩的文本文件,可以成功;上传经过压缩的二进制文件,则提示编码错误
代码如下
def upload_tmpfile_old(self,filepath):
try:
#文件存在、文件可读、文件大小小于19M
if not (os.path.isfile(filepath) and os.access(filepath,os.R_OK)
and os.path.getsize(filepath)<19922944):
return -1
boundary = '----------%s' % hex(int(time.time() * 1000))
data = []
data.append('--%s' % boundary)
fr = open(filepath, 'rb')
data.append('Content-Disposition: form-data; name="media"; filename="pickdump"')
data.append('Content-Type: application/octet-stream\r\n\r\n')
data.append(fr.read())
fr.close()
data.append('--%s--\r\n' % boundary)
http_body = '\r\n'.join(data)
print http_body
sendurl = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=" + Token.gettoken() + "&type=file"
req = urllib2.Request(sendurl, data=http_body)
req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)
req.add_header('Content-Length',len(http_body))
print req.headers
self.resp = urllib2.urlopen(req, timeout=5)
print self.resp.__dict__
print self.resp.read()
except:
logger.exception("上传文件出现异常")
raise
if __name__ == '__main__':
uploadfile = wxRequest("WZ", '', '', "file", 1000002)
uploadfile.upload_tmpfile_old("/home/wz/12306.txt.zip")
报错信息如下:
Traceback (most recent call last):
File "/home/wzk/wx2x/wxpush.py", line 264, in
uploadfile.upload_tmpfile_old("/home/wzk/12306.txt.zip")
File "/home/wzk/wx2x/wxpush.py", line 227, in upload_tmpfile_old
self.resp = urllib2.urlopen(req, timeout=5)
File "/usr/lib64/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib64/python2.7/urllib2.py", line 431, in open
response = self._open(req, data)
File "/usr/lib64/python2.7/urllib2.py", line 449, in _open
'_open', req)
File "/usr/lib64/python2.7/urllib2.py", line 409, in _call_chain
result = func(*args)
File "/usr/lib64/python2.7/urllib2.py", line 1242, in https_open
context=self._context)
File "/usr/lib64/python2.7/urllib2.py", line 1196, in do_open
h.request(req.get_method(), req.get_selector(), req.data, headers)
File "/usr/lib64/python2.7/httplib.py", line 1057, in request
self._send_request(method, url, body, headers)
File "/usr/lib64/python2.7/httplib.py", line 1097, in _send_request
self.endheaders(body)
File "/usr/lib64/python2.7/httplib.py", line 1053, in endheaders
self._send_output(message_body)
File "/usr/lib64/python2.7/httplib.py", line 895, in _send_output
msg += message_body
UnicodeDecodeError: 'utf8' codec can't decode byte 0xb6 in position 149: invalid start byte
以前用python3,byte和str有明确的区分。这个功能由于版本问题,只能用python2,结果发现编码各种坑啊。
压缩文件本来就是一个二进制文件,不能被utf-8解码也是正常的,不知道对于这个问题有没有方法解决?
PS 以前用requests的时候,是把文件读成bytes后,直接拼回body里面的,不知道这样做在python2中应该如何写?