簡體   English   中英

如何使用 python 請求將圖像與表單數據一起發送?

[英]How to send an image together with form data using python requests?

我正在嘗試使用 requests python 創建一個發布請求以及提交表單數據。 這是 HTML 形式:

<form action="" method="POST" enctype="multipart/form-data">
    <div class="dropzone">
        <div class="content">
            <img src="https://100dayscss.com/codepen/upload.svg" class="upload">
            <span class="filename"></span>
            <input type="file" class="input" name="image">
        </div>
    </div>
    <input class="upload-btn" type="submit" value="Upload Image" name="submit">
</form>

這是使用 BurpSuite 的原始請求(有效)的圖像: 工作要求

然后在 python 中提出這個請求:

payload_img = make_payload_img()    # Creates an image and returns the name of it
post_url = f"{target}/upload.php"   # Target is the ip of who we are sending the request to
files = {
    'image': (payload_img, open(payload_img, 'rb'), "image/png"),
}
headers = {"Cache-Control": "max-age=0", "Upgrade-Insecure-Requests": "1", "Origin": "http://10.10.10.185", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", "Referer": "http://10.10.10.185/upload.php"}
# The proxies are just to intercept the request on BurpSuite
proxies = {
    "http": "http://127.0.0.1:8080",
    "https": "http://127.0.0.1:8080",
}

upload_file = s.post(post_url,files=files,headers=headers,proxies=proxies)

但是,在 BurpSuite 上截獲的請求中缺少Content-Disposition: form-data; name="submit" Content-Disposition: form-data; name="submit"在最后:

不工作

是否可以手動添加它或 python 請求多部分不允許您這樣做?

您不需要將其添加到python-requests中。這不是它的請求 header 。 form-data代表 enctype 方式。除了form-data ,還有form-urlencodedtext/plain (不太常見)。 獲取更多信息在 wiki 上。

Content-Disposition :因為您使用了files=file 它會通過form-data正常發送。

name="image" :表單中的名稱。(在您的情況下,它們是圖像)。

name="submit" : 這通常表示表單的提交按鈕。當你點擊頁面上的按鈕時,它會采取這個。(通常你不需要添加它)。

如果你真的喜歡以第一種方式發布它:

files = {
    'image': (payload_img, open(payload_img, 'rb'), "image/png"),
}
data = {
    "submit": "Upload Image".
}

requests.post(url, files=files, data=data, headers=headers .... )
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM