亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python?Flask?上傳文件測(cè)試示例

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

 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)

上傳測(cè)試

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

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

上傳臨時(shí)文件

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

使用 tempfile

tempfile 會(huì)在系統(tǒng)的臨時(shí)目錄中創(chuàng)建文件,使用完了之后會(huì)自動(dò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,可以直接在內(nèi)存中完成整個(gè)過程。

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 的實(shí)例沒有文件名,需要自己手動(dòng)設(shè)置,不設(shè)置 POST 過去那邊的文件名會(huì)是 'file'
files = {'file': temp}
r = requests.post(url, files=files)

其他

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

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 上傳文件測(cè)試示例的詳細(xì)內(nèi)容,更多關(guān)于Python Flask上傳文件測(cè)試的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python冒泡排序簡(jiǎn)單實(shí)現(xiàn)方法

    python冒泡排序簡(jiǎn)單實(shí)現(xiàn)方法

    這篇文章主要介紹了python冒泡排序簡(jiǎn)單實(shí)現(xiàn)方法,實(shí)例分析了Python冒泡排序的簡(jiǎn)單實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • 詳解 Python 讀寫XML文件的實(shí)例

    詳解 Python 讀寫XML文件的實(shí)例

    這篇文章主要介紹了詳解 Python 讀寫XML文件的實(shí)例的相關(guān)資料,Python 生成XML文件和Python 讀取XML 的實(shí)例,需要的朋友可以參考下
    2017-08-08
  • 跟老齊學(xué)Python之有點(diǎn)簡(jiǎn)約的元組

    跟老齊學(xué)Python之有點(diǎn)簡(jiǎn)約的元組

    元組和列表十分類似,但是元組是不可變的.也就是說你不能修改元組。元組通過圓括號(hào)中用逗號(hào)分割的項(xiàng)目定義。元組通常用在使語句或用戶定義的函數(shù)能夠安全地采用一組值的時(shí)候,即被使用的元組的值不會(huì)改變。
    2014-09-09
  • Python學(xué)習(xí)筆記之迭代器和生成器用法實(shí)例詳解

    Python學(xué)習(xí)筆記之迭代器和生成器用法實(shí)例詳解

    這篇文章主要介紹了Python學(xué)習(xí)筆記之迭代器和生成器用法,結(jié)合實(shí)例形式詳細(xì)分析了Python迭代器與生成器的功能、原理、定義及使用方法,需要的朋友可以參考下
    2019-08-08
  • Python自動(dòng)發(fā)送郵件的方法實(shí)例總結(jié)

    Python自動(dòng)發(fā)送郵件的方法實(shí)例總結(jié)

    這篇文章主要介紹了Python自動(dòng)發(fā)送郵件的方法,結(jié)合實(shí)例形式總結(jié)分析了Python使用smtplib和email模塊發(fā)送郵件的相關(guān)使用技巧與操作注意事項(xiàng),需要的朋友可以參考下
    2018-12-12
  • python導(dǎo)出mysql指定binlog文件實(shí)現(xiàn)demo

    python導(dǎo)出mysql指定binlog文件實(shí)現(xiàn)demo

    這篇文章主要介紹了python導(dǎo)出mysql指定binlog文件實(shí)現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • python中的關(guān)鍵字參數(shù)*args和**kwargs詳解

    python中的關(guān)鍵字參數(shù)*args和**kwargs詳解

    這篇文章主要介紹了python中的關(guān)鍵字參數(shù)*args和**kwargs詳解,在定義類或函數(shù)時(shí),有時(shí)候會(huì)用到*args和**kwargs,前者叫位置參數(shù),后者叫關(guān)鍵字參數(shù),需要的朋友可以參考下
    2023-11-11
  • Python 用matplotlib畫以時(shí)間日期為x軸的圖像

    Python 用matplotlib畫以時(shí)間日期為x軸的圖像

    這篇文章主要介紹了Python 用matplotlib畫以時(shí)間日期為x軸的圖像,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • 使用python繪制二維圖形示例

    使用python繪制二維圖形示例

    今天小編就為大家分享一篇使用python繪制二維圖形示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • python-numpy-指數(shù)分布實(shí)例詳解

    python-numpy-指數(shù)分布實(shí)例詳解

    今天小編就為大家分享一篇python-numpy-指數(shù)分布實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12

最新評(píng)論