47

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.

8
  • 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? Commented Feb 16, 2010 at 20:17
  • 23
    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 Commented Feb 17, 2010 at 2:16
  • 2
    @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? Commented Feb 17, 2010 at 2:24
  • 2
    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. Commented Feb 17, 2010 at 3:20
  • 1
    @S.Lott do you have to argue everything. i too am a php er and i do not like frameworks if i can avoid avoid them. Commented Feb 27, 2022 at 15:58

7 Answers 7

36

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I will look into. People are very fast at replying here, I love this place :D
5

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.

3 Comments

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: "technically a framework"? What's the technical definition?
@Dr Hydralisk if you want to make a python web framework, then it's okay. But if you want to make a functional website that can serve the market, then i will say "not using a framework" is a huge amount of useless work and reinventing the wheel. If you love challenges, then take challenges that are meaningful.
3

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.

1 Comment

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).
2

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.

2 Comments

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.
I was looking into App Engine, looks interesting.
2

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)

3 Comments

Looks nice, but I am also anti-apache.. Is there anything for Nginx?
There seems to be a wsgi module for nginx: wiki.nginx.org/NginxNgxWSGIModule
Why is that you are an anti-apache? Just want to know the reasons.
1

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

http://webpy.org/

Comments

0

You can just make the entire thing yourself as a CGI script written in python. On an Apache server you go into the httpd.conf file and add these two lines at the bottom.

AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict

Then the standard output is redirected to the client, i.e. the print(...) method sends text to the client. Then you just read the .html, .css, and .js files stored on the server and print() each line. Connect to your database on the backend. Set up your security/authorization protocols... Basically you will need to make the entire framework yourself, only it will be customized to fit your needs perfectly.

Probably a good idea to come up with some special character to parse for when reading the files on the server and before printing to insert any dynamic content, such as:

HTML

<div>
    <p> <<& pythonData $>> </p>
</div>

Python

htmlFile = open("something.html", "r")
for line in htmlFile:
    if "<<&" in line:
        # figure out what characters that special symbol is in the line
        # replace them with dictionary value or variable or something
        print(line)
    else:
        print(line)

Here is the documentation for the official library to work with Common Gateway Interface (CGI) in python: https://docs.python.org/3/library/cgi.html It includes an example that shows reading form data sent to the server into a python script.

Don't forget to tell your scripts where the python interpreter is on the Apache server (should be in /bin somewhere), in other words point at python with the sh-bang:

#!/bin/python3.10

Or wherever your server's python interpreter is located at.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.