使用标准库的 Python Web 编程
我想编写一个简单的 python Web 应用程序来为命令行程序提供 gui(例如,考虑 hgserve
)。它只能在本地运行。我不希望它有任何外部依赖项以方便部署,因此一般来说 python Web 编程在这里不适用
如何才能以最小的麻烦完成它?任何指针如何使用 cgi
或 wsgi
、string.Template
或 string.Formatter
轻松完成此操作?我更喜欢 Python 2.6 解决方案,但即使是 Python 3.x 也可以。我还更喜欢使用一些 html 模板来手动将 html 组装在一起。
更新: 理想的解决方案包括
- 处理表单
- 、上传/下载文件、
- 输出 html 以及
- 启动网络服务器的方法
I want to write a simple python web application to provide a gui to a command line program (think of hg serve
, for example). It would run locally only. I don't want it to have any external dependencies for an easier deployment, so python web programming in general wouldn't apply here
How can it be done with a minimal hassle? Any pointer how to do it easily with cgi
or wsgi
, string.Template
or string.Formatter
? I'd prefer a Python 2.6 solution, but even a Python 3.x one is OK. I'd also prefer using a few html templates to manually assembling html together.
UPDATE:
The ideal solution would include ways
- to process a form
- to upload/download a file
- to output html
- to start a webserver
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
标准库中的 wsgiref 包有一个简单的服务器来为 wsgi 应用程序提供服务。您可以使用它来运行您自己的无框架 wsgi 应用程序,最小的 wsgi 应用程序并不是非常困难(请参阅 wsgiref 文档页面末尾的 hello world 示例)
您可能希望放宽“标准库”要求小的。无论如何,你都会对自己的模块产生“依赖”,使用其他人已经完成工作的东西真的那么糟糕吗?一些所谓的“微框架”对于部署来说应该不会有太大问题。例如, Bottle 作为单个文件模块提供,除了stdlib(我自己还没有使用过 Bottle,但我选择它作为示例,主要是因为单个文件/没有依赖项)
The wsgiref package from the standard library has a simple server to serve wsgi applications. You can use it to run your own framework-less wsgi application, a minimal wsgi application isn't terribly difficult (see the hello world example at the end of the wsgiref documentation page)
You might want to relax the "standard library" requirement a little. You're going to have "dependencies" on your own modules anyway, is it really that bad to use something where someone else has already done the work? Some of the so-called "microframeworks" shouldn't be too much of a problem for deployment. Bottle for example, comes as a single file module and has no dependencies other than stdlib (haven't used Bottle yet myself, but I picked that one as an example mainly because of the single file/no dependencies)
您是否正在寻找类似 SimpleHTTPServer 的东西?
http://docs.python.org/library/simplehttpserver.html#module-SimpleHTTPServer
Are you looking for something like SimpleHTTPServer?
http://docs.python.org/library/simplehttpserver.html#module-SimpleHTTPServer
也许 twisted 是正确的选择?或者您认为这过于依赖外部?
Maybe twisted is the way to go? Or do you consider this to be too much of an external dependency?