34

I know the various frameworks have their benefits, but I personally want my web development in python to be as straight-forward as possible: less writing to the framework, more writing python.

The only thing I have found so far that lets me do this in the most obvious way possible is web.py but I have slight concerns on its performance.

For those of you using nginx(or another flavour)+mod_wsgi+web.py... how's performance? Can it be improved further?

For those of you who have used web.py, liked the idea and went on to write something better or found something better... care to point me to the source?

I'd like to hear about all the conspicuous, minimal yet powerful approaches.

Shog9
  • 156,901
  • 35
  • 231
  • 235
  • 2
    Web development with Django IS straight-forward. Sorry, had to do it. I don't understand what's not to love about Django. – Paolo Bergantino Feb 27 '09 at 21:22
  • If you make it less insulting you probably won't get downvoted as much. – Malfist Feb 27 '09 at 21:23
  • How is this post insulting? It's certainly not offensive. – Blorgbeard Feb 27 '09 at 21:25
  • "nonsense" is a pretty loaded word. – Brian Neal Feb 27 '09 at 21:27
  • "I don't want to write django. I want to write python."? I don't know what "Django" is in this context. Please clarify your question with some description of what "Django" is and how it differs from Python. – S.Lott Feb 27 '09 at 21:28
  • Used to be nonsense meant you didn't understand something. Times change. –  Feb 27 '09 at 21:29
  • Sorry, wrong word, condescending. He's telling us that he knows more than us (i.e. tell us that Django isn't straight forward, and that the frameworks are nonsense), and then asks us for advice. Even the vocabulary is militant, e.g. 'preemptive strike' No it's not offensive, but condescending – Malfist Feb 27 '09 at 21:30
  • @blorgbeard: It's not offensive but it comes off as pretty arrogant/rude. I didn't mark it offensive but yeah... – Paolo Bergantino Feb 27 '09 at 21:30
  • I think you guys are getting worked up over nothing. He didn't swear or say anything offensive and being arrogant isn't a crime (yet). If you think frameworks are the way to go then maybe this isn't a question for you. – cbrulak Feb 27 '09 at 21:34
  • @Paolo: first comment: any reasons why? – cbrulak Feb 27 '09 at 21:35
  • @Shog9: nice edit, question is much improved – Cal Jacobson Feb 27 '09 at 21:37
  • @starko: he was essentially talking down to anyone who might be willing to help him understand/learn to use a framework. Which is stupid and rude, even if he really *does* just want to write CGI scripts. There are ways to express your desires without slamming things you found inappropriate. – Shog9 Feb 27 '09 at 21:37
  • @starko: any reason why what? Why I think its straight forward? Well, I'd never touched Python/Django before and I decided to do my latest project in it and granted I had a few hiccups but my development time has been blazing fast, docs have been helpful, and everything seems intuitive to me. – Paolo Bergantino Feb 27 '09 at 21:37
  • @Shog9. 1) Look at his rep. He is obviously new. I think we can give people a break when they have a low rep like that. 2)I don't think he is talking down. Maybe a bit too angisty 3) Your edit makes the question much nicer, so thanks for cleaning that up. – cbrulak Feb 27 '09 at 21:40
  • @starko: IMO, rep should have nothing to do with it -- the SO FAQ is available to all. The original question was not offensive, but it had an unpleasant tone to it that, had it not been edited, would have likely condemned it to a quiet death instead of having a chance for an answer. – Cal Jacobson Feb 27 '09 at 21:47
  • @Cal: Agree. However, you can still see that this person is relatively new and a gently nudge to the FAQ or a clear edit like Shog9s is much more helpful and professional than getting all worked up like others have – cbrulak Feb 27 '09 at 21:49

10 Answers10

39

The way to go is wsgi.

WSGI is the Web Server Gateway Interface. It is a specification for web servers and application servers to communicate with web applications (though it can also be used for more than that). It is a Python standard, described in detail in PEP 333.

All current frameworks support wsgi. A lot of webservers support it also (apache included, through mod_wsgi). It is the way to go if you want to write your own framework.

Here is hello world, written to wsgi directly:

def application(environ, start_response):
    status = '200 OK'
    response_headers = [('Content-type','text/plain')]
    start_response(status, response_headers)
    return ['Hello world!\n']

Put this in a file.py, point your mod_wsgi apache configuration to it, and it will run. Pure python. No imports. Just a python function.

If you are really writing your own framework, you could check werkzeug. It is not a framework, but a simple collection of various utilities for WSGI applications and has become one of the most advanced WSGI utility modules. It includes a powerful debugger, full featured request and response objects, HTTP utilities to handle entity tags, cache control headers, HTTP dates, cookie handling, file uploads, a powerful URL routing system and a bunch of community contributed addon modules. Takes the boring part out of your hands.

nosklo
  • 217,122
  • 57
  • 293
  • 297
20

It's hilarious how, even prompted with a question asking how to write without a framework, everyone still piles in to promote their favourite framework. The OP whinges about not wanting a “heavyweight framework”, and the replies mention Twisted, of all things?! Come now, really.

Yes, it is perfectly possible to write straight WSGI apps, and grab bits of wanted functionality from standalone modules, instead of fitting your code into one particular framework's view of the world.

To take this route you'll generally want to be familiar with the basics of HTTP and CGI (since WSGI inherits an awful lot from that earlier specification). It's not necessarily an approach to recommend to beginners, but it's quite doable.

I'd like to hear about all the conspicuous, minimal yet powerful approaches

You won't hear about them, because no-one has a tribal interest in promoting “do it yourself” as a methodology. Me, I use a particular standalone templating package, a particular standalone form-reading package, a particular data access layer, and a few home-brew utility modules. I'm not writing to one particular philosophy I can proselytise about, they're all just boring tools that could be swapped out and replaced with something else just as good.

bobince
  • 528,062
  • 107
  • 651
  • 834
  • I disagree. mod_python and pss isn't a framework, it's more like do it yourself. See my answer below. – user26294 Feb 27 '09 at 22:17
  • 2
    "... templating package, ... form-reading package, a ... data access layer" Names please. Generalities don't help the rest of us. – S.Lott Feb 28 '09 at 00:24
  • So you are using a few stand-alone packages for various things. Sounds like you are using a framework after all. :) – Brian Neal Feb 28 '09 at 01:39
  • 1
    @S.Lott: pxtl, htmlform (or occasionally plain old cgi), and various-on-different-projects (SQLObject, datatalk, ...). But it's not really important what they are; they have no knowledge of each other and any one has a dozen potential replacements. – bobince Feb 28 '09 at 08:34
9

You could also check cherrypy. The focus of cherrypy is in being a framework that lets you write python. Cherrypy has its own pretty good webserver, but it is wsgi-compatible so you can run cherrypy applications in apache via mod_wsgi. Here is hello world in cherrypy:

import cherrypy

class HelloWorld(object):
    def index(self):
        return "Hello World!"
    index.exposed = True

cherrypy.quickstart(HelloWorld())
nosklo
  • 217,122
  • 57
  • 293
  • 297
  • I also started with web.py and moved on to CherryPy. It feels a lot like web.py in that it feels like I'm writing Python code instead of framework code, but is much more feature-rich for the occasional fancy thing I need to do. – Eli Courtwright Feb 27 '09 at 21:59
8

+1 to all the answers with WSGI.

Eric Florenzo wrote a great blog post lately you should read: Writing Blazing Fast, Infinitely Scalable, Pure-WSGI Utilities. This will give you a better idea of pure WSGI beyond Hello World. Also pay attention to the comments, especially the first comment by Kevin Dangoor where he recommends at least adding WebOb to your toolset.

Van Gale
  • 43,536
  • 9
  • 71
  • 81
3

For what it's worth, I wrote my website in mod_python without any intervening framework like Django. I didn't really have any reason to complain. (Well maybe a little, mod_python is kind of quirky in a few ways but not in the common use cases) One thing's for sure, it will definitely let you write Python ;-)

David Z
  • 128,184
  • 27
  • 255
  • 279
  • 1
    mod_python is in notoriously bad taste. Using wsgi and mod_wsgi is far better. Personally, I got my start doing CGI, which is also definitely pure Python, no framework, nothing extra. – Devin Jeanpierre Feb 27 '09 at 21:35
  • -1 for recommending brain-dead mod_python. mod_wsgi is the way to go. mod_wsgi is a PEP standard. – nosklo Feb 27 '09 at 21:40
  • I didn't recommend mod_python. I simply said I used it and it worked well for me. I wouldn't challenge the assertion that mod_wsgi is better, but I've never used it. – David Z Feb 27 '09 at 21:43
  • @David: yes, the guy is a newbie and was pointed in the wrong direction, so my negative vote stands. To use mod_python is a mistake, unless you are developing an apache extension (not a web application) – nosklo Feb 27 '09 at 21:52
  • The big thing is that mod_cgi reload the script every times, thus this is not really effective. mod_wsgi keeps the resources loaded. – jeromej Aug 27 '13 at 02:17
2

Why do you have concerns about web.py's performance? As I mentioned here, we use CherryPy (the web server "built into" web.py) behind nginx to serve most of the HTML at Oyster.com -- nginx splits the traffic across 2 or 3 web servers each running 4 Python processes, and we can easily handle 100s of requests per second.

Oyster.com is a high-volume website averaging 200,000 dynamically-generated pageviews/day, and peaking to much higher numbers than that. However, we do use a content delivery network (CDN) for our static resources like images and CSS.

We definitely care about performance (most of our pages render in less than 25ms), but web.py isn't the bottleneck. Our bottlenecks are template rendering (we use Cheetah, which is fast enough but not other-worldly fast) and database queries (we cache heavily and keep the number of database queries per page to 0 or 1) and accessing our 3rd-party hotel pricing providers (these are accessed when you do a search with dates we don't already have cached).

Remember, premature optimization is the root of all evil -- unless you're serving google.com, web.py will probably work for you.

Community
  • 1
  • 1
Ben Hoyt
  • 10,694
  • 5
  • 60
  • 84
1

I've written a few small web applications using mod-python and PSP -- mod-python's equivalent to php.

In one case, I wrote a web page that generates release notes by inspecting our source code repository. I rewrote it into PHP, and was surprised to discover that the PSP version was about 20% faster, as well as being about half as many lines of code.

So, for small problems at least, psp has worked well for me.

user26294
  • 5,532
  • 3
  • 22
  • 18
1

I think it depends on the definition of what a framework is and what it should do for you.

As pointed out, a very minimal "framework" would be WSGI as it only defines one small interface for interfacing with a web server. But it's a powerful approach because of the middleware you can put between your app and the server.

If you want more slightly more, like some URL to function mapping, then you have some choices, some of which have been already mentioned.

If you go further you might come to Pylons or Turbogears or Django, after that maybe Zope but it grows bigger and maybe the pain as well as you always buy into the opinions of that framework.

What I recently use more and more (coming from Zope/Plone) is repoze.bfg. It's very small, does not come with a ORM bundled (so you can use SQLAlchemy, Storm or simply go to an object database like the ZODB). What it does is basically handling how you come from a URL to a view which is a function. It supports both URL Mapping (a la Routes) or object traversal, which IMHO is very powerful in some circumstances esp. if you have a not so strict mapping. The good thing is that it directly comes with an ACL based security framework which can use if you want to which IMHO is very practical to have. That way you don't need decorators which seem to be used mostly for such things.

And of course it's based on WSGI. Also look for the repoze subversion repository for quite a lot of middleware and the Paste stuff is also very useful for WSGI related tasks.

MrTopf
  • 4,813
  • 2
  • 24
  • 19
0

What's wrong with Django? It doesn't force you to use it's ORM and controllers are just plain Python functions instead of Rails-like class methods. Also, url routing is done with regular expressions instead of another framework-invented syntax. If django seems like too much for you anyway, i recommend taking a look at Werkzeug

Сыч
  • 930
  • 1
  • 8
  • 15
0

I'm pretty fond of Google AppEngine. I use the ORM and templating system, but otherwise follow a REST-patterned design and just implement Python methods for the corresponding HTTP ones. It makes the raw HTTP interaction central, and optionally gives you other things to use. Plus no more configuring and managing your deployment environment!