views:

77

answers:

5

CherryPy claims:

Your CherryPy powered web applications are in fact stand-alone Python applications embedding their own multi-threaded web server. You can deploy them anywhere you can run Python applications. Apache is not required, but it's possible to run a CherryPy application behind it (or lighttpd, or IIS). CherryPy applications run on Windows, Linux, Mac OS X and any other platform supporting Python.

Having come from PHP and wanting to learn Python, I came upon a thread here in SO while looking for a webserver I can setup to start Python web development. However, after almost googling myself to death, I can't still find one. I came across entries like "Django has its own lightweight webserver" and the aforementioned Cherrypy.

What I am confused about is this :I was used to using XAMPP, where I have a web server, a database server and my application and I can't visualize the idea of a "web server inside the application itself". How do I connect to my database server then? How do I configure stuff like custom urls and directory protection (much like what I do in Apache)?

Thanks in advance guys!

A: 

You can use something like mod_wsgi to connect httpd to your Python applications, but Python itself is powerful enough to write a web server (and even a database) in, and the Python standard library even includes a few simple servers that can be used or expanded upon.

Ignacio Vazquez-Abrams
+2  A: 

Apache is a large, rich, powerful and complicated webserver -- you can configure everything and a half, there are many plug-in modules (all the various mod_this, mod_that, etc), and so forth. That's great, but of course there are niches for smaller, lighter webservers as well -- lighttpd (which your cherrypy quote mentions) is an example of one, focusing on speed and simplicity; cherrypy is another, focusing on simplicity and Python support.

Of course you can still configure several aspects, see the tutorial section about the configuration files for a short overview, the reference for more details -- but it won't be anywhere as rich as Apache (hey, few webservers are, except maybe IIS;-). Probably some configuration options that you may feel are missing may be easily compensated with Python code, but not all -- that's why you may want to run cherrypy "behind" other servers!

The way you code your Python web apps need not be constrained by the web server you're using: just program to the WSGI standard (and that's what just about all web app frameworks these days support), and your deployment options are boundless -- from cherrypy, or even just the reference wsgi implementation that comes with the Python standard library (only recommended for development!-), all the way to Apache with mod_wsgi, IIS, even Google App Engine (it supports WSGI, too!-).

Alex Martelli
A: 

"web server inside the application itself" -- all that is meant by this is that the same python process handles both the traditional web server role of server static files (HTML, CSS, images, etc) as well as generating dynamic content produced by the application. Thus the python process itself listens for browser connections to port 80, for example, as opposed to having a separate web server process handle that part of the overall web application functionality.

Peter Lyons
A: 

I think that if you'd go through the Django tutorial, it would be a little bit more clear.

Essentially, there is a built-in web server that you use for development. You can then move your project to a "real" environment without having to make changes to it (or at least very few). Same thing goes for databases, you can start out using sqlite and when you want to deploy you application, you can change to another database like for example MySQL. This is done easily with only a few configuration changes.

Mattias Nilsson
A: 

How do I connect to my database server then? How do I configure stuff like custom urls and directory protection (much like what I do in Apache)?

You still have a separate database server, like Postgres or MySQL. Your interface can be either one of the Python database interfaces or an ORM, like in PHP. Doing stuff like custom URLs is handled by the framework. I can't comment on CherryPy but in Pylons there is a class that configures routing, configured in routing.py.

Think about the difference like this:

A PHP framework like Symfony expects to be entered when *.php is invoked by your web server. That's the interface to your application: Symfony creates an index.php which, when invoked, initializes the different framework services such as routing and the ORM. It parses the URI and figures out which controller/action pair should receive the request.

A Python framework, like Pylons, comes with a web server -- an application which listens for HTTP requests on a configured port. That server, when it receives a request, does the same thing. It creates a database connection and uses the routing map configured for your application to figure out where to send the request. Unlike the PHP framework, it also will check to see if the URI references a static file and returns that instead if it's configured to do so. In the LAMP environment that's the purview of Apache.

Jesse Dhillon
CherryPy mirrors the class and method structure as paths (ie. `myclassone.some_method(...)` might be mapped to `/one/some_method` and `myclasstwo.some_other_method(...)` might be mapped to `/two/some_other_method`). It's more complicated than just that, but you get the idea hopefully.
detly