web development with Python and Mod_Python and my ideal web handler function - (remarkably like wsgi) has a good background on http and dynamic content mechanisms for web developers
framework and interface method requires making hard choices. s Each decision is frought with peril, and you will live with the decisions you make for a long time. s Some decisions will be made based on existing requirements, others will be made based on business decisions. s But before you rule ANYTHING out - the key to success is understanding s YOUR APPLICATION, s YOUR REQUIREMENTS AND YOUR LIMITS.
interface with? s What API's will you consume data from? s What API's will your users use to consume data? s Will your users only use the app interactively? s Will you produce an API? s Do you want to control API Access?
that budget adjust for growth? s What is your hardware platform? s Does that budget adjust for growth? s What is your bandwidth solution? s Does that budget adjust for growth?
string Content = \ GenerateResponse( SafeCGI, SafeCookieJar, Session, Site, SafeENV ) try: do this except: # recover this way Handleit - change the Status, Headers, and Content as necessary if SafeCookieJar.changed: add cookie changes to Headers update the Headers Response Size in Header Dict.
>>>> WEBSERVER (client) (server) HTTP RESPONSE <<<<< - - - WEBSERVER REQUESTS ARE ONE WAY REQUESTS RESPONSES ARE ONE WAY HTTP IS * A BI-DIRECTIONAL, PAIRED, ONE WAY COMMUNICATIONS CHANNEL HTTP IS NOT * AN INTERACTIVE CHANNEL or PROTOCOL REQUESTS ARE STATELESS REQUESTS ARE MADE TO A SERVER RESPONSES ARE SENT FROM A SERVER
(usually port 80) step 1 > send formatted request line step 2 > send request metadata as CRLF seperated NVP's step 3 > send END Request Metadata Marker as CRLF+CRLF step 4 > send optional Encoded Upload or Post Data
step 1 > send formatted STATUS Line step 2 > send Response metadata as CRLF seperated NVP's step 3 > send END Response Metadata Marker as CRLF+CRLF step 4 > send optional content described in the Response MetaData
USE IN PRODUCTION) # MaxRequestsPerChild - IS FOR DEVELOPMENT ONLY sMaxRequestsPerChild 1 <Directory /var/www/> Options Indexes FollowSymLinks MultiViews +ExecCGI AllowOverride None Order allow,deny allow from all s RedirectMatch ^/$ /pystuff/site.py?$1 </Directory> <Directory /var/www/pystuff> s AddHandler mod_python .py s PythonHandler mplookup s PythonDebug On </Directory>
you expect the handler function in mplookup (your defined handler) is called EVERY TIME ANY .PY filename is called in the directory. sss IT DOES NOT LOAD THE FILENAME YOU SPECIFY! sss EVER TIME A .py file is called in the handled directory the HANDLER IN mplookup will be executed THIS WILL CONFUSE YOU IF YOU DONT TAKE NOTE.
# note ^^^^^^^^^^^^^^^^^^^^ this style import # it deals with reloading changed modules for long # running processes def handler(req): # note ^^^ apache interface object req.content_type = 'text/html' req.write(content.generatePage(req)) # note ^^^^^^^^^^^^^^^^^^^^ this calls the # imported function generatePage # keeping you from restarting the server # over and over again return apache.OK # note ^^^^^^^^^ this assumes you always want # to return a 200 OK
= 'Hello - thanks for visiting %s ' % req.hostname #note ^^^^^^ CONSTRUCT YOUR CONTENT except: #note ^^^^^^ ASSUME YOUR FUNCTION COULD FAIL!!! result = 'An error happened processing your request!' return result #note ^^^^^^ RETURN YOUR CONTENT
\ GenerateResponse( SafeCGI, SafeCookieJar, Session, Site, SafeENV ) try: do this except: # recover this way Handleit - change the Status, Headers, and Content as necessary if SafeCookieJar.changed: add cookie changes to Headers update the Headers Response Size in Header Dict. We could have returned STATUS, HEADERS and Content ;) with a simple change to mplookup.py and content.py