解决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)命令,效果更佳

猜你喜欢

转载自www.cnblogs.com/slqt/p/10238019.html