1 Server端代碼
import os
from flask import Flask, request
from werkzeug.utils import secure_filename
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'E://recv//'
@app.route('/seenton/monitor/alarmImage', methods=['POST'])
def upload_file():
head = request.headers
print(f' recvvvvvvv head= [{head}]')
content_type = request.content_type
print(f' recvvvvvvv content_type= [{content_type}]')
boundary = request.content_type.split(';')[1].split('=')[1]
print(f' recvvvvvvv boundary= [{boundary}]')
content_len = request.content_length
if 'file' not in request.files:
return 'No file part'
file = request.files['file']
if file.filename == '':
return 'No selected file'
print(f' file.filename= [{file.filename}]')
if file:
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return 'File saved successfully'
if __name__ == '__main__':
app.run(host='192.168.1.173', port=18098)
2 客戶端截圖
PostMan生成Python 代碼:文章來源地址http://www.zghlxwxcb.cn/news/detail-723595.html
import requests
url = "http://192.168.1.16:18098/seenton/monitor/alarmImage"
payload = {}
files=[
('file',('測(cè)溫點(diǎn)位1-58@stator#20220226-221220#stat.jpg',open('/D:/Desktop/20220226-221220-帶雙光融合效果/測(cè)溫點(diǎn)位1-58@stator#20220226-221220#stat.jpg','rb'),'image/jpeg'))
]
headers = {}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)
3 代碼說明
- 代碼基本功能:實(shí)現(xiàn)將客戶端發(fā)送的文件轉(zhuǎn)發(fā)到本地。
- 代碼中的打印:為了調(diào)試方便代碼中增加了一些關(guān)于boudary的打印。
- 代碼中的自定義字段:代碼中的 ‘file’ 就是postman客戶端請(qǐng)求中的文件名稱。
文章來源:http://www.zghlxwxcb.cn/news/detail-723595.html
到了這里,關(guān)于Python 實(shí)現(xiàn)http server接收mutipart/form-data文件 方法1的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!