flask用的什麼伺服器
❶ 怎麼使用python flask搭建靜態伺服器
Frozen-Flask freezes aFlaskapplication into a set of static files. The result can be hosted without any server-side software other than a traditional web server.
Note:This project used to be called Flask-Static.
Installation
Install the extension with one of the following commands:
$ easy_install Frozen-Flask
or alternatively if you have pip installed:
$ pip install Frozen-Flask
or you can get thesource code from github.
Context
This documentation assumes that you already have a workingFlaskapplication. You can run it and test it with the development server:
from myapplication import appapp.run(debug=True)
Frozen-Flask is only about deployment: instead of installing Python, a WGSI server and Flask on your server, you can use Frozen-Flask tofreezeyour application and only have static HTML files on your server.
Getting started
Create aFreezerinstance with yourappobject and call itsfreeze()method. Put that in afreeze.pyscript (or call it whatever you like):
from flask_frozen import Freezerfrom myapplication import appfreezer = Freezer(app)if __name__ == '__main__':
freezer.freeze()
This will create abuilddirectory next to your application』, with your application』s content frozen into static files.
Note
Frozen-Flask considers it 「owns」 its build directory. By default, it willsilently overwritefiles in that directory, andremovethose it did not create.
Theconfigurationallows you to change the destination directory, or control what files are removed if at all.
This build will most likely be partial since Frozen-Flask can only guess so much about your application.
Finding URLs
Frozen-Flask works by simulating requests at the WSGI level and writing the responses to aptly named files. So it needs to find out which URLs exist in your application.
The following URLs can be found automatically:
Static files handled by Flask for your application or any of itsblueprints.
Views with no variable parts in the URL, if they accept theGETmethod.
New in version 0.6:Results of calls toflask.url_for()made by your application in the request for another URL. In other words, if you useurl_for()to create links in your application, these links will be 「followed」.
- @app.route('/')def procts_list():
- return render_template('index.html', procts=models.Proct.all())@app.route('/proct_<int:proct_id>/')def proct_details():
- proct = models.Proct.get_or_404(id=proct_id)
- return render_template('proct.html', proct=proct)
- @freezer.register_generatordef proct_details():
- for proct in models.Proct.all():
- yield {'proct_id': proct.id}
- @freezer.register_generatordef proct_details(): # endpoint defaults to the function name
- # `values` dicts
- yield {'proct_id': '1'}
- yield {'proct_id': '2'}@freezer.register_generatordef proct_url_generator(): # Some other function name
- # `(endpoint, values)` tuples
- yield 'proct_details', {'proct_id': '1'}
- yield 'proct_details', {'proct_id': '2'}@freezer.register_generatordef proct_url_generator():
- # URLs as strings
- yield '/proct_1/'
- yield '/proct_2/'@freezer.register_generatordef proct_url_generator():
- # Return a list. (Any iterable type will do.)
- return [
- '/proct_1/',
- # Mixing forms works too.
- ('proct_details', {'proct_id': '2'}),
- ]
- if __name__ == '__main__':
- freezer.run(debug=True)
FREEZER_BASE_URL
Full URL your application is supposed to be installed at. This affects the output offlask.url_for()for absolute URLs (with_external=True) or if your application is not at the root of its domain name. Defaults to'http://localhost/'.
FREEZER_RELATIVE_URLS
If set toTrue, Frozen-Flask will patch the Jinja environment so thaturl_for()returns relative URLs. Defaults toFalse. Python code is not affected unless you userelative_url_for()explicitly. This enables the frozen site to be browsed without a web server (opening the files directly in a browser) but appends a visibleindex.htmlto URLs that would otherwise end with/.
New in version 0.10.
FREEZER_DEFAULT_MIMETYPE
The MIME type that is assumed when it can not be determined from the filename extension. If you』re using the Apache web server, this should match theDefaultTypevalue of Apache』s configuration. Defaults toapplication/octet-stream.
New in version 0.7.
FREEZER_IGNORE_MIMETYPE_WARNINGS
If set toTrue, Frozen-Flask won』t show warnings if the MIME type returned from the server doesn』t match the MIME type derived from the filename extension. Defaults toFalse.
New in version 0.8.
FREEZER_DESTINATION
Path to the directory where to put the generated static site. If relative, interpreted as relative to the application root, next to . Defaults tobuild.
FREEZER_REMOVE_EXTRA_FILES
If set toTrue(the default), Frozen-Flask will remove files in the destination directory that were not built ring the current freeze. This is intended to clean up files generated by a previous call toFreezer.freeze()that are no longer needed. Setting this toFalseis equivalent to settingFREEZER_DESTINATION_IGNOREto['*'].
New in version 0.5.
FREEZER_DESTINATION_IGNORE
A list (defaults empty) offnmatchpatterns. Files or directories in the destination that match any of the patterns are not removed, even ifFREEZER_REMOVE_EXTRA_FILESis true. As in.gitignorefiles, patterns apply to the whole path if they contain a slash/, to each slash-separated part otherwise. For example, this could be set to['.git
This means that if your application has an index page at the URL/(without parameters) and every other page can be found from there by recursively following links built withurl_for(), then Frozen-Flask can discover all URLs automatically and you』re done.
Otherwise, you may need to write URL generators.
URL generators
Let』s say that your application looks like this:
If, for some reason, some procts pages are not linked from another page (or these links are not built byurl_for()), Frozen-Flask will not find them.
To tell Frozen-Flask about them, write an URL generator and put it after creating yourFreezerinstance and before callingfreeze():
Frozen-Flask will find the URL by callingurl_for(endpoint,**values)whereendpointis the name of the generator function andvaluesis each dict yielded by the function.
You can specify a different endpoint by yielding a(endpoint,values)tuple instead of justvalues, or you can by-passurl_forand simply yield URLs as strings.
Also, generator functions do not have to bePython generatorsusingyield, they can be any callable and return any iterable object.
All of these are thus equivalent:
Generating the same URL more than once is okay, Frozen-Flask will build it only once. Having different functions with the same name is generally a bad practice, but still work here as they are only used by their decorators. In practice you will probably have a mole for your views and another one for the freezer and URL generators, so having the same name is not a problem.
Testing URL generators
The idea behind Frozen-Flask is that you canuse Flask directlyto develop and test your application. However, it is also useful to test yourURL generatorsand see that nothing is missing, before deploying to a proction server.
You can open the newly generated static HTML files in a web browser, but links probably won』t work. TheFREEZER_RELATIVE_URLSconfigurationcan fix this, but adds a visibleindex.htmlto the links. Alternatively, use therun()method to start an HTTP server on the build result, so you can check that everything is fine before uploading:
Freezer.run()will freeze your application before serving and when the reloader kicks in. But the reloader only watches Python files, not templates or static files. Because of that, you probably want to useFreezer.run()only for testing the URL generators. For everything else use the usualapp.run().
Flask-Scriptmay come in handy here.
Controlling What Is Followed
Frozen-Flask follows links automatically or with some help from URL generators. If you want to control what gets followed, then URL generators should be used with the Freezer』swith_no_argument_rulesandlog_url_forflags. Disabling these flags will force Frozen-Flask to use URL generators only. The combination of these three elements determines how much Frozen-Flask will follow.
Configuration
Frozen-Flask can be configured using Flask』sconfiguration system. The following configuration values are accepted:
❷ flask 適合使用什麼資料庫
反正我用的是pymongo(不是flask的擴展)直接可以用的。
到現在都不喜歡orm ,直接操作數據多好了,為什麼還要多那麼一層。
況且直接用pymongo也不用處理什麼,直接拿來就可以用。
❸ 運行什麼文件可以打開伺服器測試flask
咨詢記錄 · 回答於2021-09-18
❹ 【Python基礎】flask框架是用來干什麼的
你可以用來開發網站伺服器,它包含一個wsgi工具包(werkzeug)、 模板引擎(jinja2)還有主體(flask)。
安裝方式:
- 打開命令行
- 輸入命令
- 命令是"pip install flask"
❺ 使用flask開發的網站有哪些
Python 生態圈有兩個現象級的 Web 框架 Flask, Django.
兩個框架風格迥異, 但是都各自帶動了龐大的生態圈, 這得益於二者靈活的擴展能力.
本書講述的是基於 Flask 開發 Web 項目.
在對 Flask 框架的各個部分簡要分析後, 本書介紹了
* 表單處理(Flask-WTF)
* 持久化(Flask-SQLAlchemy, 這貨作者就是 Flask 的作者喲)
* 郵件
* 配置
* 一個真實案例: 模型, 用戶驗證, 角色, 關注, 寫template, 寫API
# 特點
* 線程局部變數
我不知道該把這個稱作特點還是缺點.
至少在Flask的文檔中作者明確給出這樣的解釋: 作為一個碼農, 你也許會感到不適, 但我就想這樣設計.
大部分的 Web 框架, 請求對象是外部注入的, 唯獨 Flask 選擇了全局 (flask.request, flask.g).
這個特點存在意味著你要小心使用這個特性, 否則很容易遇上需要調試大半天的Bug.
另外, request 不是那麼容易造出來的.
不過, 這不是什麼大不了的事情.
這個順便波及到測試, 測試的setup 與 teardown, 你必須去營造一個上下文, 關於這點, 書中有講解如何操作.
* Route/Template
框架的路由使用的是Werkzeug.
Template使用的是Jinja2, 當然不喜歡的話, 用別的也很簡單.
* 足夠小
Flask 只封裝了請求, 路由, 模板這么幾個功能.
用起來容易, 要寫好也得費點腦子漲點經驗才行.
說穿了也就是一句話:
> The idea of Flask is to build a good foundation for all applications.
> Everything else is up to you or extensions.
[Ref: What Flask is, What Flask is Not](http://flask.pocoo.org/docs/design/#what-flask-is-what-flask-is-not)
# 工作流
常規的三板斧: 開發, 測試, 部署.
書裡面的部署和配置這兩章節講的很出色, 值得一讀, 我給打五星.
# 插件
借用 @死魚眼28號 常說的一句話, 很多 Flask 插件都寫得很渣.
我表示 +1.
挑選插件時記得看下插件源碼, 給作者的碼力打個分再決定要不要用.
# 後記
關於 Web 開發, 我們的選擇有很多:
* PHP(Laravel, CodeIgniter, Yii, Symfony, CakePHP, etc.)
* Ruby(RoR, Sinatra, etc.)
* Python(Flask, Django, Quixote, Web.py, Bottle, etc.)
* Java/Scala(Spring, Play!, etc.)
殊途同歸, 他們也給出了幾乎一樣的解決方案.
我們可以看到大部分的 Web 項目都有著類似的目錄分類, 類似的架構.
Flask 也不外乎如此: 幫你包裝好請求對象, 剩下的路由, 路由邏輯, 響應內容你來填.
上面大部分框架從大學到工作或多或少使用過, 其實真正寫到業務層面時, 框架那些都不是事兒.
你想要的東西, 或框架自己造, 或慫恿隊友幫你造, 或你自己造, 總之基本上你總能拿到你要的信息.
不要被框架局限了視野喲.
❻ python庫Django,Flask各有什麼用途
Flask適合做app後台或基於json通信的ajax應用,是輕量級框架,我用到的一個技術方案是pypy+ Flask +peewee + uWSGI + nginx + ubuntu server,速度快,開發效率高,主要是通過伺服器返回json數據(json可以用壓縮方式)走http和各個app交互,你也可以用Flask+WebSocket實現類似在線游戲的長連接,數據格式建議用json,如果有性能要求可以用Protocol Buffers協議。這樣基於json api(RESTful太底層了,一般用json文本就可以了)的開發方式,一套後台程序,可以用於ajax網頁,android,ios,pc,tv等多套app,最大程度滿足了軟體復用思想,否則一個app開發一個後台或變種後台,代價非常高,也不利於安全和性能。微服務就是這種思想擴展,但主要是針對企業應用,web2.0發展以來,一般應用用http api生成json數據交互就好,mvc模式不適合在http api上用,mvc模式是web1.0時代沒有ajax,json無法分離html的妥協解決方案。http調用類似函數一樣,有入口參數,返回json給調用者,當然可以在返回時加密、壓縮,從而實現低耦合高內聚。另外對於http api需要防止注入攻擊,加強許可權控制,限制調用次數。
Django屬於重量級的,很多東西是現存的,只要熟悉就可以用,但缺點是不如Flask定製靈活,適合用於普通的web應用。
❼ 求助,用flask搭android伺服器
最近這些年,REST已經成為web services和APIs的標准架構,很多APP的架構基本上是使用RESTful的形式了。 本文將會使用python的Flask框架輕松實現一個RESTful的服務。 REST的六個特性: Client-Server:伺服器端與客戶端分離。 Stateless(無狀態
❽ flask框架有什麼用
Flask是一個使用 Python 編寫的輕量級 Web 應用框架。其 WSGI 工具箱採用 Werkzeug ,模板引擎則使用 Jinja2 。
Flask使用 BSD 授權。Flask也被稱為 「microframework」 ,因為它使用簡單的核心,用 extension 增加其他功能。Flask沒有默認使用的資料庫、窗體驗證工具。
web網站發展至今,特別是伺服器端,涉及到的知識、內容,非常廣泛,這對程序員的要求會越來越高的。如果採用成熟,穩健的框架,那麼一些基礎的工作,比如說安全性,數據流控制等都可以讓框架來處理,那麼程序開發人員就可以將精力放在具體的業務邏輯上面。
使用框架的優勢:穩定性、可擴展性強,可以降低開發效率,提高開發效率。
而Flask框架是Python的web框架,最大特徵就是輕便,讓開發者自由靈活的兼容開發的feature。Python語言靈活性給予了Flask框架同樣的特徵,無論用戶畫像還是產品推薦,Python對比其他語言都有很大的優勢。
另外Flask框架輕便,容易上手,試錯成本低,搭建網站的時候,Flask框架是最好的選擇。