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– S.Lott2010-02-16 20:17:30 +00:00Commented Feb 16, 2010 at 20:17
-
23PHP 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 :DDr Hydralisk– Dr Hydralisk2010-02-17 02:16:17 +00:00Commented 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?S.Lott– S.Lott2010-02-17 02:24:37 +00:00Commented Feb 17, 2010 at 2:24
-
2Features 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– Dr Hydralisk2010-02-17 03:20:36 +00:00Commented 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.webs– webs2022-02-27 15:58:30 +00:00Commented Feb 27, 2022 at 15:58
7 Answers
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.
1 Comment
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
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
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
webapp framework is pretty minimal, and it's damn near as easy to get up and running as plain old PHP on commodity hosting.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
You should try web.py, it provides a bare minimum of features that does not get in your way.
Comments
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.