Juergen Fiedler
juergen at fiedlerfamily.net
Wed Sep 29 19:47:18 EDT 2004
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I am beginning to learn mod_python; I have just written the skeleton of a web application, but have no idea where exactly the glaring holes are could someone please take a look at the code and tell me whether I am on the right track? My .htaccess looks like this: - --- AddHandler python-program .py PythonHandler main PythonAutoReload On PythonDebug On - --- main.py: - --- from mod_python import apache def handler(req): ~ page = '' ~ try: ~ page = req.uri.split('main.py/')[1] ~ except IndexError: ~ pass ~ if len(page)==0: ~ page = 'index' ~ try: ~ exec "import site_code."+page ~ exec "h = site_code."+page+".Handler" ~ except ImportError: ~ return apache.HTTP_NOT_FOUND ~ return h(req).handle() - --- site_code/test.py - --- from mod_python import apache class Handler: ~ def __init__(self,req): ~ self.__req = req ~ def handle(self): ~ #apache.log_error("flan!"); ~ #raise apache.SERVER_RETURN, apache.HTTP_FORBIDDEN ~ page = 'index' ~ try: ~ page = self.__req.uri.split('main.py/')[1] ~ except IndexError: ~ pass ~ self.__req.content_type = "text/plain" ~ self.__req.send_http_header() ~ self.__req.write("hello '"+page+"'") ~ return apache.OK - --- The idea is that the main handler takes a URL like 'http://www.test.com/main.py/test' and separates the last part (everything after the 'main.py/'). It then loads a Handler class from a module of that name in the site_code package. Then it instantiates an object of that class and calls the 'handle' method. I purposely did not touch anything but the default handler yet. Now the big question: Is this a valid way to go about writing a web application in mod_python or am I completely on the wrong track? The exec calls in the main handler are probably not optimal when it comes to ~ execution speed, but I couldn't think of anything better to do to import a module dynamically based on external variables. Any input would be greatly appreciated. Thanks - --j -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFBWzt2YFE+dpEf2hQRAmK2AKC1crxcBFYWHpklqZh9Hswx9qgRewCeKmUC z7824NxbM4LeerguT1UPRqg= =sCdF -----END PGP SIGNATURE-----
|