40
votes

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 experienced 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 like flask or django to name a few.

6
"restriction of a framework". Why do you say a framework is a restriction? PHP is a huge and sophisticated framework to save you from writing a lot of tedious code. Why give up that level of power? What's wrong with frameworks that PHP's is OK but another non-PHP framework is not OK? - S.Lott
PHP is not really a framework, it is an interpreted language, but not a framework. To me, most of the time a framework is a restriction because I have to do what the framework tells me I have to do. For example, in CakePHP you have to name controllers, views, and models a specific name, you don't have to do it, but if you do not, then things do not "magically" link together, so what is the point of using the framework? And frameworks are stuffed with features I do not need making them slower. I prefer to write my own stuff (I keep a library of all my code), I am a freak like that :D - Dr Hydralisk
@Dr Hydralisk: PHP can stand alone. However, when embedded in Apache, it most definitely is a framework. Try writing your own version of mod_php "from scratch" and see all the things PHP is doing for you automagically. The point of using a framework is that it frees you from details. PHP frees you from numerous HTTP processing details. Many Python frameworks free you from details. Frameworks are "stuffed with features"? Really? Can you update your question to specifically identify the features you don't like? - S.Lott
Features that I do not need. I love to do stuff from scratch, I do not mind writing all of that, makes the project more fun (makes it feel like a bigger project than it actually is I suppose), I live for configuration. - Dr Hydralisk

6 Answers

34
votes

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.

4
votes

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.

2
votes

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.

1
votes

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)

1
votes

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

http://webpy.org/

1
votes

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.