python2转python3 poster模块求指点迷津
公众号开发文档是python2的内容,现在用python3开发,安装了poster3模块,但是不会用# 报错信息
Traceback (most recent call last):
File "media.py", line 27, in <module>
myMedia.upload(accessToken, filePath, mediaType)
File "media.py", line 15, in upload
postData, postHeaders = poster3.encode.multipart_encode(param)
File "/usr/local/lib/python3.8/dist-packages/poster3/encode.py", line 411, in multipart_encode
headers = get_headers(params, boundary)
File "/usr/local/lib/python3.8/dist-packages/poster3/encode.py", line 311, in get_headers
headers['Content-Length'] = str(get_body_size(params, boundary))
File "/usr/local/lib/python3.8/dist-packages/poster3/encode.py", line 302, in get_body_size
size = sum(p.get_size(boundary) for p in MultipartParam.from_params(params))
File "/usr/local/lib/python3.8/dist-packages/poster3/encode.py", line 176, in from_params
retval.append(cls(name=name, filename=filename,
File "/usr/local/lib/python3.8/dist-packages/poster3/encode.py", line 94, in __init__
self.filename = self.filename.encode("string_escape").\
AttributeError: 'bytes' object has no attribute 'encode'
from basic import Basic
import urllib
import poster3.encode
from poster3.streaminghttp import register_openers
class Media(object):
def __init__(self):
register_openers()
# 上传图片
def upload(self, accessToken, filePath, mediaType):
openFile = open(filePath, "rb")
param = {'media': openFile}
postData, postHeaders = poster3.encode.multipart_encode(param)
postUrl = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s" % (
accessToken, mediaType)
request = urllib.request.Request(postUrl, postData, postHeaders)
urlResp = urllib.request.urlopen(request)
print(urlResp.read())
if __name__ == '__main__':
myMedia = Media()
accessToken = Basic().get_access_token()
filePath = "1213.jpg"# 请按实际填写
mediaType = "image"
myMedia.upload(accessToken, filePath, mediaType)
求大神指点迷津,不胜感激 没看代码,我光看报错,意思是字节类型没有encode,字节只有decode成其他类型的,比如字符串。
要么你方法用错了,要么你方法的参数弄错了 from basic import Basic
import urllib
import poster3
from poster3.streaminghttp import register_openers
class Media(object):
def __init__(self):
register_openers()
# 上传图片
def upload(self, accessToken, filePath, mediaType):
openFile = open(filePath, "rb")
param = {'media': openFile}
postData, postHeaders = poster3.encode.multipart_encode(param)
postUrl = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s" % (
accessToken, mediaType)
request = urllib.request.Request(postUrl, postData, postHeaders)
urlResp = urllib.request.urlopen(request)
print(urlResp.read())
if __name__ == '__main__':
myMedia = Media()
accessToken = Basic().get_access_token()
filePath = "1213.jpg"# 请按实际填写
mediaType = "image"
myMedia.upload(accessToken, filePath, mediaType) 本帖最后由 ufo0033 于 2021-3-6 11:03 编辑
python的库还真是多,
没用过poster3 不过查了下api
https://github.com/EvanDarwin/poster3
from poster import Form
form = Form()
# Add an image object
form.add_file('media', open('upload.jpg', 'rb'))
content, headers = form.encode()
url = '要上传图片的url'
request = urllib.request.Request(url, content, headers)
urlResp = urllib.request.urlopen(request)
我一般用requests上传文件
imports requests
url = '上传地址'
file = open('123.jpg', "rb"))
resp = requests.post(url,files=file)
print(resp.text) ufo0033 发表于 2021-3-6 10:59
python的库还真是多,
没用过poster3 不过查了下api
https://github.com/EvanDarwin/poster3
感谢指点,有request上传的时候报错说要解包的值太多{:1_907:}Traceback (most recent call last):
File "media.py", line 35, in <module>
myMedia.upload(accessToken, filePath, mediaType)
File "media.py", line 26, in upload
urlResp = requests.post(postUrl, files=openFile)
File "/usr/lib/python3/dist-packages/requests/api.py", line 116, in post
return request('post', url, data=data, json=json, **kwargs)
File "/usr/lib/python3/dist-packages/requests/api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 519, in request
prep = self.prepare_request(req)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 452, in prepare_request
p.prepare(
File "/usr/lib/python3/dist-packages/requests/models.py", line 316, in prepare
self.prepare_body(data, files, json)
File "/usr/lib/python3/dist-packages/requests/models.py", line 504, in prepare_body
(body, content_type) = self._encode_files(files, data)
File "/usr/lib/python3/dist-packages/requests/models.py", line 141, in _encode_files
for (k, v) in files:
ValueError: too many values to unpack (expected 2)
第一个解决方法模块还是有问题,因为poster只支持python2,所以安装的是poster3,里边没有Form
Traceback (most recent call last):
File "media.py", line 5, in <module>
from poster import Form
ModuleNotFoundError: No module named 'poster'
Traceback (most recent call last):
File "media.py", line 5, in <module>
from poster3 import Form
ImportError: cannot import name 'Form' from 'poster3' (/usr/local/lib/python3.8/dist-packages/poster3/__init__.py)
非常感谢您的热心 本帖最后由 ufo0033 于 2021-3-6 12:03 编辑
yek2furw 发表于 2021-3-6 11:20
感谢指点,有request上传的时候报错说要解包的值太多Traceback (most ...把file改下
file = {'file': open('report.xls', 'rb')}
页:
[1]