
Python Web Programming - plessthanpt05
http://docs.python.org/release/3.1.5/howto/webservers.html
======
DizzyDoo
A good article, but I don't want the Flask web framework
(<http://flask.pocoo.org/>) to go unnoticed. I struggled along with Django for
a number of months, and since I was just beginning web development I felt like
I was fighting the large framework, even to build the simplest app. Upon
discovering Flask I found it refreshingly simple, and when I moved on to more
complicated applications I found it scaled beyond 'toy' apps. It really is
worth a look for any Python developer.

~~~
teilo
I've used both Flask and Django for apps that are both simple and complex.
What I have discovered, is that if I use Flask for a complex app, I need to
organize my code in clean components that end up looking like Django. Yes, I
can do this more-or-less in a freeform manner with Flask, and even use the
"one file for everything" method, but for a complex app, I wouldn't want to.
It's too hard to manage.

So, in the end, I end up mimicking the basic structure of a Django project:
Models, Views, Forms, Utilities, etc. Yes, different ORM, different Form
library, WSGI middleware instead of context processors, etc., but all-in-all,
no less complex than had I used Django. It's really to the point that I find
it hardly any faster to code in Flask for all but the most basic sites.

I still use Flask for super simple projects that require no Admin interface,
such as Personalized URL sites for ad campaigns. So it certainly has it's
place.

~~~
lucian1900
I'd say getting SQLAlchemy instead of the terrible Django ORM is enough of an
advantage.

However, I have also found the tools around Django's ORM to be quite useful
(admin, south).

~~~
ashconnor
You can use SQLAlchemy with Django.

The part I really like about Flask is the decorators for routes. This is how
it should be, not some messy file of reg-exes.

~~~
lucian1900
You can sort of maybe use SA with Django, except when all the apps you use
depend on it and you have to use SA reflection to get at your data. Sadly, the
integration is poor.

I totally agree about the regexes: only a small subset of all routes will ever
need regexes, and you can do it explicitly in Flask when needed.

------
davepeck
It's worth pointing to Graham Dumpleton's blog [1] -- he seems to be the
Python world's web hosting ninja. See the great slide deck [2] from his recent
talk on hosting python web apps [3] -- the deck nicely captures the recent
state of the world.

Apache2 + mod_wsgi in Daemon mode is probably the most common approach
(especially for VPSs), although I know several people who prefer nginx +
gunicorn + supervisor. Both are sane and stable options at this point. (The
python web frameworks themselves all speak WSGI and there are about seventeen
million of 'em to choose from.)

[1] <http://blog.dscpl.com.au/>

[2] [http://modwsgi.googlecode.com/files/mod_wsgi-pycon-
sydney-20...](http://modwsgi.googlecode.com/files/mod_wsgi-pycon-
sydney-2011.pdf)

[3] <http://www.youtube.com/watch?v=PWIvm-uloMg>

------
streptomycin
Seems to me that a better title for this article would be "A history of
different ways to use Python in the web" rather than "HOWTO Use Python in the
web", since most of the article is spent describing old things that should not
be used for new projects.

~~~
LeonidasXIV
Author here: While I agree that part of this guide are indeed outdated and
could be worth an update (which I haven't had the time to since some years) I
still think the general basics are useful. I saw many people get into web
programming without having a slightest clue how HTTP works, sometimes even
after coming from PHP.

------
brendn
Interesting that there's no mention of Tornado [1]. (Disclosure: as a co-
author of O'Reilly's _Introduction to Tornado_ , I'm biased in its favor.) I
haven't played around with Brubeck [2] yet, but I also hear many great things
about it.

Needless to say, there are some great Python web servers and frameworks out
there that aren't listed on that page.

[1] <http://www.tornadoweb.org/>

[2] <http://brubeck.io/>

Edit: Added links

~~~
kzahel
I'm also confused. Tornado is definitely a more compelling web framework. Who
doesn't want the ability to have tons and tons and tons of long-lived
connections?

~~~
slurgfest
Tornado also locks you in to its server completely. That should not be a
problem if you are 100% sure it is what you will be using in the future

~~~
brendn
Tornado relies on its own HTTP server, but its modules for everything from
templating to authentication are loosely coupled, so you can take them or
leave them.

To your point, I'd be curious to see a non-Tornado server that is Tornado
compatible on the backend. It could be an interesting project. I would
counter, however, that any server choice has its own inertia--changing web
servers is hardly ever a simple process.

------
Kilimanjaro
Bottle.py is the simplest way to start playing with web programming in python.
Mix it with Jinja and there you have it, good enough for any personal project.
Then you make your way up with Flask and Django.

* I wish there was a one-file jinja copycat to make it easier to just drop bottle and jinja and start hacking.

~~~
derelk
I agree Bottle is simple and has some nice features, but I'm a little wary of
the decorator-based "micro"frameworks. In my experience it can lead to strange
code structure and it's more difficult to translate into the class-based
handlers of larger frameworks.

Moreover, I don't think the barrier of entry to use Tornado, for example, is
significantly higher than Bottle, and you can use it for any sized project. I
realize that is both a server and framework, but truthfully I guess I'm also
wary of WSGI on the whole and wouldn't recommend anyone even start down that
rabbit hole in this day and age.

------
domodomo
This reminds me, whatever happened to Turbogears? Are many people using it? I
never really hear people mention using it compared to Django.

I thought long and hard about the "Should I learn Django or Turbogears"
question 3 or four years ago, and went with Django. Back then, Django was not
yet the "default" answer.

~~~
OptimusSubprime
The TG, Pylons, and Zope guys all joined forces under the Pyramid banner a
couple of years ago. There are still people developing in each of those three
projects (and I think it's largely still maintained), but the definite trend
is to Pyramid itself.

------
mistercow
It's a real shame that support for Python Server Pages is as rare as it is in
the wild, and that we don't see that kind of capability common in other server
side languages. I really think that the ability to mix HTML and server code
together painlessly is one of the main advantages that PHP still has today.
It's the reason I still use it for new projects even though I dislike almost
everything about the language itself.

If I could just say <?coffeescript like that, my programmer-happiness rating
would quadruple instantly.

~~~
pconf
I'd love to see Python Server Pages as well but not implemented anything like
it is in PHP. Mixing View and MVC Controller methods/classes in View pages is
simply not a sustainable model. Following the PHP example here would seriously
taint Python's potential for webapps. Java's JSP, OTOH, is the reference model
for that.

One big plus for Python Server Pages is the ability to pre-compile into
bytecode. True Python doesn't have near the level of compile-time checking
that Java does, but it that could be written into a framework...

------
F_J_H
What would be extremely useful is an equivalent of Michael Hartl's "Ruby on
Rails Tutorial" (<http://ruby.railstutorial.org/ruby-on-rails-tutorial-book>)
for one of the more popular Python web frameworks...

If someone knows of something similar, please let me know.

~~~
slurgfest
Did you try Googling "Django tutorial"?

<https://docs.djangoproject.com/en/dev/intro/tutorial01/>

If you don't like that one, there are even more:
<https://code.djangoproject.com/wiki/Tutorials>

------
n2dasun
I know nothing about python web programming, but I'm hoping to get deep into
it very soon. I wanted to point out the framework that I'm planning to use,
because I hadn't seen it mentioned in this thread yet.

<http://www.web2py.com/>

------
phzbOx
Why is there a link to python's documentation on HN frontpage? I mean, it's
not like it's a new release or, wait for it, was anything _new_.

Here are some others:

    
    
       - Python/C API Reference Manual http://docs.python.org/py3k/c-api/
       - Higher-order functions and operations on callable objects http://docs.python.org/library/functools.html
       - Google http://www.google.com

------
jonathansizz
This link might help you all out:
<http://docs.python.org/release/3.1.5/index.html> or are we going to
eventually have every single Python doc page posted to HN?

It there's no Python news, that doesn't mean you should post random tutorials
instead. This is getting tiresome. Flagged.

~~~
slurgfest
If it's not useful to post random tutorials, how much less useful is it to
post links to documentation for obscure Python versions which hardly anybody
uses? Do you even use Python for anything?

------
mattdeboard
Great timing on this. I'm training a very smart & academically accomplished
new dev who's spent the last several years pursuing his PhD in CS, but has no
idea about web development (I don't consider that a vice).

Fortunately he's one of the awesome, awesome people I can just point at an
article and he'll be off and running.

------
RossM
> Most HTTP servers are written in C or C++, so they cannot execute Python
> code directly – a bridge is needed between the server and the program.

Are there any HTTP servers written in Python (rather than modules)? I'm
struggling to think of any.

~~~
seanwoods
CherryPy, Tornado, maybe others...

~~~
RossM
Ah, I thought Tornado was a framework. Will investigate a bit more.

~~~
bdarnell
It's both. The server and the framework are to some degree usable
independently from one another (via the tornado.wsgi module), but the most
interesting features (i.e. all the asynchronous stuff) only work when both are
used together.

A couple of other pure-python (I think) HTTP servers that haven't come up in
this thread yet are twisted and paster.

------
jwallaceparker
Great resource.

As a side note, I find it interesting that the Django Book does not contain a
chapter on test writing.

<http://www.djangobook.com/>

------
jefe78
Why is something like this being posted? It's been in the docs for ages. Seems
like link bait to all the folks drooling over Python lately(of which I am).

~~~
arethuza
I don't know why it was posted, but as a recent convert to Python (and using
CherryPy for web development) I'm interested to see what else is out there in
the Python ecosystem.

