views:

1054

answers:

9

I'm pretty proficient in PHP, but want to try something new.

I'm also know a bit of Python, enough to do the basics of the basics, but haven't used in a web design type situation.

I've just written this, which works:

#!/usr/bin/python

def main():
    print "Content-type: text/html"
    print
    print "<html><head>"
    print "<title>Hello World from Python</title>"
    print "</head><body>"
    print "Hello World!"
    print "</body></html>"

if __name__ == "__main__":
    main()

Thing is, that this seems pretty cumbersome. Without using something huge like django, what's the best way to write scripts that can process get and post?

+9  A: 

As far as full frameworks go I believe Django is relatively small.

If you really want lightweight, though, check out web.py, CherryPy, Pylons and web2py.

I think the crowd favorite from the above is Pylons, but I am a Django man so I can't say much else.

For more on lightweight Python frameworks, check out this question.

Paolo Bergantino
Maybe Django isn't so big as I thought then - I thought it was the equivalent of Rails or CakePHP.
Rich Bradshaw
Heck, I include django in some non-web applications just for its DB or templating support.
FogleBird
+6  A: 

There are a couple of web frameworks available in python, that will relieve you from most of the work

  1. Django
  2. Pylons (and the new TurboGears, based on it).
  3. Web2py
  4. CherryPy (and the old TurboGears, based on it)

I do not feel Django as "big" as you say; however, I think that Pylons and CherryPy may be a better answer to your question. CherryPy seems simpler,. but seems also a bit "passé", while Pylons is under active development.
For Pylons, there is also an interesting Pylons book, available online.

Roberto Liffredo
A: 

I really love django and it doesn't seem to me that is huge. It is very powerful but not huge.

If you want to start playing with http and python, the simplest thing is the BaseHttpServer provided in the standard library. see http://docs.python.org/library/basehttpserver.html for details

luc
A: 

I agree with Paolo - Django is pretty small and the way to go - but if you are not down with that I would add to TurboGears to the list

Shane C. Mason
I was going to add TurboGears but I am under the impression it is as big if not bigger than Django. How do I check something like that?
Paolo Bergantino
A: 

If you are looking for a framework take a look at this list: Python Web Frameworks

If you need small script(-s) or one time job script, might be plain CGI module is going to be enough - CGI Scripts and cgi module itself.

I would recommend you to stick to some framework if you want to create something more then static pages and simple forms. Django is I believe most popular and most supported.

zdmytriv
+4  A: 

Your question was about basic CGI scripting, looking at your example, but it seems like everyone has chosen to answer it with "use my favorite framework". Let's try a different approach.

If you're looking for a direct replacement for what you wrote above (ie. CGI scripting), then you're probably looking for the cgi module. It's a part of the Python standard library. Complimentary functionality is available in urllib and urllib2. You might also be interested in BaseHTTPServer and SimpleHTTPServer, also part of the standard library.

Getting into more interesting territory, wsgiref gives you the basics of a WSGI interface, at which point you probably want to start thinking about more "frameworky" (is that a word?) things like web.py, Django, Pylons, CherryPy, etc, as others have mentioned.

esm
Nice in depth answer - thanks!
Rich Bradshaw
This answer is correct, and will certainly allow you to explore writing CGI in Python. But, it will not help you to learn the Pythonic approach to solving this particular problem. The Pythonic approach to this problem is found in all the answers you got about the various web frameworks that are available and under active development and support.
semiuseless
Yes, but I want to understand how things work at a lower level first.
Rich Bradshaw
In that case, forget CGI and look as WSGI. It also takes about ten minutes to learn, and then you can go on looking at a web framework. ;)
Lennart Regebro
Yeah, that sounds like a good idea.
Rich Bradshaw
A: 

What is "huge" is a matter of taste, but Django is a "full stack" framework, that includes everything from an ORM, to templates to well, loads of things. So it's not small (although smaller than Grok and Zope3, other full-stack python web frameworks).

But there are also plenty of really small and minimalistic web frameworks, that do nothing than provide a framework for the web part. Many have been mentioned above. To the list I must add BFG and Bobo. Both utterly minimal, but still useful and flexible.

http://bfg.repoze.org/ http://bobo.digicool.com/

Lennart Regebro
+1  A: 

In Python, the way to produce a website is to use a framework. Most of the popular (and actively maintained/supported) frameworks have already been mentioned.

In general, I do not view Djano or Turbogears as "huge", I view them as "complete." Each will let you build a database backed, dynamic website. The preference for one over the other is more about style than it is about features.

Zope on the other hand, does feel "big". Zope is also "enterprise" class in terms of the features that are included out of the box. One very nice thing is that you can use the ZODB (Zope Object Database) without using the rest of Zope.

It would certainly help if we knew what kinds of websites you were interested in developing, as that might help to narrow the suggestions.

semiuseless
+1  A: 

In web2py the previous code would be

# in controller default.py
def main():
    return dict(message="Hello World")

# in view default/main.html
<html><head>
<title>Hello World from Python</title>
</head><body>
{{=message}}
</body></html>

nothing else, no installation, no configuration, you can edit the two files above directly on the web via the admin interface. web2py is based on wsgi but works also with cgi, mod_python, mod_proxy and fastcgi if mod_wsgi is not available.

mdipierro