Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

python上传文件问题

python向asp的网站上传文件。。
代码如下:

url="xxx"
        files = {'img_src': ( '1.jpg',filec,"application/octet-stream")}
        resp = req.post(url, files=files,verify=False)
        print resp.content
        

运行后发现asp提示错误500,文件并没有上传,对发送的数据包进行分析,发现
image.png

问题出在箭头所指的地方

image.png
成功上传

image.png
错误500

也就是说成功长传的和错误500的少了两个--

但是我写的python代码默认是没有那两个--的,这个是可以自己定义的嘛?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

我是一初学者,认为可以这样试试:

import requests

url = "xxx"

headers = {
    'Cache-Control': "no-cache",
    'Content-Type':"binary",
    }
    
with open(filename, 'rb') as f:
    r = requests.post(url, data=f, headers=headers)
    print(r.text)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...