使用 python 构建 Web 应用程序

发布于 2024-07-09 09:22:22 字数 711 浏览 4 评论 0原文

这是两个的后续问题 我大约一周前问过。 结果是我正在构建一个基于人工智能的网络应用程序原型,我想知道该使用什么语言。 结论似乎是我应该选择像 python 这样的东西,然后将任何关键位转换成更快的东西,比如 Java 或 C/C++。

这对我来说听起来不错,但我现在想知道 python 是否真的是构建 Web 应用程序的正确语言。 我过去开发过的大多数 Web 应用程序都是 C/C++ CGI,然后是 php。 我发现 php 更容易使用,因为它使用户界面与后端的链接变得更加容易,而且对我来说也更具逻辑意义。

我以前没有使用过 python,但我基本上想知道 python 中的 CGI 编程有多容易? 我是否必须回到在 C/C++ 中执行此操作的繁琐方式,即必须将 HTML 代码存储在模板中并让 CGI 读取它们并用适当的值替换特殊代码,或者是否可以使用模板 与 php 一样的代码吗?

我可能在这里问了一个非常无知的问题,对此我表示歉意,但希望有人会知道我在说什么! 我的总体问题是:用 python 编写 Web 应用程序是一个好主意吗?它和用 php 一样容易吗?

This is a follow-up to two questions I asked a week or so back. The upshot of those was that I was building a prototype of an AI-based application for the web, and I wondered what language(s) to use. The conclusion seemed to be that I should go for something like python and then convert any critical bits into something faster like Java or C/C++.

That sounds fine to me, but I'm wondering now whether python is really the right language to use for building a web application. Most web applications I've worked on in the past were C/C++ CGI and then php. The php I found much easier to work with as it made linking the user interface to the back-end so much easier, and also it made more logical sense to me.

I've not used python before, but what I'm basically wondering is how easy is CGI programming in python? Will I have to go back to the tedious way of doing it in C/C++ where you have to store HTML code in templates and have the CGI read them in and replace special codes with appropriate values or is it possible to have the templates be the code as with php?

I'm probably asking a deeply ignorant question here, for which I apologise, but hopefully someone will know what I'm getting at! My overall question is: is writing web applications in python a good idea, and is it as easy as it is with php?

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

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

发布评论

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

评论(4

甲如呢乙后呢 2024-07-16 09:22:22

Python是一个不错的选择。

不过,我会避免使用 CGI 模型 - 您将为每个请求启动解释器付出很大的代价。 大多数 Python Web 框架都支持 WSGI 标准,并且可以通过多种方式连接到服务器,但大多数都存在于某种与 Web 服务器通信的长时间运行的进程中(通过代理、FastCGI、SCGI 等)。

说到框架,Python 环境已经成熟。 这既是好事也是坏事。 有很多不错的选择,但对于新手来说可能会令人望而生畏。

如果您正在寻找预先打包了 web/DB/templateting 集成的东西,我建议您查看 DjangoTurboGears塔架。 如果您想对各个组件有更多的控制,请查看 CherryPyColubridweb.py。

至于是否像PHP一样简单,那是主观的。 通常,我们鼓励在 Python Web 编程世界中将模板和应用程序逻辑分开,这可以让您的生活更轻松。 另一方面,能够在 PHP 文件中编写页面的所有代码是“简单”的另一个定义。

祝你好运。

Python is a good choice.

I would avoid the CGI model though - you'll pay a large penalty for the interpreter launch on each request. Most Python web frameworks support the WSGI standard and can be hooked up to servers in a myriad of ways, but most live in some sort of long-running process that the web server communicates with (via proxying, FastCGI, SCGI, etc).

Speaking of frameworks, the Python landscape is ripe with them. This is both good and bad. There are many fine options but it can be daunting to a newcomer.

If you are looking for something that comes prepackaged with web/DB/templating integration I'd suggest looking at Django, TurboGears or Pylons. If you want to have more control over the individual components, look at CherryPy, Colubrid or web.py.

As for whether or not it is as "easy as PHP", that is subjective. Usually it is encouraged to keep your templates and application logic separate in the Python web programming world, which can make your life easier. On the other hand, being able to write all of the code for a page in a PHP file is another definition of "easy".

Good luck.

糖果控 2024-07-16 09:22:22

“Python 中的 CGI 编程有多容易?” 比 C 简单,这是肯定的。 Python 更容易,因为——简单地说——它是一种比 C 更容易使用的语言。首先也是最重要的:没有内存分配-释放。 除此之外,OO 编程模型非常出色。

除了基本的语言简单性之外,Python WSGI 标准比 CGI 标准更容易处理。

然而,与全 Python 框架的大大简化的世界相比,原始 CGI 是一个巨大的痛苦(TurboGears,< a href="http://www.cherrypy.org/" rel="nofollow noreferrer">CherryPy, Django,无论如何。)

框架强加了很多(必要的)结构。 对于 CGI 程序员来说,开箱即用的体验是需要学习的东西太多了。 真的。 所有新事物都需要学习太多。 然而,其价值远远超过投资。

使用 Django,您可以在几分钟内启动并运行。 严重地。 django-admin.py startproject 并且您几乎可以立即运行一些东西。 您必须设计 URL、编写视图函数并设计页面模板。 所有这些都是工作。 但它比 C 语言中的 CGI 工作量。Django

具有比 PHP 更好的架构,因为演示模板与处理完全分离。 这会导致一些混乱(请参阅 语法错误当你想在 Django 框架上使用自由无约束的 PHP 风格时,我将 python 代码放在 django 模板中)。

将用户界面链接到后端

Python 前端(例如 Django)使用 Python 视图函数。 这些视图函数可以包含任何 Python 代码。 如有必要,这包括用 C 编写并可从 Python 调用的模块。

这意味着您可以使用 Python 友好的界面编译 CLIPS 模块。 通过 import 语句,它就成为 Python 代码可用的东西。

然而,有时这是无效的,因为您的 Django 页面正在等待 CLIPS 引擎完成。 另一种方法是使用命名管道之类的东西。

您拥有基于 CLIPS 的应用程序,完全用 C 编写,从命名管道读取。 您的 Django 应用程序完全用 Python 编写,写入该命名管道。 由于您有两个独立的进程,因此您将像这样很快地最大化所有核心。

"how easy is CGI programming in python?" Easier than C, that's for sure. Python is easier because -- simply -- it's an easier language to work with than C. First and foremost: no memory allocation-deallocation. Beyond that, the OO programming model is excellent.

Beyond the essential language simplicity, the Python WSGI standard is much easier to cope with than the CGI standard.

However, raw CGI is a huge pain when compared with the greatly simplified world of an all-Python framework (TurboGears, CherryPy, Django, whatever.)

The frameworks impose a lot of (necessary) structure. The out-of-the-box experience for a CGI programmer is that it's too much to learn. True. All new things are too much to learn. However, the value far exceeds the investment.

With Django, you're up and running within minutes. Seriously. django-admin.py startproject and you have something you can run almost immediately. You do have to design your URL's, write view functions and design page templates. All of which is work. But it's less work than CGI in C.

Django has a better architecture than PHP because the presentation templates are completely separated from the processing. This leads to some confusion (see Syntax error whenever I put python code inside a django template) when you want to use the free-and-unconstrained PHP style on the Django framework.

linking the user interface to the back-end

Python front-end (Django, for example) uses Python view functions. Those view functions can contain any Python code at all. That includes, if necessary, modules written in C and callable from Python.

That means you can compile a CLIPS module with a Python-friendly interface. It becomes something available to your Python code with the import statement.

Sometimes, however, that's ineffective because your Django pages are waiting for the CLIPS engine to finish. An alternative is to use something like a named pipe.

You have your CLIPS-based app, written entirely in C, reading from a named pipe. Your Django application, written entirely in Python, writes to that named pipe. Since you've got two independent processes, you'll max out all of your cores pretty quickly like this.

旧时模样 2024-07-16 09:22:22

我建议 Django,但考虑到你要求的东西“就像使用 php 一样简单" 那么你必须看看 PSP ( Python 服务器页面)。
虽然Django是一个完整的做网站的框架,但PSP可以像PHP一样使用,不需要任何框架。

I would suggest Django, but given that you ask for something "as easy as it is with php" then you must take a look at PSP (Python Server Pages).
While Django is a complete framework for doing websites, PSP can be used in the same way than PHP, without any framework.

长发绾君心 2024-07-16 09:22:22

用 python 编写网络应用程序比用 php 更容易。 特别是因为 python 并不是一门蹩脚的语言。

选择一些支持 mod_wsgi 的 Web 框架或推出您自己的框架。 当您掌握了 WSGI 应用程序后,部署起来非常容易。

如果你想要模板,那么 genshi 是我为 Python 找到的最好的模板引擎,你可以随心所欲地使用它。

It is easier to write web-apps in python than it's in php. Particularly because python is not a broken language.

Pick up some web framework that supports mod_wsgi or roll out your own. WSGI apps are really easy to deploy after you get a hold from doing it.

If you want templates then genshi is about the best templating engine I've found for python and you can use it however you like.

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