哪个 Python Web 框架可以轻松开发和部署?
我在 BaseHTTPServer 中编写了一个 Web API。它只能在本地主机上使用。它在 GET/POST 操作上返回 JSON 对象。
http://localhost:8888/operation?param
代码就像
def do_GET(self):
if self.path=="operation":
self.wfile.write("output")
但我担心保持活动机制(阅读:可以重生工作人员的网络服务器)、缺乏多线程和 PITA 式维护。
正如我所说,我正在考虑选择此 Web 框架的开发和部署问题。
开发
Web 界面目前有 250 行,功能非常简单。我正在寻找适合清洁维护和部署的东西。我不希望框架的 MVC、ORM、模板和其他功能扰乱我的学习曲线。重定向到适当模块的 URL 模式很好。
部署
它应该轻松地部署在带有 WSGI 模块的成熟服务器上。这样的设置具有热部署(需要一个更好的词),安装新应用程序或更新代码意味着将文件复制到文件系统中的 www-root 。
CherryPy 和 Flask 看起来很有趣。 Django 和 Web2Py 看起来太全面了。
I have written a web API in BaseHTTPServer. It is meant to be used only on localhost. It returns JSON objects on GET/POST operations.
http://localhost:8888/operation?param
and code is like
def do_GET(self):
if self.path=="operation":
self.wfile.write("output")
But I am worried about keep-alive mechanisms (read: a webserver that can respawn workers), lack of multi-threading, and PITA-ful maintenance.
And like I said, I am looking at the development and deployment issues for choosing this web framework.
Development
The web interface is currently 250 lines and has very simple features. I am looking for something that lends itself well to clean maintenance and deployment. I dont want the framework's MVC, ORM, templating and other features messing my learning curve. UrL patterns that redirect to appropriate module is nice.
Deployment
It should deploy on a mature server with a WSGI module with minimum fuss. And such a setup has hot-deploy (for want of a better word), installing a new application or updating the code means copying the files to the www-root in the filesystem.
CherryPy and Flask seem interesting. Django and Web2Py seem too comprehensive.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
部署wsgi的推荐方式是作为长时间运行的进程,嵌入或守护进程,而不是作为cgi脚本。不管怎样,它与像在 php 中那样上传文件有点不同,通过触摸配置文件来重新启动服务器/进程通常是最接近使用 wsgi 的“热部署”。
不用说,如果框架本身符合 wsgi 规范,则不会施加任何类型的部署限制。根据您的需要选择:apache+modwsgi、gunicorn、cherry.py、paste。它们都不提供“热部署”(据我所知),您仍然需要创建一个 wsgi 脚本并重新加载进程。文件系统布局通常不用担心,这很好。您通常也不会自动重新加载。我知道 werkzeug 和cherry.py 可以,而且 werkzeug 也提供了一些非常酷的调试工具。请注意,tornado/werkzeug* 本身提供了自动重新加载选项,但实际上是考虑用于开发而不是部署,并且 与 wsgi 模块不兼容。
但无论部署有多么痛苦或轻松,都建议使用 Fabric 之类的东西来自动化部署,并且设置 wsgi Web 服务器并不那么困难。
框架本身的选择有点棘手,取决于你想在哪个级别工作。Tornado、werkzeug 是流行的低级框架(但也包括更高级别的工具,而且很多是框架 + Web 服务器),但你也可以直接使用 webob 并插入您需要的任何其他内容。
你有像 Flask 或 Bottle 这样的微框架,然后是像 web2.py 这样的轻量级框架,或者可能是金字塔(关于框架重量的界限有点模糊)。
然后你就有了“全栈”django、grok、turbogears 等......
然后是 Zope,它一直在节食,但仍然很重。
请注意,您几乎可以对所有它们执行任何操作(仅取决于您想要弯曲它们的程度),并且在许多情况下您可以相当轻松地交换组件。我会开始尝试像 bottle 或 flask 这样的微框架(您不必使用 ORM 或模板,但一旦使用就可以轻松使用),而且看看webob。
*评论:将 werkzeug 添加到并非真正的自动重新加载阵营中。
The recommended way of deploying wsgi is as a long-running-process, either embedded or daeomonized, and not as a cgi script. Either way, its going to be a little different than just uploading files like in php, restarting the server/process by touching the config file is normally the closest you get to "hot-deployment" using wsgi.
Needless to say, the framework itself does not impose any kind of deployment restraints if it is wsgi compliant. Take your pick depending on your needs: apache+modwsgi, gunicorn, cherry.py, paste. None of them offer "hot-deployment" (afaik), you will still need to create a wsgi script and reload the processes. The filesystem layout is normally of no concern and that's good. You don't usually get autoreload either. I know werkzeug and cherry.py do, and werkzeug offers some really cool debugging tools too. Please note that tornado/werkzeug* itself offers an autoreload option, but is actually considered for development and not deployment, and not compatible with the wsgi module.
But no matter how painful or painless the deployment is, it is recommended to use something like fabric to automate your deployments, and setting up a wsgi web server isnt that that hard.
Choice of the framework itself is kind of tricky, and depends on what level you want to work in. Tornado, werkzeug are popular low level frameworks, (but also include higher level tools, and many are frameworks+webserver), but you could also work with webob directly and just plugin whatever else you need.
You have the microframeworks like flask or bottle, then the lightweight frameworks, like web2.py, or maybe pyramid (the lines on how heavy a framework are kind of blurry).
Then you have the "full-stack" django, grok, turbogears, etc...
And then you have zope, which has been on a diet but still very heavy.
Note that you can pretty much do anything with all of them (just depends how much you want to bend them), and in many cases you can swap components rather easily. I'd start try out a microframework like bottle or maybe flask (you don't have to use ORM's or templating, but are easily available once you do), but also take a look at webob.
*comment: added werkzeug to the not really autoreload camp.
对于您所描述的内容,我会选择: Tornado Web Server
这是你好世界:
它具有高度可扩展性,而且我我认为您可能需要 10 分钟才能使用您的代码进行设置。
For what you describe, id go with: Tornado Web Server
This is the hello world:
It's highly scalable, and I think it may take you 10 minutes to set it up with your code.