使用标准库的 Python Web 编程

发布于 2024-09-27 04:45:19 字数 528 浏览 8 评论 0原文

我想编写一个简单的 python Web 应用程序来为命令行程序提供 gui(例如,考虑 hgserve)。它只能在本地运行。我不希望它有任何外部依赖项以方便部署,因此一般来说 python Web 编程在这里不适用

如何才能以最小的麻烦完成它?任何指针如何使用 cgiwsgistring.Templatestring.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

西瑶 2024-10-04 04:45:19

标准库中的 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)

︶葆Ⅱㄣ 2024-10-04 04:45:19

您是否正在寻找类似 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

猫瑾少女 2024-10-04 04:45:19

也许 twisted 是正确的选择?或者您认为这过于依赖外部?

Maybe twisted is the way to go? Or do you consider this to be too much of an external dependency?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文