Python - requests 上传文件及报错


上传文件

import requests 
from requests_toolbelt import MultipartEncoder
import shutil 

# pip install  requests-toolbelt  

def tran(file_path, save_path):
    url = 'http://xxxxx'

    m = MultipartEncoder(fields={'file': ('a.pdf', open(file_path, 'rb'), 'application/pdf')} ) 
    print('-- m : ', m) 

    res = requests.post(url, data=m, headers={'Content-Type': m.content_type}, timeout=60*15)
    l = len(res.text.strip())
    # print('-- ret : ', res, file_path, '\n---------\n', res.text)  
    print('-- ret : ', res, l)  


报错 1

ImportError: cannot import name ‘appengine’ from ‘urllib3.contrib’


升级 requests-toolbelt

pip install --upgrade twine requests-toolbelt

根据 stackoverflow
https://stackoverflow.com/questions/76175487/sudden-importerror-cannot-import-name-appengine-from-requests-packages-urlli


报错 2

ConnectionResetError(54, ‘Connection reset by peer’)

如果你的接口只能内网访问,可以检查下你的网络环境(是否用了?)


你可能感兴趣的:(Python,python,开发语言,requests,上传文件,报错,appengine)