tags:

views:

265

answers:

7

I am just starting Python and I was wondering how I would go about programming web applications without the need of a framework. I am an experiance PHP developer but I have an urge to try out Python and I usually like to write from scratch without the restriction of a framework.

+11  A: 

WSGI is the Python standard for web server interfaces. If you want to create your own framework or operate without a framework, you should look into that. Specifically I have found Ian Bicking's DIY Framework article helpful.

As an aside, I tend to think frameworks are useful and personally use Django, like the way Pylons works, and have used Bottle in the past for prototyping--you may want to look at Bottle if you want a stay-out-of-your-way microframework.

Michael Greene
Thank you, I will look into. People are very fast at replying here, I love this place :D
Dr Hydralisk
+2  A: 

You will have to look into something like CGI or FastCGI, which provides an API to communicate to the webserver.

Google App Engine enables you to write simple apps, and even provides a local webserver where you can try things out.

Ikke
I think App Engine is a great suggestion. The `webapp` framework is pretty minimal, and it's damn near as easy to get up and running as plain old PHP on commodity hosting.
Will McCutchen
I was looking into App Engine, looks interesting.
Dr Hydralisk
+2  A: 

One of the lightest-weight frameworks is mod_wsgi. Anything less is going to be a huge amount of work parsing HTTP requests to find headers and URI's and methods and parsing the GET or POST query/data association, handling file uploads, cookies, etc.

As it is, mod_wsgi will only handle the basics of request parsing and framing up results.

Sessions, cookies, using a template generator for your response pages will be a surprising amount of work.

Once you've started down that road, you may find that a little framework support goes a long way.

S.Lott
I like a challenge :D
Dr Hydralisk
Hmmm, mod_wsgi isn't technically a framework, it is a WSGI adapter. It doesn't even handle HTTP request parsing as Apache does all that for it.
Graham Dumpleton
@Graham Dumpleton: "technically a framework"? What's the technical definition?
S.Lott
A: 

For a PHP programmer, I think mod_python is a good way to get started without any framework. It can be used directly as Apache 2 module. You can have code tags (like <? ?> in PHP) and even conditional HTML output (HTML inside if statement):

<%
if x == y:
   # begin
%>

  ... some html ...

<%
# end
%>

(simplified example taken from onlamp.com's Python Server Pages tutorial)

AndiDog
Looks nice, but I am also anti-apache.. Is there anything for Nginx?
Dr Hydralisk
There seems to be a wsgi module for nginx: http://wiki.nginx.org/NginxNgxWSGIModule
AndiDog
+1  A: 

You should try web.py, it provides a bare minimum of features that does not get in your way.

http://webpy.org/

truppo
A: 

The answer is "don't". Using a framework makes your code more maintainable, scalable, readable, testable, and modular.

Mike Graham
Like I said above, I love to write my own things, and when I do write them, I make sure I follow all those things, I am a bit of perfectionist when it comes to programming (a serious optimization freak, its a pain). And I keep a library for most of my code, so when I need to do something I just stick them together and I got a personal mini framework that has only the features I need but can still be added onto later if I need to.
Dr Hydralisk
The proper NIH solution (if there is such a thing) would be to *write a framework*, not to avoid a framework. Being able to introduce reasonable abstractions (i.e., using a framework) makes writing great applications feasible. I don't know what it means to be "a serious optimization freak, its a pain", but it doesn't sound like anyone I'd ever want to hire—time spent optimizing code is often uneconomical and wasted. Clarity, programmer time, maintainability, and testability are all usually worth optimizing for over speed and space.
Mike Graham
A: 

People here love frameworks. One shortcoming I have noted is that Python lacks a handy-dandy module for Sessions as a library built-in, despite it being available in PHP and as CGI::Session in Perl.

You will end up doing:

import cgi  # if you want to work with forms and such
import cgitb; cgitb.enable()  # to barf up errors to the web
print 'Content-type: text/html\n\n'  # to start off any HTML.

You will have to write session stuff on your own.

MetaHyperBolic
I think I am just going to not use Python for web development, I will just use it as a general purpose programming language. Gonna go back to PHP and maybe give Rails another try (ya I know it is a framework.. it is the only one that I really liked, its just the Ruby syntax is so annoying).
Dr Hydralisk