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框架是最好的选择。