Understanding Python from a PHP coder's perspective

Ian Kelly ian.g.kelly at gmail.com
Mon Dec 7 16:37:23 EST 2015


On Mon, Dec 7, 2015 at 2:07 PM,  <villascape at gmail.com> wrote:
> Hello all!  Just started getting into Python, and am very excited about the prospect.
>
> I am struggling on some general concepts.  My past experience with server-side code is mostly limited to PHP and websites.  I have some file called "whatever.php", the browser accesses it, and PHP parses it and returns HTML, JSON, etc.  Every now and then, I need to run some background PHP script, or have some PHP script initiated by a CRON job, or have some PHP script initiated by the command line running in an endless loop, and while it works, feel other languages might be more appropriate.
>
> So, my interest in Python...
>
> I've read up on Python, and while some aspects seem similar to PHP, some don't.  I have learned how to create Python script, have run it from the command line, and have even accessed it with Apache by placing http://example.com/myscript.py in the browser.  I am used to seeing .php extensions, but never .py extentions, and even visited multiple sites which I knew were written in Python, but never saw the browser expose the .py extensions.  I am obviously missing something.
>
> Why don't I see the .py extension in my browser?

PHP tends to be served like CGI, which is that the URL mirrors the
path to a script that lives under a particular directory, which the
webserver locates and executes. This has some downsides:

* It exposes details about the structure of your server to the public,
which is considered a security weakness.
* An improperly configured webserver might accidentally serve scripts
or other files that are not intended to be served over the web, which
is a much bigger security risk.
* It's inflexible; if you move your script, then the URL must
necessarily change as well.

With Python, it's more usual to have a greater abstraction between
URLs and code. A set of URLs are mapped to a single Python gateway,
and the gateway dispatches the request to the appropriate request
handler. With this scheme there is no reason to have .py extensions in
the URLs because the URL structure is unrelated to how the actual
scripts are organized in the file system. This also makes it easier to
create meaningful URLs: see
https://en.wikipedia.org/wiki/Semantic_URL.



More information about the Python-list mailing list