Is Python only for building backends in making websites?

4

I have seen no website built by Python such that the url of the website contains .py at the end. I have seen only index.html and index.php, but sometimes nothing as in SO. I know that you can hide the end somehow by .htaccess -file.

I would like to know the reason for that.

Why is there no website ending with .py?

Léo Léopold Hertz 준영

Posted 2009-07-26T17:17:04.917

Reputation: 4 828

5head over to stackoverflow.com for more help with python +++ – pavsaund – 2009-07-26T18:30:50.447

1

a related question is now at http://stackoverflow.com/questions/1185248/is-python-only-for-building-backends-when-you-need-to-write-sql-by-hand

– Léo Léopold Hertz 준영 – 2009-07-26T19:34:45.270

Answers

4

Typically PHP scripts are accessed directly, similarly to a CGI script - basically you access the script like any regular file, but the web server intercepts this request, runs the script and returns the output instead of the file contents.

Most Python frameworks have their own routing systems, where you define something like..

urls = (
    ('^/article/(\d+)$', ArticleController),
    ('^/user/(.+?)$', UserController)
)

..then you would access something like..

http://example.com/router.py?uri=article/123

..and it would be run through the URL mapping defined in urls, and send the request to the appropriate class... but having the router.py part in all your URLs is a bit crappy, so you use mod_rewrite to redirect /(.*) to /router.py/$1 (either via .htaccess, your Apache configuration etc)

You can absolutely do the same thing with PHP, or any other language - this is exactly how CodeIgniter works (a PHP framework)

If you are utterly insane, you could use mod_rewrite and map /user/(\d+).py to /ViewController.php?id=$1 or something, but there's generally no point to adding a fake extension to the page (there's a few exceptions, mainly backwards compatibility, and allowing access to data in different formats in an API)

You can also do the "PHP way" using Python, if you write each page as a separate CGI script, just most Python frameworks tend to use the MVC (or MVC-like) setup I described above..

dbr

Posted 2009-07-26T17:17:04.917

Reputation: 4 987

17

A good URL would be independent of technologies, so a .php or .py or .rb extension is something to be prevented. Mostly, python frameworks(like Turbogears) handle all the URL parsing and the python program that runs in the background gets all its parameters in a nice data structure, so you don't see any file extension in that case.

See "cool URIs don't change" by Tim Berners-Lee

hayalci

Posted 2009-07-26T17:17:04.917

Reputation: 1 652

8

Well, probably most people hide the extension because it is the smarter way to go. It means if you say, switched to PHP (not that you would want to...), you wouldn't have to change the extensions.

However, there are site with .py extensions (several parts of Google have it).

The better questions to ask is, why do most ASP.NET and PHP users insist on keeping the extenstions?

Zifre

Posted 2009-07-26T17:17:04.917

Reputation: 1 390

Your answer suggests me that it is easier to get rid of the extension in Python than in PHP and ASP.NET. --- Which Python module does provide the functionality? – Léo Léopold Hertz 준영 – 2009-07-26T19:53:49.283

2@Masi: There's no module that does something specific to remove the extension. In fact in Python there is no extension to be gotten rid of. To put it another way, the extension in the URL does not need to correspond to the extension of the file. So if your web app is made of .py files, there is absolutely no reason your URLs need end in .py. This might not make sense if you're used to PHP, since PHP forces the URL's extension to be the same as the file's extension (well, you can even work around that but it's not especially straightforward); it's the same in ASP I presume. – David Z – 2009-07-26T23:04:17.170

3

What is transfered to your computer is HTML (possibly with CSS and JavaScript code). CSS and JavaScript aren't displayed per se, so you don't see those extensions. HTML is the content to be displayed, so you're used to seing .html extension.

Now, who cares if the HTML document was written manually in Notepad, or dynamically put together with some programs that run just 0.1 second ago. That's no difference.

PHP is often used to create HTML documents on-the-fly this way. Adding .php extesion to HTML documents that were created by a PHP program is customary and doesn't mean much. The end user sees a HTML document in the browser.

Web server needs some information how to find a HTML document to serve - read a hand-made HTML document from disk? Create one by running some PHP program? .php extension can be used as a hint. That's only a server configuration matter.

If you are interested in serving the Web with python, look at project django

Tadeusz A. Kadłubowski

Posted 2009-07-26T17:17:04.917

Reputation: 2 005

3

Django, the (probably) most popular framework for doing webapps/websites in python, uses url-mapping to python functions, so when you request /url/ it will actually call a function that's assigned to handling requests to that url. In other words, there's not a 1-to-1 mapping between urls and .py files.

Update (per comment):

In a typical php website, the url would look something like /path/to/file.php and upon this request, the webserver runs the php file and returns its output as the HTTP Response.

In Django, a url of the form /path/of/url/ is intercepted by the framework, it doesn't actually correspond to anyfile, it's a simple a part of the HTTP Request, and the Django framework has a url-dispatching mechanism that maps a url to a normal function. If it finds a function that's assigned to handle this type of url, it will call it, and return the Http Response that the function returned.

http://docs.djangoproject.com/en/dev/topics/http/urls/

hasen

Posted 2009-07-26T17:17:04.917

Reputation: 4 556

Do you know any video/guide/code-example which demonstrates your point of 1-to-1 mapping? I would like to learn what you mean. – Léo Léopold Hertz 준영 – 2009-07-27T11:19:06.830

2

Here, you can see that Google leave the .py extension.

Loïc Wolff

Posted 2009-07-26T17:17:04.917

Reputation: 1 947

Thank you for pointing that out! -- It is still difficult to see that, since it it not at the end alone. – Léo Léopold Hertz 준영 – 2009-07-26T22:22:18.317

1

Not directly related to your question, but not only websites are created with Python. The entire AI engine for EVE online (a MMP space game) is written in Python I believe.

Toby Allen

Posted 2009-07-26T17:17:04.917

Reputation: 2 634

1

Also

Google Search .py

9th Result http://schmidt.nuigalway.ie/cs103/python/graphics.py

Thats at least one website with a file that ends in .py - there are probably more.

Toby Allen

Posted 2009-07-26T17:17:04.917

Reputation: 2 634

1That's not a website: it's a posted script. – Telemachus – 2009-07-26T19:59:26.490

This is the site: http://schmidt.nuigalway.ie/cs103/python/

– Telemachus – 2009-07-26T19:59:57.300

1

This difference is mostly caused by the way the request is dispatched.

The websites which show a file extension are mostly dispatching directly via the filesystem. When you see a URL path like /example/index.php, the web server goes to the example directory under the document root, opens the index.php file, and executes its contents (simplifying a bit). Since they are going through the filesystem, they tend to use the whole filename, including the extension.

The websites which do not show a file extension are mostly dispatching in some other way. For instance, in Django, there is (simplifying a bit) a table mapping the URL path to the code which will process the request. Since here there is no relation between the URL path and the filesystem path, the mapping can be completely arbitrary, and there is no reason to add a "fake" extension.

There are exceptions to the first case; for instance, with mod_rewrite you can remap the URL path, easily hiding any file extension (this is very common).

The reason you do not see often the first case with Python is that most people use web frameworks when building sites with Python, which do their own dispatching. This is also common even with PHP (Drupal is a common PHP framework), but creating "standalone" pages (without a framwork) is more common with PHP than with Python.

CesarB

Posted 2009-07-26T17:17:04.917

Reputation: 4 480