最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python?Flask?上傳文件測試示例

 更新時間:2022年07月15日 08:37:28   作者:常偉佳  
這篇文章主要為大家介紹了Python?Flask?上傳文件測試的方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

 Flask file upload代碼

import os
from flask import Flask, request, redirect, url_for, send_from_directory
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = '/tmp/flask-upload-test/'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            print 'no file'
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit a empty part without filename
        if file.filename == '':
            print 'no filename'
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('uploaded_file',
                                    filename=filename))
    return '''
    <!doctype html>
    <title>Upload new File</title>
    <h1>Upload new File</h1>
    <form action="" method=post enctype=multipart/form-data>
      <p><input type=file name=file>
         <input type=submit value=Upload>
    </form>
    '''
@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'],
                               filename)
if __name__ == "__main__":
    app.run(debug=True)

上傳測試

$ curl -F 'file=@"foo.png";filename="bar.png"' 127.0.0.1:5000

注意:使用上傳文件功能的時候使用 POST form-data,參數(shù)名就是參數(shù)名(一般會提前約定好,而不是變化的文件名),參數(shù)的值是一個文件(這個文件正常是有文件名的)。

上傳臨時文件

有時候腳本生成了要上傳的文件,但并沒有留在本地的需求,所以使用臨時文件的方式,生成成功了就直接上傳。

使用 tempfile

tempfile 會在系統(tǒng)的臨時目錄中創(chuàng)建文件,使用完了之后會自動刪除。

import requests
import tempfile
url = 'http://127.0.0.1:5000'
temp = tempfile.NamedTemporaryFile(mode='w+', suffix='.txt')
try:
    temp.write('Hello Temp!')
    temp.seek(0)
    files = {'file': temp}
    r = requests.post(url, files=files, proxies=proxies)
finally:
    # Automatically cleans up the file
    temp.close()

使用 StringIO

或者直接使用 StringIO,可以直接在內存中完成整個過程。

import requests
from StringIO import StringIO
url = 'http://127.0.0.1:5000'
temp = StringIO()
temp.write('Hello Temp!')
temp.seek(0)
temp.name = 'hello-temp.txt'    # StringIO 的實例沒有文件名,需要自己手動設置,不設置 POST 過去那邊的文件名會是 'file'
files = {'file': temp}
r = requests.post(url, files=files)

其他

補上發(fā)現(xiàn)的一段可以上傳多個同名附件的代碼:

files = [
    ("attachments", (display_filename_1, open(filename1, 'rb'),'application/octet-stream')),
    ("attachments", (display_filename_2, open(filename2, 'rb'),'application/octet-stream'))
]
r = requests.post(url, files=files, data=params)

參考

10.6. tempfile — Generate temporary files and directories — Python 2.7.12 documentation

以上就是Python Flask 上傳文件測試示例的詳細內容,更多關于Python Flask上傳文件測試的資料請關注腳本之家其它相關文章!

相關文章

最新評論

新沂市| 齐齐哈尔市| 历史| 南木林县| 龙口市| 菏泽市| 长春市| 马鞍山市| 南靖县| 缙云县| 绥德县| 卢湾区| 天台县| 兴义市| 开远市| 鄂托克前旗| 图木舒克市| 永嘉县| 陵川县| 阿巴嘎旗| 马鞍山市| 昂仁县| 阿鲁科尔沁旗| 西充县| 岗巴县| 德阳市| 浑源县| 临洮县| 义马市| 苗栗县| 舟山市| 佳木斯市| 顺平县| 仪征市| 巴林右旗| 镶黄旗| 潍坊市| 民权县| 衢州市| 望江县| 富裕县|