Python Web 编程的不同方法的优缺点

发布于 2024-07-04 22:16:45 字数 894 浏览 6 评论 0 原文

我想使用 Python 编写一些服务器端脚本。 但我有点迷失了许多方法来做到这一点。

它从 DIY CGI 方法开始,似乎以一些非常强大的框架结束,这些框架基本上可以自己完成所有工作。 中间还有很多东西,比如 web.pyPyrrideDjango

  • 您所使用的框架或方法的优点缺点是什么?
  • 有哪些权衡
  • 他们在什么类型的项目方面做得很好,在什么方面做得不好?

编辑:我还没有太多的网络编程经验。

我想避免一些基本而乏味的事情,比如解析 URL 中的参数等。
另一方面,博客的视频在 15 分钟内创建,其中 Ruby on Rails 给我留下了深刻的印象,我意识到有数百件事情对我隐藏 - 如果您需要编写一个有效的 < code>webapp 很快,但对于真正理解它的魔力来说并不是那么好 - 这就是我现在所寻求的。

I'd like to do some server-side scripting using Python. But I'm kind of lost with the number of ways to do that.

It starts with the do-it-yourself CGI approach and it seems to end with some pretty robust frameworks that would basically do all the job themselves. And a huge lot of stuff in between, like web.py, Pyroxide and Django.

  • What are the pros and cons of the frameworks or approaches that you've worked on?
  • What trade-offs are there?
  • For what kind of projects they do well and for what they don't?

Edit: I haven't got much experience with web programing yet.

I would like to avoid the basic and tedious things like parsing the URL for parameters, etc.
On the other hand, while the video of blog created in 15 minutes with Ruby on Rails left me impressed, I realized that there were hundreds of things hidden from me - which is cool if you need to write a working webapp in no time, but not that great for really understanding the magic - and that's what I seek now.

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

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

发布评论

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

评论(9

棒棒糖 2024-07-11 22:16:46

最简单的 Web 程序是 CGI 脚本,它基本上只是一个程序,其标准输出被重定向到发出请求的 Web 浏览器。 在这种方法中,每个页面都有自己的可执行文件,必须在每次请求时加载和解析该文件。 这使得启动和运行某些东西变得非常简单,但在性能和组织方面的扩展性都很差。 因此,当我需要一个非常快速且不会发展成更大系统的动态页面时,我会使用 CGI 脚本。

更进一步的一步是将 Python 代码嵌入到 HTML 代码中,例如使用 PSP。 我认为现在没有多少人使用它,因为现代模板系统已经使它变得相当过时了。 我使用 PSP 工作了一段时间,发现它与 CGI 脚本具有基本相同的组织限制(每个页面都有自己的文件),并且由于尝试将不了解空白的 HTML 与对空白敏感的 Python 混合在一起而产生了一些与空白相关的烦恼。

下一步是非常简单的 Web 框架,例如我也使用过的 web.py。 与 CGI 脚本一样,启动并运行某些内容非常简单,并且不需要任何复杂的配置或自动生成的代码。 您自己的代码将非常容易理解,因此您可以看到发生了什么。 然而,它的功能并不像其他 Web 框架那么丰富; 上次我使用它时,没有会话跟踪,所以我不得不自己动手。 它还有“太多神奇的行为”来引用 Guido(“upvars(),呸”)。

最后,您将拥有功能丰富的 Web 框架,例如 Django。 这些需要一些工作才能让简单的 Hello World 程序正常工作,但是每个主要的程序都有一个很棒的、写得很好的教程(尤其是 Django)来引导您完成它。 我强烈建议在任何实际项目中使用这些 Web 框架之一,因为它具有便利性、功能和文档等。

最终您必须决定您喜欢什么。 例如,框架都使用模板语言(特殊代码/标签)来生成HTML文件。 其中一些模板(例如 Cheetah 模板)允许您编写任意 Python 代码,以便您可以在模板中执行任何操作。 其他模板(例如 Django 模板)则更具限制性,迫使您将表示代码与程序逻辑分开。 这完全取决于您个人的喜好。

另一个例子是 URL 处理; 某些框架(例如 Django)让您通过正则表达式在应用程序中定义 URL。 其他工具(例如 CherryPy)会根据函数名称自动将函数映射到 url。 再次强调,这是个人喜好。

我个人使用混合的 Web 框架,使用 CherryPy 来处理我的 Web 服务器内容(表单参数、会话处理、URL 映射等),使用 Django 来处理我的对象关系映射和模板。 我的建议是从一个高级 Web 框架开始,按照您的方式完成其教程,然后开始一个小型个人项目。 我已经使用我提到的所有技术完成了这项工作,而且非常有益。 最终,您会感受到自己喜欢什么,并在此过程中成为一名更好的网络程序员(并且总体上是一名更好的程序员)。

The simplest web program is a CGI script, which is basically just a program whose standard output is redirected to the web browser making the request. In this approach, every page has its own executable file, which must be loaded and parsed on every request. This makes it really simple to get something up and running, but scales badly both in terms of performance and organization. So when I need a very dynamic page very quickly that won't grow into a larger system, I use a CGI script.

One step up from this is embedding your Python code in your HTML code, such as with PSP. I don't think many people use this nowadays, since modern template systems have made this pretty obsolete. I worked with PSP for awhile and found that it had basically the same organizational limits as CGI scripts (every page has its own file) plus some whitespace-related annoyances from trying to mix whitespace-ignorant HTML with whitespace-sensitive Python.

The next step up is very simple web frameworks such as web.py, which I've also used. Like CGI scripts, it's very simple to get something up and running, and you don't need any complex configuration or automatically generated code. Your own code will be pretty simple to understand, so you can see what's happening. However, it's not as feature-rich as other web frameworks; last time I used it, there was no session tracking, so I had to roll my own. It also has "too much magic behavior" to quote Guido ("upvars(), bah").

Finally, you have feature-rich web frameworks such as Django. These will require a bit of work to get simple Hello World programs working, but every major one has a great, well-written tutorial (especially Django) to walk you through it. I highly recommend using one of these web frameworks for any real project because of the convenience and features and documentation, etc.

Ultimately you'll have to decide what you prefer. For example, frameworks all use template languages (special code/tags) to generate HTML files. Some of them such as Cheetah templates let you write arbitrary Python code so that you can do anything in a template. Others such as Django templates are more restrictive and force you to separate your presentation code from your program logic. It's all about what you personally prefer.

Another example is URL handling; some frameworks such as Django have you define the URLs in your application through regular expressions. Others such as CherryPy automatically map your functions to urls by your function names. Again, this is a personal preference.

I personally use a mix of web frameworks by using CherryPy for my web server stuff (form parameters, session handling, url mapping, etc) and Django for my object-relational mapping and templates. My recommendation is to start with a high level web framework, work your way through its tutorial, then start on a small personal project. I've done this with all of the technologies I've mentioned and it's been really beneficial. Eventually you'll get a feel for what you prefer and become a better web programmer (and a better programmer in general) in the process.

写下不归期 2024-07-11 22:16:46

如果您决定使用基于 WSGI 的框架(例如 TurboGears),我会推荐您可以阅读 Ian Bicking 撰写的优秀文章 Another Do-It-Yourself Framework

在文章中,他从头开始构建了一个简单的 Web 应用程序框架。

另外,请观看 Kevin Dangoor 的视频使用 WSGI 创建 Web 框架。 Dangoor 是 TurboGears 项目的创始人。

If you decide to go with a framework that is WSGI-based (for instance TurboGears), I would recommend you go through the excellent article Another Do-It-Yourself Framework by Ian Bicking.

In the article, he builds a simple web application framework from scratch.

Also, check out the video Creating a web framework with WSGI by Kevin Dangoor. Dangoor is the founder of the TurboGears project.

玻璃人 2024-07-11 22:16:46

如果你想做得更大,选择 Django 就可以了。 但是,如果您只是想学习,请使用已经提到的 WebOb 推出自己的框架 - 这真的很有趣并且我相信您会学到更多(而且您可以使用您喜欢的组件:模板系统、url 调度程序、数据库层、会话等)。

在过去 2 年里,我使用 Django 构建了几个大型网站,我只能说,Django 将在 20% 的时间内满足你 80% 的需求。 无论您使用哪种框架,剩下 20% 的工作都将花费 80% 的时间。

If you want to go big, choose Django and you are set. But if you want just to learn, roll your own framework using already mentioned WebOb - this can be really fun and I am sure you'll learn much more (plus you can use components you like: template system, url dispatcher, database layer, sessions, et caetera).

In last 2 years I built few large sites using Django and all I can say, Django will fill 80% of your needs in 20% of time. Remaining 20% of work will take 80% of the time, no matter which framework you'd use.

陌生 2024-07-11 22:16:46

作为学习练习,以艰难的方式做一些事情(一次)总是值得的。 一旦了解了它的工作原理,就可以选择一个适合您的应用程序的框架并使用它。 一旦了解了角速度,您就不需要重新发明轮子。 :-)

在开始之前,还需要确保您对框架背后的编程语言有相当深入的了解 - 尝试同时学习 Django 和 Python(或 Ruby 和Rails(或 X 和 Y)可能会导致更多混乱。 先用该语言编写一些代码,然后添加框架。

我们学习开发,不是通过使用工具,而是通过解决问题。 撞到几堵墙,爬过去,找到更高的墙!

It's always worth doing something the hard way - once - as a learning exercise. Once you understand how it works, pick a framework that suits your application, and use that. You don't need to reinvent the wheel once you understand angular velocity. :-)

It's also worth making sure that you have a fairly robust understanding of the programming language behind the framework before you jump in -- trying to learn both Django and Python at the same time (or Ruby and Rails, or X and Y), can lead to even more confusion. Write some code in the language first, then add the framework.

We learn to develop, not by using tools, but by solving problems. Run into a few walls, climb over, and find some higher walls!

天邊彩虹 2024-07-11 22:16:46

如果您之前从未进行过任何 CGI 编程,我认为值得使用 DIY 方法做一个项目(也许只是为您自己制作一个示例游戏网站)。 与使用框架相比,您将学到更多关于所有各个部分如何工作的知识。 这将帮助您设计和调试所有未来的 Web 应用程序,无论您如何编写它们。

就我个人而言,我现在使用 Django。 真正的好处是非常快速的应用程序部署。 对象关系映射使事情进展迅速,模板库使用起来很有趣。 此外,管理界面还为您提供所有对象的基本 CRUD 屏幕,因此您无需编写任何“无聊”的内容。

使用基于 ORM 的解决方案的缺点是,如果您确实想要手工编写一些 SQL(例如出于性能原因),那么它比其他方式要困难得多,尽管仍然很有可能。

If you've never done any CGI programming before I think it would be worth doing one project - perhaps just a sample play site just for yourself - using the DIY approach. You'll learn a lot more about how all the various parts work than you would by using a framework. This will help in you design and debug and so on all your future web applications however you write them.

Personally I now use Django. The real benefit is very fast application deployment. The object relational mapping gets things moving fast and the template library is a joy to use. Also the admin interface gives you basic CRUD screens for all your objects so you don't need to write any of the "boring" stuff.

The downside of using an ORM based solution is that if you do want to handcraft some SQL, say for performance reasons, it much harder than it would have been otherwise, although still very possible.

壹場煙雨 2024-07-11 22:16:46

如果您使用 Python,则不应从 CGI 开始,而应从 WSGI 开始(并且您可以使用 wsgiref.handlers.CGIHandler 将 WSGI 脚本作为 CGI 脚本运行。结果基本上与 CGI 一样低级(这可能在具有教育意义,但也会有点烦人),但不必编写完全过时的接口(并将您的应用程序绑定到单个进程模型)

如果您想要一个不那么烦人但类似的低级接口,请使用<。 a href="http://pythonpaste.org/webob/" rel="nofollow noreferrer">WebOb 会提供这一点,您将实现所有逻辑,并且将会有。一些你不会理解的黑暗角落,但你不必花时间弄清楚如何解析 HTTP 日期(它们很奇怪!)或解析 POST 正文 我用这种方式编写应用程序(没有任何其他框架)。是完全可行的。 作为初学者,如果您有兴趣了解框架的用途,我会建议您这样做,因为您不可避免地会编写自己的迷你框架。 OTOH,一个真正的框架可能会教您应用程序设计和结构的良好实践。 要成为一名真正优秀的网络程序员,我相信你需要认真尝试两者; 您应该了解框架所做的一切,并且不要害怕其内部结构,但您也应该花时间在其他人设计的深思熟虑的环境(即现有框架)中,并了解该结构如何帮助您。

If you are using Python you should not start with CGI, instead start with WSGI (and you can use wsgiref.handlers.CGIHandler to run your WSGI script as a CGI script. The result is something that is basically as low-level as CGI (which might be useful in an educational sense, but will also be somewhat annoying), but without having to write to an entirely outdated interface (and binding your application to a single process model).

If you want a less annoying, but similarly low-level interface, using WebOb would provide that. You would be implementing all the logic, and there will be few dark corners that you won't understand, but you won't have to spend time figuring out how to parse HTTP dates (they are weird!) or parse POST bodies. I write applications this way (without any other framework) and it is entirely workable. As a beginner, I'd advise this if you were interested in understanding what frameworks do, because it is inevitable you will be writing your own mini framework. OTOH, a real framework will probably teach you good practices of application design and structure. To be a really good web programmer, I believe you need to try both seriously; you should understand everything a framework does and not be afraid of its internals, but you should also spend time in a thoughtful environment someone else designed (i.e., an existing framework) and understand how that structure helps you.

桜花祭 2024-07-11 22:16:46

好吧,rails 实际上相当不错,但其中的魔力有点太多(从 Ruby 世界来看,我更喜欢 merb 而不是 Rails)。 我个人使用 Pylons,并且非常高兴。 我想说(与 django 相比),pylons 允许你比 django 更容易地交换 ints 内部部分。 缺点是你必须自己编写更多的东西(比如基本的 CRUD)。

使用框架的优点:

  1. 快速完成工作(我的意思是,一旦你了解了框架,速度就会快如闪电),
  2. 一切都符合标准(当你自己开发框架时,这可能不太容易实现),
  3. 更容易让事情发挥作用(有很多教程) )而不需要阅读大量的文章和文档

缺点:

  1. 你学习的
  2. 更换部件的难度较小(在 pylon 中这不是一个大问题,对于 django 来说更是如此)
  3. 调整一些低级的东西(如上面提到的 SQL)更困难

从那你可以可能会设计出它们的优点:-) 既然您获得了所有代码,就可以对其进行调整以适应最奇怪的情况(据称塔现在可以在 Google 应用程序引擎上工作......)。

OK, rails is actually pretty good, but there is just a little bit too much magic going on in there (from the Ruby world I would much prefer merb to rails). I personally use Pylons, and am pretty darn happy. I'd say (compared to django), that pylons allows you to interchange ints internal parts easier than django does. The downside is that you will have to write more stuff all by youself (like the basic CRUD).

Pros of using a framework:

  1. get stuff done quickly (and I mean lighning fast once you know the framework)
  2. everything is compying to standards (which is probably not that easy to achieve when rolling your own)
  3. easier to get something working (lots of tutorials) without reading gazillion articles and docs

Cons:

  1. you learn less
  2. harder to replace parts (not that much of an issue in pylons, more so with django)
  3. harder to tweak some low-level stuff (like the above mentioned SQLs)

From that you can probably devise what they are good for :-) Since you get all the code it is possible to tweak it to fit even the most bizzare situations (pylons supposedly work on the Google app engine now...).

揽清风入怀 2024-07-11 22:16:46

对于较小的项目,自己动手是相当容易的。 特别是,您可以简单地导入 Genshi 这样的模板引擎,然后快速轻松地完成很多事情。 有时,使用螺丝刀比去找电钻更快。

成熟的框架提供了更多的功能,但必须先安装和设置才能利用该功能。 对于较大的项目,这是一个可以忽略不计的问题,但对于较小的项目,这可能会占用您的大部分时间 - 特别是如果框架不熟悉的话。

For smaller projects, rolling your own is fairly easy. Especially as you can simply import a templating engine like Genshi and get alot happening quite quickly and easily. Sometimes it's just quicker to use a screwdriver than to go looking for the power drill.

Full blown frameworks provide alot more power, but do have to be installed and setup first before you can leverage that power. For larger projects, this is a negligible concern, but for smaller projects this might wind up taking most of your time - especially if the framework is unfamiliar.

茶色山野 2024-07-11 22:16:45

CGI 非常适合低流量网站,但对于其他网站来说,它会存在一些性能问题。 这是因为每次收到请求时,服务器都会在自己的进程中启动 CGI 应用程序。 这很糟糕,原因有两个:1)启动和停止进程可能需要时间,2)您无法在内存中缓存任何内容。 你可以使用 FastCGI,但我认为你最好直接写一个 WSGI 应用程序,如果您打算走这条路(WSGI 的工作方式实际上与 CGI 没有太大不同)。

除此之外,您的选择在很大程度上取决于您希望框架执行多少操作。 您可以使用 Django 或 Pylons 这样的全能唱歌、全能跳舞的框架。 或者您可以采用混合搭配的方法(使用 CherryPy 之类的东西来处理 HTTP 内容,使用 SQLAlchemy 来处理数据库内容,使用粘贴来进行部署等)。 我还应该指出,大多数框架还允许您为其他框架切换不同的组件,因此这两种方法不一定是相互排斥的。

就我个人而言,我不喜欢那些对我来说有太多魔力的框架,并且更喜欢混合搭配技术,但有人告诉我,我也完全疯了。 :)

你有多少网络编程经验? 如果你是初学者,我建议选择 Django。 如果您更有经验,我建议您尝试不同的方法和技术,直到找到合适的方法和技术。

CGI is great for low-traffic websites, but it has some performance problems for anything else. This is because every time a request comes in, the server starts the CGI application in its own process. This is bad for two reasons: 1) Starting and stopping a process can take time and 2) you can't cache anything in memory. You can go with FastCGI, but I would argue that you'd be better off just writing a straight WSGI app if you're going to go that route (the way WSGI works really isn't a whole heck of a lot different from CGI).

Other than that, your choices are for the most part how much you want the framework to do. You can go with an all singing, all dancing framework like Django or Pylons. Or you can go with a mix-and-match approach (use something like CherryPy for the HTTP stuff, SQLAlchemy for the database stuff, paste for deployment, etc). I should also point out that most frameworks will also let you switch different components out for others, so these two approaches aren't necessarily mutually exclusive.

Personally, I dislike frameworks that do too much magic for me and prefer the mix-and-match technique, but I've been told that I'm also completely insane. :)

How much web programming experience do you have? If you're a beginner, I say go with Django. If you're more experienced, I say to play around with the different approaches and techniques until you find the right one.

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