python接口实现上传图片
生活随笔
收集整理的這篇文章主要介紹了
python接口实现上传图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、fiddler抓包查看上傳圖片接口信息
1.連接類型為form-data
2.查看上傳參數,用python寫出對應請求參數
每個參數是filename=后面對應的值,沒有就寫None
3.完整代碼
import requestsdef login(getCookie=True):'''登錄獲取Cookie'''seesion = requests.Session()url = 'http://10.22.1.101:30003/adm/loginNew'payload = {"username":"admin","password":"NW0LxUKlHK2Wq9YQw4h3pJWtTK/leJNLLblqXqlY42XJ5lTRPAhZ9gijKenhI7xuq23d6hyvquRDoItZ7ZmeRO46LXdO5sZp1WwnECJThiuyWrRnLwZDL0DfzxMBUl92r6+ntr+Rha8ZPY7dHgwoIUIdPMGd8/5xT/V4Q4VWOUA=","attribute1":"","attribute2":''}res = seesion.post(url=url,json=payload)if getCookie:Cookie = {}cookie = dict(seesion.cookies)Cookie['Cookie']='OPENAPI-ADM='+cookie['OPENAPI-ADM']return Cookieelse:return res.json()def upload(pathName,pathRoute,pathType):'''上傳圖片'''Cookie = login(getCookie=True)url = 'http://10.22.1.101:30003/adm/fileupload/upload'file = open(pathRoute,'rb')files = {'image':(pathName,file,pathType),'bucketName':(None,'banner'),'type':(None,'image')}# image中寫入的參數為:圖片的名稱(pathName),以二進制形式打開文件(file),文件類型(pathType)headers = Cookieres = requests.post(url=url,files=files,headers=headers)return res.json()if __name__ == '__main__':res = upload('頭像.jpg','F:\截圖\頭像.jpg','image/jpeg')print(res)總結
以上是生活随笔為你收集整理的python接口实现上传图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 管理学入门书籍,德鲁克并不是最好的选择!
- 下一篇: 【亲身体验】华强北的AirPods Pr