如何在不依赖框架的情况下使用Python进行Web开发?

发布于 2024-07-14 03:48:49 字数 357 浏览 5 评论 0原文

我知道各种框架都有其优点,但我个人希望使用 python 进行 Web 开发尽可能简单:少编写框架,多编写 python

到目前为止,我发现的唯一能让我以最明显的方式做到这一点的东西是 web.py 但我有对其性能略有担忧。

对于那些使用 nginx(或其他风格)+mod_wsgi+web.py 的人...性能如何? 是否可以进一步改进?

对于那些使用过 web.py、喜欢这个想法并继续写出更好的东西或发现更好的东西的人...介意向我指出源代码吗?

我想听听所有引人注目的、最小的但强大的方法。

I know the various frameworks have their benefits, but I personally want my web development in python to be as straight-forward as possible: less writing to the framework, more writing python.

The only thing I have found so far that lets me do this in the most obvious way possible is web.py but I have slight concerns on its performance.

For those of you using nginx(or another flavour)+mod_wsgi+web.py... how's performance? Can it be improved further?

For those of you who have used web.py, liked the idea and went on to write something better or found something better... care to point me to the source?

I'd like to hear about all the conspicuous, minimal yet powerful approaches.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(10

吃不饱 2024-07-21 03:48:49

正确的方法是wsgi

WSGI 是Web 服务器网关接口。 它是 Web 服务器和应用程序服务器与 Web 应用程序通信的规范(尽管它也可以用于更多用途)。 它是一个Python标准,在PEP 333中详细描述< /强>。

当前所有框架都支持 wsgi。 许多网络服务器也支持它(包括 apache,通过 mod_wsgi)。 如果您想编写自己的框架,那么这是可行的方法。

这是直接写入 wsgi 的 hello world:

def application(environ, start_response):
    status = '200 OK'
    response_headers = [('Content-type','text/plain')]
    start_response(status, response_headers)
    return ['Hello world!\n']

将其放入 file.py 中,将您的 mod_wsgi apache 配置指向它,它将运行。 纯蟒蛇。 没有进口。 只是一个Python函数。

如果你真的在编写自己的框架,你可以检查 werkzeug。 它不是一个框架,而是 WSGI 应用程序的各种实用程序的简单集合,并且已成为最先进的 WSGI 实用程序模块之一。 它包括一个强大的调试器、功能齐全的请求和响应对象、处理实体标签的 HTTP 实用程序、缓存控制标头、HTTP 日期、cookie 处理、文件上传、强大的 URL 路由系统和一堆社区贡献的插件模块。 把无聊的部分从你手中拿走。

The way to go is wsgi.

WSGI is the Web Server Gateway Interface. It is a specification for web servers and application servers to communicate with web applications (though it can also be used for more than that). It is a Python standard, described in detail in PEP 333.

All current frameworks support wsgi. A lot of webservers support it also (apache included, through mod_wsgi). It is the way to go if you want to write your own framework.

Here is hello world, written to wsgi directly:

def application(environ, start_response):
    status = '200 OK'
    response_headers = [('Content-type','text/plain')]
    start_response(status, response_headers)
    return ['Hello world!\n']

Put this in a file.py, point your mod_wsgi apache configuration to it, and it will run. Pure python. No imports. Just a python function.

If you are really writing your own framework, you could check werkzeug. It is not a framework, but a simple collection of various utilities for WSGI applications and has become one of the most advanced WSGI utility modules. It includes a powerful debugger, full featured request and response objects, HTTP utilities to handle entity tags, cache control headers, HTTP dates, cookie handling, file uploads, a powerful URL routing system and a bunch of community contributed addon modules. Takes the boring part out of your hands.

我纯我任性 2024-07-21 03:48:49

有趣的是,即使提出了一个问题,询问如何在没有框架的情况下进行编写,每个人仍然蜂拥而至地推广他们最喜欢的框架。 OP 抱怨不想要一个“重量级框架”,而回复中提到了 Twisted,尤其是?! 现在来吧,真的。

是的,完全有可能编写直接的 WSGI 应用程序,并从独立模块中获取一些所需的功能,而不是让您的代码适应某个特定框架的世界观。

要走这条路,您通常需要熟悉 HTTP 和 CGI​​ 的基础知识(因为 WSGI 继承了早期规范的很多内容)。 这不一定是推荐给初学者的方法,但它是相当可行的。

我想听听所有引人注目的、最小的但强大的方法

您不会听到它们,因为没有人有兴趣推广“自己动手”作为一种方法。 我使用一个特定的独立模板包、一个特定的独立表单读取包、一个特定的数据访问层和一些自制实用程序模块。 我并不是在写一种我可以宣扬的特定哲学,它们都只是无聊的工具,可以替换成其他同样好的东西。

It's hilarious how, even prompted with a question asking how to write without a framework, everyone still piles in to promote their favourite framework. The OP whinges about not wanting a “heavyweight framework”, and the replies mention Twisted, of all things?! Come now, really.

Yes, it is perfectly possible to write straight WSGI apps, and grab bits of wanted functionality from standalone modules, instead of fitting your code into one particular framework's view of the world.

To take this route you'll generally want to be familiar with the basics of HTTP and CGI (since WSGI inherits an awful lot from that earlier specification). It's not necessarily an approach to recommend to beginners, but it's quite doable.

I'd like to hear about all the conspicuous, minimal yet powerful approaches

You won't hear about them, because no-one has a tribal interest in promoting “do it yourself” as a methodology. Me, I use a particular standalone templating package, a particular standalone form-reading package, a particular data access layer, and a few home-brew utility modules. I'm not writing to one particular philosophy I can proselytise about, they're all just boring tools that could be swapped out and replaced with something else just as good.

逆流 2024-07-21 03:48:49

您还可以检查 cherrypy。 Cherrypy 的重点是成为一个让您编写 Python 的框架。 Cherrypy 有自己相当好的网络服务器,但它与 wsgi 兼容,因此您可以通过 mod_wsgi 在 apache 中运行 Cherrypy 应用程序。 这是cherrypy中的hello world:

import cherrypy

class HelloWorld(object):
    def index(self):
        return "Hello World!"
    index.exposed = True

cherrypy.quickstart(HelloWorld())

You could also check cherrypy. The focus of cherrypy is in being a framework that lets you write python. Cherrypy has its own pretty good webserver, but it is wsgi-compatible so you can run cherrypy applications in apache via mod_wsgi. Here is hello world in cherrypy:

import cherrypy

class HelloWorld(object):
    def index(self):
        return "Hello World!"
    index.exposed = True

cherrypy.quickstart(HelloWorld())
涫野音 2024-07-21 03:48:49

对 WSGI 的所有答案+1。

Eric Florenzo 最近写了一篇很棒的博客文章,您应该阅读:编写速度极快、可无限扩展的纯 WSGI 实用程序。 这将使您更好地了解 Hello World 之外的纯 WSGI。 另请注意评论,尤其是 Kevin Dangoor 的第一条评论,他建议至少将 WebOb 添加到您的工具集中。

+1 to all the answers with WSGI.

Eric Florenzo wrote a great blog post lately you should read: Writing Blazing Fast, Infinitely Scalable, Pure-WSGI Utilities. This will give you a better idea of pure WSGI beyond Hello World. Also pay attention to the comments, especially the first comment by Kevin Dangoor where he recommends at least adding WebOb to your toolset.

静水深流 2024-07-21 03:48:49

无论如何,我用 mod_python 编写了我的网站,没有任何像 Django 这样的干预框架。 我真的没有任何理由抱怨。 (好吧,也许有一点,mod_python 在某些方面有点奇怪,但在常见用例中并非如此)有一点是肯定的,它肯定会让你编写 Python ;-)

For what it's worth, I wrote my website in mod_python without any intervening framework like Django. I didn't really have any reason to complain. (Well maybe a little, mod_python is kind of quirky in a few ways but not in the common use cases) One thing's for sure, it will definitely let you write Python ;-)

与酒说心事 2024-07-21 03:48:49

为什么您担心 web.py 的性能? 正如我此处提到的,我们使用CherryPy(“内置于”web.py 中的 Web 服务器)位于 nginx 为 Oyster.com 提供大部分 HTML —— nginx 将流量分配给 2 或 3 个 Web 服务器,每个服务器运行 4 个 Python 进程,我们可以轻松处理 100 个请求每秒。

Oyster.com 是一个大容量网站,平均每天动态生成的页面浏览量为 200,000 次,峰值数字远高于此。 但是,我们确实为图像和 CSS 等静态资源使用内容交付网络 (CDN)。

我们绝对关心性能(大多数页面的渲染时间少于 25 毫秒),但 web.py 并不是瓶颈。 我们的瓶颈是模板渲染(我们使用 Cheetah,它足够快,但速度还不够快)和数据库查询(我们大量缓存,并将每页的数据库查询数量保持为 0 或 1)并访问我们的第 3 方酒店定价提供商(当您使用我们尚未缓存的日期进行搜索时,将访问这些提供商)。

请记住,过早的优化是万恶之源——除非您为 google.com 提供服务,否则 web.py 可能会为您工作。

Why do you have concerns about web.py's performance? As I mentioned here, we use CherryPy (the web server "built into" web.py) behind nginx to serve most of the HTML at Oyster.com -- nginx splits the traffic across 2 or 3 web servers each running 4 Python processes, and we can easily handle 100s of requests per second.

Oyster.com is a high-volume website averaging 200,000 dynamically-generated pageviews/day, and peaking to much higher numbers than that. However, we do use a content delivery network (CDN) for our static resources like images and CSS.

We definitely care about performance (most of our pages render in less than 25ms), but web.py isn't the bottleneck. Our bottlenecks are template rendering (we use Cheetah, which is fast enough but not other-worldly fast) and database queries (we cache heavily and keep the number of database queries per page to 0 or 1) and accessing our 3rd-party hotel pricing providers (these are accessed when you do a search with dates we don't already have cached).

Remember, premature optimization is the root of all evil -- unless you're serving google.com, web.py will probably work for you.

晨与橙与城 2024-07-21 03:48:49

我使用 mod-python 和 PSP -- mod-python 相当于 php。

在一种情况下,我编写了一个网页,通过检查我们的源代码存储库来生成发行说明。 我将其重写为 PHP,并惊讶地发现 PSP 版本的速度快了大约 20%,代码行数也减少了大约一半。

所以,至少对于小问题,psp 对我来说效果很好。

I've written a few small web applications using mod-python and PSP -- mod-python's equivalent to php.

In one case, I wrote a web page that generates release notes by inspecting our source code repository. I rewrote it into PHP, and was surprised to discover that the PSP version was about 20% faster, as well as being about half as many lines of code.

So, for small problems at least, psp has worked well for me.

離人涙 2024-07-21 03:48:49

我认为这取决于框架是什么以及它应该为您做什么的定义。

正如所指出的,WSGI 是一个非常小的“框架”,因为它只定义了一个用于与 Web 服务器连接的小接口。 但这是一种强大的方法,因为您可以在应用程序和服务器之间放置中间件。

如果你想要更多一点,比如一些 URL 到函数的映射,那么你有一些选择,其中一些已经提到过。

如果你走得更远,你可能会看到 Pylons 或 Turbogears 或 Django,之后可能是 Zope,但它会变得更大,也许会带来痛苦,而且你总是会相信该框架的观点。

我最近越来越多地使用(来自 Zope/Plone)是 repoze.bfg。 它非常小,没有捆绑 ORM(因此您可以使用 SQLAlchemy、Storm 或简单地访问对象数据库,例如 ZODB)。 它所做的基本上是处理如何从 URL 到作为函数的视图。 它支持 URL 映射(如路由)或对象遍历,恕我直言,这在某些情况下非常强大,尤其是。 如果你有一个不那么严格的映射。 好处是它直接附带了基于 ACL 的安全框架,如果您愿意,可以使用该框架,恕我直言,它非常实用。 这样你就不需要装饰器,而装饰器似乎主要用于此类事情。

当然它是基于 WSGI 的。 还要查找 repoze subversion 存储库 以获取大量中间件和 粘贴内容对于 WSGI 相关任务也非常有用。

I think it depends on the definition of what a framework is and what it should do for you.

As pointed out, a very minimal "framework" would be WSGI as it only defines one small interface for interfacing with a web server. But it's a powerful approach because of the middleware you can put between your app and the server.

If you want more slightly more, like some URL to function mapping, then you have some choices, some of which have been already mentioned.

If you go further you might come to Pylons or Turbogears or Django, after that maybe Zope but it grows bigger and maybe the pain as well as you always buy into the opinions of that framework.

What I recently use more and more (coming from Zope/Plone) is repoze.bfg. It's very small, does not come with a ORM bundled (so you can use SQLAlchemy, Storm or simply go to an object database like the ZODB). What it does is basically handling how you come from a URL to a view which is a function. It supports both URL Mapping (a la Routes) or object traversal, which IMHO is very powerful in some circumstances esp. if you have a not so strict mapping. The good thing is that it directly comes with an ACL based security framework which can use if you want to which IMHO is very practical to have. That way you don't need decorators which seem to be used mostly for such things.

And of course it's based on WSGI. Also look for the repoze subversion repository for quite a lot of middleware and the Paste stuff is also very useful for WSGI related tasks.

躲猫猫 2024-07-21 03:48:49

姜戈出了什么问题? 它不会强迫您使用它的 ORM,并且控制器只是普通的 Python 函数,而不是类似 Rails 的类方法。 此外,url 路由是使用正则表达式而不是其他框架发明的语法来完成的。 如果 django 看起来对你来说太多了,我建议看看 Werkzeug

What's wrong with Django? It doesn't force you to use it's ORM and controllers are just plain Python functions instead of Rails-like class methods. Also, url routing is done with regular expressions instead of another framework-invented syntax. If django seems like too much for you anyway, i recommend taking a look at Werkzeug

浮华 2024-07-21 03:48:49

我非常喜欢 Google AppEngine。 我使用 ORM 和模板系统,但在其他方面遵循 REST 模式设计,只为相应的 HTTP 方法实现 Python 方法。 它使原始 HTTP 交互成为中心,并且可以选择为您提供其他可以使用的东西。 而且无需再配置和管理您的部署环境!

I'm pretty fond of Google AppEngine. I use the ORM and templating system, but otherwise follow a REST-patterned design and just implement Python methods for the corresponding HTTP ones. It makes the raw HTTP interaction central, and optionally gives you other things to use. Plus no more configuring and managing your deployment environment!

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