mod_php 与 mod_python
为什么 mod_python 是 oop 而 mod_php 不是?
示例:如果您使用 mod_python,我们将访问 www.example.com/dir1/dir2
apache 打开 www/dir1.py 并调用目录2方法 但如果你使用 php 模块 apache 打开 www/dir1/dir2/index.php
Why mod_python is oop but mod_php is not ?
Example :We go to www.example.com/dir1/dir2
if you use mod_python apache opens www/dir1.py and calls dir2 method
but if you use php module apache opens www/dir1/dir2/index.php
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我们来谈谈 mod_python 与 mod_php。
由于 Python 语言不是专门为提供网页服务而设计的,因此 mod_python 必须做一些额外的工作。
由于 PHP 语言是专门为提供网页服务而设计的,因此 mod_php 只是启动一个命名的 PHP 模块。
就 mod_python 而言(与 mod_fastcgi 或 mod_wsgi 不同),mod_python 的设计者认为调用 Python 的最佳方式是调用模块中定义的对象的方法。 希望该对象的方法会将标题和网页内容写入标准输出。
对于 mod_wsgi,设计者认为调用 Python 的最佳方式是调用模块中的函数(或可调用对象)。 希望该函数将使用提供的对象来创建标头并返回包含网页内容的列表字符串。
对于 mod_php,它们只是调用模块,因为 PHP 模块旨在为网页提供服务。 程序运行,运行的结果是一页内容; 通常是 HTML。
mod_php 以一种方式工作,mod_python 以另一种方式工作,mod_wsgi 以第三种方式工作的原因是因为它们是不同的。 由不同的人编写,对制作网页的方式有不同的定义。
php页面可以是面向对象的。 mod_wsgi 函数可以是可调用对象,也可以是简单函数。 这些都是设计选择。
mod_python 对类的要求是 mod_python 设计者做出的设计选择。
“为什么”这个理由从来没有用。 “为什么”的原因是因为有人决定这样设计。
Let's talk about mod_python vs. mod_php.
Since the Python language is NOT specifically designed for serving web pages, mod_python must do some additional work.
Since the PHP language IS specifically designed to serve web pages, mod_php simply starts a named PHP module.
In the case of mod_python (different from mod_fastcgi or mod_wsgi), the designer of mod_python decided that the best way to invoke Python is to call a method of an object defined in a module. Hopefully, that method of that object will write the headers and web page content to stdout.
In the case of mod_wsgi, the designer decided that the best way to invoke Python is to call a function (or callable object) in a module. Hopefully that function will use the supplied object to create header and return a list strings with the web page content.
In the case of mod_php, they just invoke the module because PHP modules are designed to serve web pages. The program runs and the result of that run is a page of content; usually HTML.
The reason mod_php works one way, mod_python works another and mod_wsgi works a third way is because they're different. Written by different people with different definitions of the way to produce a web page.
The php page can be object-oriented. A mod_wsgi function may be a callable object or it may be a simplef unction. Those are design choices.
The mod_python requirement for a class is a design choice made by the designer of mod_python.
The reason "why" is never useful. The reason "why" is because someone decided to design it that way.
因为mod_python正在将URL抽象成“类似RPC”的机制。 您可以在 PHP 中实现相同的目的。 我总是手动完成,但我很确定有为此预先打包的 pear 模块。
请注意,mod_python 行为并不是强制“最好”的。 这一切都取决于您想要为应用程序提供的设计类型,以及它对客户的行为方式。 据我所知,mod_python 方法假设每个 URL 都有一个函数,例如将模块结构映射到 URL 树中。 从技术上讲,这并不是一个特别好的方法,因为您使用的技术 (mod_python) 和应用程序的 URL 布局之间存在紧密的相关性。
至于原因为什么,没有任何理由。 有些人喜欢一种食物,有些人喜欢另一种食物。 在某些情况下,设计是品味和艺术选择的问题,而不仅仅是技术限制。
Because mod_python is abstracting the URL into a "RPC-like" mechanism. You can achieve the same in PHP. I always did it by hand, but I am pretty sure there are prepackaged pear modules for this.
Note the mod_python behavior is not forcibly "the best". It all amounts to the type of design you want to give to your application, and how it behaves to the clients. As far as I see, the mod_python approach assumes that for every URL you have a function, like mapping the module structure into a URL tree. This is technically not a particularly nice approach, because there's a tight correlation between the technology you are using (mod_python) and the URL layout of your application.
On the reason why, theres' no reason. Some people like one food, and some other like another. Design is, in some cases, a matter of taste and artistic choices, not only technical restrains.
我认为您对 HTTP 的工作原理有一些误解。 http 标准中没有任何内容要求您将某个文件作为资源。 这就是 mod_php 的工作方式,对于给定的路径,该路径被转换为服务器磁盘上的 php 文件,然后由编译器解释。
另一方面,mod_python 模块更加通用,它允许您将任何资源映射到对某个 python 对象的调用。 碰巧有些配置是开箱即用的,以使其更容易上手。 在大多数情况下,url 的调度由框架管理,其工作方式取决于框架实现者。
由于 mod_python 模块的通用性质,您还可以访问 mod_php 模块中不可用的一些 apache 功能,例如,您可以编写自己的身份验证处理程序,它不仅适用于您的 python web 应用程序,还适用于您的 apache 中的其他应用程序。
I think you have some misconceptions about how HTTP works. Nothing in the http standard requires you to have a certain file as a resource. It is just the way how mod_php works, that for a given path, this path is translated to a php file on the disk of the server, which in turn is interpreted by the compiler.
The mod_python module on the other hand is much more generic, it allows you to map any resource to a call to some python object. It just happens that some configurations are available out of the box, to make it easier to start with. In most cases the dispatch of the url is managed by your framework, and how that works is up to the framework implementor.
Because of the generic nature of the mod_python module you are also able to access some apache features which are not available in the mod_php module, for instance you may write your own authentication handler, which my not only apply to your python webapp, but also to other apps in your apache as well.
在 PHP 中,您可以通过自上而下的脚本、过程编程和函数调用、OOP 对网页进行编程。 这是 PHP 首次创建及其演变的主要原因。 mod_php 只是 Web 服务器使用 PHP 作为预处理器的一个模块。 所以它只是将 HTTP 信息和 PHP 脚本传递给 PHP 解释器。
PHP 的网页创建方式就是做你想做的事; 编写自上而下的脚本,在不同的文件中定义函数并包含这些函数并调用函数,或者以 OOP 编写应用程序,您现在还可以使用许多功能齐全的框架来确保您的应用程序设计和结构符合当今的最佳实践和设计模式。
我是 Python 新手,不熟悉使用 Python 进行 Web 编程。 但据我所知,Python 的创建并不是为了让 Web 编程变得更容易。 它的目的是成为一种通用编程语言,因此尽管可以在 python 中编写简单的自上而下脚本并将它们作为网页响应运行(我不确定这样的事情是否可能),但它是不是 pythonic 方式,所以我认为 mod_python 的开发人员希望 python 中的 Web 编程以 pythonic 方式进行。
in PHP you can program your web pages the top-to-bottom scripts, procedural programming and function calls, OOP. this is the main reason why PHP was first created, and how it evolved. mod_php is just a module for web servers to utilize PHP as a preprocessor. so it just passes HTTP information and the PHP script to PHP interpreter.
the PHP way of web page creation is do what you want; write a top-to-bottom script, define functions in different files and include those and call functions, or write your app in OOP, you can also use many full-featured frameworks today to make sure your application design and structure meets today best practices and design patterns.
I'm new to Python, and am not familiar with web programming with python. but as much as I know, python was not created to make web programming easier. it was intended to be a general purpose programming language, so although it might be possible to write simple top-to-bottom scripts in python and run them as web page responses (I'm not sure if such thing is possible), it is not the pythonic way, and so I think developers of the mod_python wanted web programming in python to be in a pythonic way.
也许我误解了你的问题,但Python和PHP都支持过程编程和面向对象编程。 (尽管有人可能会说 Python 对 OO 的支持是两者中更强大的一个。)
Perhaps I misunderstand your question, but both Python and PHP support both procedural and object-oriented programming. (Though one could argue that Python's support for OO is the stronger of the two.)
请参阅 PHP 5 中的类和对象
See Class and Objects in PHP 5