吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1745|回复: 5
收起左侧

[求助] python2转python3 poster模块求指点迷津

[复制链接]
yek2furw 发表于 2021-3-6 10:15
公众号开发文档是python2的内容,现在用python3开发,安装了poster3模块,但是不会用
[Python] 纯文本查看 复制代码
# 报错信息
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'



[Python] 纯文本查看 复制代码
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)



求大神指点迷津,不胜感激

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Ercilan 发表于 2021-3-6 10:43
没看代码,我光看报错,意思是字节类型没有encode,字节只有decode成其他类型的,比如字符串。
要么你方法用错了,要么你方法的参数弄错了
kof21411 发表于 2021-3-6 10:50
[Python] 纯文本查看 复制代码
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 10:59
本帖最后由 ufo0033 于 2021-3-6 11:03 编辑

python的库还真是多,
没用过poster3 不过查了下api
https://github.com/EvanDarwin/poster3
  
[Python] 纯文本查看 复制代码
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上传文件
[Python] 纯文本查看 复制代码
imports requests

url = '上传地址'
file = open('123.jpg', "rb"))
resp = requests.post(url,files=file)
print(resp.text)

免费评分

参与人数 1热心值 +1 收起 理由
yek2furw + 1 谢谢@Thanks!

查看全部评分

 楼主| yek2furw 发表于 2021-3-6 11:20
ufo0033 发表于 2021-3-6 10:59
python的库还真是多,
没用过poster3 不过查了下api
https://github.com/EvanDarwin/poster3

感谢指点,有request上传的时候报错说要解包的值太多
[Python] 纯文本查看 复制代码
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
[Python] 纯文本查看 复制代码
Traceback (most recent call last):
  File "media.py", line 5, in <module>
    from poster import Form
ModuleNotFoundError: No module named 'poster'


[Python] 纯文本查看 复制代码
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:00
本帖最后由 ufo0033 于 2021-3-6 12:03 编辑
yek2furw 发表于 2021-3-6 11:20
感谢指点,有request上传的时候报错说要解包的值太多[mw_shl_code=python,true]Traceback (most ...
把file改下
file = {'file': open('report.xls', 'rb')}

免费评分

参与人数 1吾爱币 +1 收起 理由
yek2furw + 1 已经处理,感谢您对吾爱破解论坛的支持!

查看全部评分

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 07:30

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表