用于 Web 脚本编写的 Python

发布于 2024-08-11 16:50:00 字数 290 浏览 13 评论 0原文

我刚刚开始使用 Python,到目前为止已经在 IDLE 界面中进行了练习。现在我想使用 MAMP 配置 Python,这样我就可以开始创建真正基本的 Web 应用程序 - 在 HTML 中使用 Python,或者反之亦然。 (我假设 Python 中允许使用 HTML,就像 PHP 一样?如果不允许,是否有任何模块/模板引擎?)

我需要安装哪些模块才能从本地主机运行 .py?谷歌搜索一下,似乎有多种方法 - mod_python、FastCGI 等。我应该使用哪一种以及如何使用 MAMP Pro 1.8.2 安装它?

非常感谢

I'm just starting out with Python and have practiced so far in the IDLE interface. Now I'd like to configure Python with MAMP so I can start creating really basic webapps — using Python inside HTML, or well, vice-versa. (I'm assuming HTML is allowed in Python, just like PHP? If not, are there any modules/template engines for that?)

What modules do I need to install to run .py from my localhost? Googling a bit, it seems there're various methods — mod_python, FastCGI etc.. which one should I use and how to install it with MAMP Pro 1.8.2?

Many thanks

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

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

发布评论

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

评论(3

茶底世界 2024-08-18 16:50:00

我认为最简单的入门方法可能是使用 Django< /a>.它是一个自上而下的 Web 开发堆栈,为您提供开发和运行后端服务器所需的一切。在那个世界里事情可以非常简单,除非你真的有需要,否则不需要乱用 mod_python 或 FastCGI。

它也很好,因为它符合 WSGI,这是一个 Python 标准,允许您将不相关的部分插入在一起可重用代码,用于在需要时向您的 Web 应用程序添加特定功能(例如 on-the- Fly gzip 压缩,或 OpenID 身份验证)。一旦你已经超出了默认的 Django 堆栈,或者想要更改特定的内容,如果你愿意,你可以沿着这条路走。

这些是帮助您入门的一些提示。您还可以查看其他替代框架,例如 TurboGears粘贴 如果你愿意的话,但是 Django 是快速启动和运行某些东西的好方法。不管怎样,我相信您会喜欢这种体验:WSGI 使您能够通过在 Web 上找到的丰富的 Python 代码构建 Web 应用程序成为一种真正的乐趣。

[编辑:如果您遇到问题,您可能会发现在 stack-overflow 上浏览一些可能Django 相关问题会很有帮助问题]

I think probably the easiest way for you to get started is to work with something like Django. It's a top-to-bottom web development stack which provides you with everything you need to develop and run a backend server. Things can be very simple in that world, no need to mess around with mod_python or FastCGI unless you really have the need.

It's also nice because it conforms to WSGI, which is a Python standard which allows you to plug together unrelated bits of reusable code to add specific functionality to your web app when needed (say for example on-the-fly gzip compression, or OpenID authentication). Once you have outgrown the default Django stack, or want to change something specific you can go down this road if you want.

Those are a few pointers to get you started. You could also look at other alternative frameworks such as TurboGears or paste if you wanted but Django is a great way to get something up and running quickly. Anyway, I'm sure you'll enjoy the experience: WSGI makes it a real joy knocking up web apps with the wealth of Python code you'll find on the web.

[edit: you may find it helpful to browse some of the may Django related questions here on stack-overflow if you run into problems]

苄①跕圉湢 2024-08-18 16:50:00

你问Python中是否允许HTML,这表明你仍然用PHP的术语思考太多。与 PHP 不同,Python 并不是为创建动态网页而设计的。相反,它被设计为一种独立的通用编程语言。因此您将无法将 HTML 放入 Python 中。有一些模板库允许您以相反的方式进行操作,但这是一个完全不同的问题。

使用 Django 或 TurboGears 或所有其他 Web 框架,您基本上设置了一个小型独立的 Web 服务器(它与框架捆绑在一起,因此您无需执行任何操作),告诉服务器哪个功能应该处理什么URL然后编写那些函数。在最简单的情况下,您指定的每个 URL 都有其自己的功能。

该“处理函数”(或 Django 术语中的“视图函数”)接收一个请求对象,其中包含有关刚刚接收到的请求的有趣信息。然后它会执行所需的任何处理(例如数据库查询)。最后,它产生一些输出,返回给客户端。获取输出的典型方法是将一些数据传递到模板,在模板中数据与一些 HTML 一起呈现。

因此,HTML 被分隔在模板中(在典型情况下),而不是在 Python 代码中。

关于 Python 3:我想您会发现世界上绝大多数的 Python 开发仍然使用 Python 2.*。正如其他人在这里指出的那样,Python 3 才刚刚问世,大多数好东西还没有可用,你不应该为此烦恼。

我的建议:掌握 Python 2.6 和 Django 1.1 并投入其中。这很有趣。

You asked whether HTML is allowed within Python, which indicates that you still think too much in PHP terms about it. Contrary to PHP, Python was not designed to create dynamic web-pages. Instead, it was designed as a stand-alone, general-purpose programming language. Therefore you will not be able to put HTML into Python. There are some templating libraries which allow you to go the other way around, somewhat, but that's a completely different issue.

With things like Django or TurboGears or all the other web-frameworks, you essentially set up a small, stand-alone web-server (which comes bundled with the framework so you don't have to do anything), tell the server which function should handle what URL and then write those functions. In the simplest case, each URL you specify has its own function.

That 'handler function' (or 'view function' in Django terminology) receives a request object in which interesting info about the just-received request is contained. It then does whatever processing is required (a DB query for example). Finally, it produces some output, which is returned to the client. A typical way to get the output is to have some data passed to a template where it is rendered together with some HTML.

So, the HTML is separated in a template (in the typical case) and is not in the Python code.

About Python 3: I think you will find that the vast majority of all Python development going on in the world is still with Python 2.*. As others have pointed out here, Python 3 is just coming out, most of the good stuff is not available for it yet, and you shouldn't be bothered about that.

My advise: Grab yourself Python 2.6 and Django 1.1 and dive in. It's fun.

疯到世界奔溃 2024-08-18 16:50:00

Django 绝对不是最简单的方法。

检查塔架。 http://pylonshq.com/

还可以检查 sqlalchemy 中与 sql 相关的内容。非常酷的图书馆。

另一方面,您始终可以从非常简单的东西开始,例如用于模板的 mako。 http://www.makotemplates.org/

Django is definitely not the easiest way.

check out pylons. http://pylonshq.com/

also check sqlalchemy for sql related stuff. Very cool library.

On the other hand, you can always start with something very simple like mako for templating. http://www.makotemplates.org/

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