IIS部署flask之實現檔案上傳功能

來源:互聯網
上載者:User

標籤:iis flask

1、環境

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
已經在配置提交路徑“MACHINE/WEBROOT/APPHOST”向“MACHINE/WEBROOT/APPHOST”的“system.webServer/fastCgi”節應用了配置更改
“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

650) this.width=650;" src="http://jbcdn2.b0.upaiyun.com/2017/04/437b9614cffd92bd8585ff40532c71f8.png" style="border:0px;vertical-align:middle;height:auto;" alt="437b9614cffd92bd8585ff40532c71f8.png" />

5、安裝URL重寫

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

650) this.width=650;" class="alignnone wp-image-883 size-full" src="https://10.12.49.221/wp-content/uploads/2017/08/URL%E9%87%8D%E5%86%99%E5%B7%A5%E5%85%B7%E5%AE%89%E8%A3%85.png" width="907" height="617" style="border:0px;vertical-align:middle;height:auto;" />

6、配置IIS6.1 添加網站,根目錄是d:\data\mysite\upload

650) this.width=650;" class="alignnone wp-image-884 size-large" src="https://10.12.49.221/wp-content/uploads/2017/08/IIS%E6%B7%BB%E5%8A%A0%E7%BD%91%E7%AB%99-1024x540.png" width="640" height="338" style="border:0px;vertical-align:middle;height:auto;" />

6.2 d:\data\mysite\upload目錄結構

upload

–static上傳目錄的靜態檔案目錄

–upload.py上傳檔案程式

–web.config設定檔

6.3 upload目錄下web.config內容
<?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的內容是執行wfastcgi-enable的輸出

  • WSGI_HANDLER的value

  • PYTHONPATH的value

  • WSGI_LOG的目錄一定要存在

6.4 upload.py上傳檔案的代碼
#_*_coding:utf-8_*_import osfrom flask import Flask, request, redirect, url_for,render_templatefrom werkzeug import secure_filenamefrom flask import send_from_directoryUPLOAD_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_FOLDERdef 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>    <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(‘/upload/<filename>‘)def uploaded_file(filename):    return u‘檔案上傳成功‘if __name__ == ‘__main__‘:    app.run()
7、flask學習網站

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


本文出自 “走在路上的朋友” 部落格,請務必保留此出處http://supan.blog.51cto.com/7405859/1956899

IIS部署flask之實現檔案上傳功能

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.