D:\software\Python2..."/>
溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

IIS部署flask之實現(xiàn)文件上傳功能

發(fā)布時間:2020-07-30 16:06:35 來源:網(wǎng)絡 閱讀:1545 作者:_wangpan 欄目:建站服務器

1、環(huán)境

windows 7 x64

IIS 6

python 2.7.9

wfastcgi-3.0.0

flask-0.12.2

2、安裝wfastcgi,并啟動wfastcgi

pip install wfastcgi

C:\Users\wangpan>D:\software\Python27\Scripts\wfastcgi-enable.exe
已經(jīng)在配置提交路徑“MACHINE/WEBROOT/APPHOST”向“MACHINE/WEBROOT/APPHOST”的“system.webServer/fastCgi”節(jié)應用了配置更改
“d:\software\python27\python.exe|d:\software\python27\lib\site-packages\wfastcgi.pyc” can now be used as a FastCGI script processor

3、安裝flask

pip install flask

4、打開windows功能,安裝IIS,啟用CGI

IIS部署flask之實現(xiàn)文件上傳功能

5、安裝URL重寫

IIS 需要安裝 URL 重寫組件,這個可以通過Microsoft Web Platform Installer來安裝。下載Microsoft Web Platform Installer后運行,搜索URL,安裝URL重寫工具。

IIS部署flask之實現(xiàn)文件上傳功能

6、配置IIS

6.1 添加網(wǎng)站,根目錄是d:\data\mysite\upload

IIS部署flask之實現(xiàn)文件上傳功能

6.2 d:\data\mysite\upload目錄結(jié)構(gòu)

upload

–static上傳目錄的靜態(tài)文件目錄

–upload.py上傳文件程序

–web.config配置文件

6.3 upload目錄下web.config內(nèi)容

<?xml version="1.0" encoding="UTF-8"?>
 <configuration>
 <system.webServer>
 <handlers>
 <add name="FlaskFastCGI" path="*" verb="*" modules="FastCgiModule" scriptProcessor="d:\software\python27\python.exe|d:\software\python27\lib\site-packages\wfastcgi.pyc" resourceType="Unspecified" requireAccess="Script" />
 </handlers>
 <security>
 <requestFiltering allowDoubleEscaping="true"></requestFiltering>
 </security>
 <directoryBrowse enabled="true" />
 </system.webServer>

<appSettings>
 <!-- Required settings -->
 <add key="WSGI_HANDLER" value="upload.app" />
 <add key="PYTHONPATH" value="~/" />

<!-- Optional settings -->
 <add key="WSGI_LOG" value="d:\data\mysite\logs\oboeqa_web.log" />
 <add key="WSGI_RESTART_FILE_REGEX" value="" />
 </appSettings>
 </configuration>

注意:

  • scriptProcessor的內(nèi)容是執(zhí)行wfastcgi-enable的輸出

  • WSGI_HANDLER的value

  • PYTHONPATH的value

  • WSGI_LOG的目錄一定要存在

6.4 upload.py上傳文件的代碼

#_*_coding:utf-8_*_
import os
from flask import Flask, request, redirect, url_for,render_template
from werkzeug import secure_filename
from flask import send_from_directory


UPLOAD_FOLDER = 'd:\data\mysite\upload\static'
ALLOWED_EXTENSIONS = set(['txt', 'docx', 'doc', 'xlsx' , 'xls','ppt' , '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':
        file = request.files['file']
        filename = file.filename
        if file and allowed_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 redirect('success.html')
    return '''
    <!doctype html>
    <title>Upload new File</title>
    <h2>Upload new File</h2>
    <form action="" method=post enctype=multipart/form-data>
      <p><input type=file name=file>
         <input type=submit value=Upload>
    </form>
    '''
@app.route('/upload/<filename>')
def uploaded_file(filename):
    return u'文件上傳成功'

if __name__ == '__main__':
    app.run()

7、flask學習網(wǎng)站

http://docs.jinkan.org/docs/flask/


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI