开发者

Program web applications in python without a framework?

I am just starting Py开发者_Go百科thon 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.


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.


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.


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.


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.


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)


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

http://webpy.org/


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜