Graham Dumpleton
grahamd at dscpl.com.au
Mon Jun 20 18:23:32 EDT 2005
If what you are trying to do is come up with your own mechanism for dispatching different URLs against different handlers in different files, you should probably start by researching how mod_python.cgihandler, mod_python.publisher, mpservlets and Vampire work as they all provide this basic feature in various ways You will find mod_python.publisher and mod_python.cgihandler as a part of mod_python itself. As to mpservlets and Vampire, you can find them at: http://home.comcast.net/~d.popowich/mpservlets/ http://www.dscpl.com.au/projects/vampire A quick way of doing it (untested) would be: from mod_python import apache import os def handler(req): directory = os.path.dirname(req.filename) name = os.path.splitext(os.path.split(req.filename)[1])[0] module = apache.import_module(name,path=[directory]) return module.main(req) Then in the script file: def main(req): req.content_type = "text/plain" req.send_http_header() req.write('Hello there!') return apache.OK This will not necessarily work in all situations as it doesn't do all the checks it should. You would be better off using one of the other dispatchers available unless your plan is to learn by writing your own. Graham amit wrote .. > Hello Grisha, > > def main(): > req.write('Hello there!') > > if __name__=='__main__': > import test_mod # test_mod.py is the name of the script > ret = test_mod.main() > sys.exit(ret) > > Nothing happend. > I guessed that __name__ under mod_python might behave differently so I > changed the '==' to '!=' and got the following traceback: > > Mod_python error: "PythonHandler pythonframe" > > Traceback (most recent call last): > > File "F:\Python23\Lib\site-packages\mod_python\apache.py", line 299, > in HandlerDispatch > result = object(req) > > File "C:/Program Files/Apache Group/Apache2/cgi-bin/mod_python/\pythonframe.py", > line 91, in handler > execfile(req.filename, {'req':req}) > > File "C:/Program Files/Apache Group/Apache2/cgi-bin/mod_python/test_mod.py", > line 6, in ? > ret = test_mod.main() > > File "C:/Program Files/Apache Group/Apache2/cgi-bin/mod_python/\test_mod.py", > line 2, in main > req.write('Hello there!') > > NameError: global name 'req' is not defined > > > Amit > > > > I tried using the usual method to call main() > Gregory (Grisha) Trubetskoy wrote: > > > > > What calls the main() function below? > > > > Grisha > > > > > > On Mon, 20 Jun 2005, amit wrote: > > > >> Hello, > >> > >> I am using mod_pyton to write a collection of scripts. And I am > >> facing the following problem: > >> I use the handler to execute a script. And I try to pass the req > >> object to it: > >> > >> def handler(req): > >> execfile(req.filename, {'req':req}) > >> > >> in the script I have the following code: > >> > >> def main(): > >> req.write('Hello there!') > >> > >> I know that the script is executed but I don't get the 'hello there!' > >> message in my browser. Can anyone help? > >> > >> Thanks > >> Amit > >> > >> _______________________________________________ > >> Mod_python mailing list > >> Mod_python at modpython.org > >> http://mailman.modpython.org/mailman/listinfo/mod_python > >> > > > > > > _______________________________________________ > Mod_python mailing list > Mod_python at modpython.org > http://mailman.modpython.org/mailman/listinfo/mod_python
|