Python3 post请求上传文件

安装requests模块

进入python安装根目录下面的Scripts目录执行pip install requests命令,等待安装成功之后,执行import requests命令来查看是否安装成功

post方法参数

requests.post(url,data=data,header=header,files=files)
  • data设置body数据
  • header设置请求头
  • files设置上传的文件

示例代码

该WebService接口使用的是multipart/form-data协议

import requests
import hashlib
import time

url = "http://test"
clientId = "1111"
clientKey = "2222"
timestap = (str)(int(round(time.time()*1000)))

clientSecret = hashlib.sha256(clientId.encode("utf-8") + clientKey.encode("utf-8") + timestap.encode("utf-8"))

header = {'clientId':clientId,'timestap':timestap,'clientSecret':clientSecret}


files = {'apk':open('D:\\test.apk','rb')}

data = {'enctype':'multipart/form-data','name':'wang'}

reponse = requests.post(url,data=data,header=header,files=files)

text = reponse.text

print (text)

参考资料