1. 程式人生 > >解決python傳送multipart/form-data請求上傳檔案的問題

解決python傳送multipart/form-data請求上傳檔案的問題

 

 

#coding=utf8
import requests
from uuid import uuid4
import os

file_name='test'
url=

boundary=uuid4().hex
header={'Content-Type': 'multipart/form-data; boundary={0}'.format(boundary),'charset': 'UTF-8'}
with open(r'C:\test'.decode('utf8'), 'r') as f:
    content=f.readlines()
    print content
    content=''.join(content)
    datas = '--{0}{1}Content-Disposition: form-data; name="file"; filename="{2}"{1}Content-Type: application/octet-stream{1}{1}{3}{1}--{0}--{1}'. \
        format(boundary,os.linesep, file_name, content,boundary)
    print repr(datas)
    print header
    response = requests.post(url,
                             data=datas,
                             headers=header,
                             verify=False)
    print response.status_code,response.text

  結合saltstack,在proxy上執行的 "salt '{}' cp.push {}".format(path, agent_id, file_path)命令,效果更佳