简体   繁体   English

mod_php vs mod_python

[英]mod_php vs mod_python

Why mod_python is oop but mod_php is not ? 为什么mod_python是oop而mod_php不是?

Example :We go to www.example.com/dir1/dir2 示例:我们访问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 如果你使用mod_python apache打开www / dir1.py并调用dir2方法但是如果你使用php模块apache打开www / dir1 / dir2 / index.php

Let's talk about mod_python vs. mod_php. 我们来谈谈mod_python与mod_php。

Since the Python language is NOT specifically designed for serving web pages, mod_python must do some additional work. 由于Python语言不是专为服务网页而设计的,因此mod_python必须做一些额外的工作。

Since the PHP language IS specifically designed to serve web pages, mod_php simply starts a named PHP module. 由于PHP语言专门用于提供网页,因此mod_php只是启动一个命名的PHP模块。

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. 对于mod_python(与mod_fastcgi或mod_wsgi不同),mod_python的设计者决定调用Python的最佳方法是调用模块中定义的对象的方法。 Hopefully, that method of that object will write the headers and web page content to stdout. 希望该对象的方法将标题和网页内容写入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. 在mod_wsgi的情况下,设计者决定调用Python的最佳方法是在模块中调用函数(或可调用对象)。 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. 在mod_php的情况下,它们只是调用模块,因为PHP模块设计用于提供网页。 The program runs and the result of that run is a page of content; 程序运行,运行结果是一页内容; usually HTML. 通常是HTML。

The reason mod_php works one way, mod_python works another and mod_wsgi works a third way is because they're different. mod_php以一种方式工作的原因,mod_python工作另一种方式,mod_wsgi工作第三种方式是因为它们是不同的。 Written by different people with different definitions of the way to produce a web page. 由不同的人撰写,对网页的制作方式有不同的定义。

The php page can be object-oriented. php页面可以是面向对象的。 A mod_wsgi function may be a callable object or it may be a simplef unction. mod_wsgi函数可以是可调用对象,也可以是简单函数。 Those are design choices. 这些都是设计选择。

The mod_python requirement for a class is a design choice made by the designer of mod_python. 类的mod_python要求是由mod_python的设计者做出的设计选择。

The reason "why" is never useful. “为什么”的原因永远不会有用。 The reason "why" is because someone decided to design it that way. “为什么”的原因是因为某人决定以这种方式设计它。

Because mod_python is abstracting the URL into a "RPC-like" mechanism. 因为mod_python将URL抽象为“类RPC”机制。 You can achieve the same in PHP. 您可以在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". 请注意,mod_python行为不是强制“最好的”。 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. 据我所知,mod_python方法假定每个URL都有一个函数,比如将模块结构映射到URL树。 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. 这在技术上并不是一个特别好的方法,因为您使用的技术(mod_python)与应用程序的URL布局之间存在紧密的关联。

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. 在某些情况下,设计是品味和艺术选择的问题,而不仅仅是技术限制。

I think you have some misconceptions about how HTTP works. 我认为你对HTTP如何运作有一些误解。 Nothing in the http standard requires you to have a certain file as a resource. http标准中没有任何内容要求您将某个文件作为资源。 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. 这就是mod_php如何工作的方式,对于给定的路径,此路径被转换为服务器磁盘上的php文件,而后者又由编译器解释。

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. 另一方面, mod_python模块更通用,它允许您将任何资源映射到对某个python对象的调用。 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. 在大多数情况下,URL的调度由您的框架管理,其工作方式取决于框架实现者。

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. 由于mod_python模块的通用特性,您还可以访问mod_php模块中没有的一些apache功能,例如,您可以编写自己的身份验证处理程序,这不仅适用于您的python webapp,而且还适用于你的apache中的其他应用程序也是如此。

in PHP you can program your web pages the top-to-bottom scripts, procedural programming and function calls, OOP. 在PHP中,您可以为您的网页编写从上到下的脚本,过程编程和函数调用,OOP。 this is the main reason why PHP was first created, and how it evolved. 这是PHP首次创建的主要原因,以及它是如何发展的。 mod_php is just a module for web servers to utilize PHP as a preprocessor. mod_php只是Web服务器利用PHP作为预处理器的模块。 so it just passes HTTP information and the PHP script to PHP interpreter. 所以它只是将HTTP信息和PHP脚本传递给PHP解释器。 the PHP way of web page creation is do what you want; PHP的网页创建方式就是你想要的; 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. 编写一个从上到下的脚本,在不同的文件中定义函数并包含这些函数和调用函数,或者在OOP中编写应用程序,您现在还可以使用许多功能齐全的框架来确保您的应用程序设计和结构符合当今的最佳实践和设计模式。

I'm new to Python, and am not familiar with web programming with python. 我是Python的新手,并不熟悉使用python进行Web编程。 but as much as I know, python was not created to make web programming easier. 但据我所知,python并不是为了使Web编程更容易而创建的。 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中编写简单的从上到下的脚本并将它们作为网页响应运行(我不确定这样的事情是否可行),它是不是pythonic方式,所以我认为mod_python的开发人员希望python中的web编程采用pythonic方式。

Perhaps I misunderstand your question, but both Python and PHP support both procedural and object-oriented programming. 也许我误解了你的问题,但Python和PHP都支持程序和面向对象的编程。 (Though one could argue that Python's support for OO is the stronger of the two.) (虽然有人可能会说Python对OO的支持更强。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM