標籤: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>
注意:
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之實現檔案上傳功能