[mod_python] NameError: name 'req' is not defined

Jorey Bump list at joreybump.com
Mon Oct 13 08:52:16 EDT 2008


Handrix wrote, at 10/13/2008 06:37 AM:

> When i try to execute a python script in modpython i got this error:
> NameError: name 'req' is not defined

Give an example. Exactly how are you trying to access your script?

> from mod_python import apache
> 
> def handler(req):
>     req.content_type = 'text/plain'
>     req.write("Hello World!")
>     return apache.OK
> 
> handler(req)

?

> here is my apache config
> 
> <Directory "/var/www/python">
> AddHandler python-program .py
> PythonHandler  mod_python.publisher
> PythonDebug On
> </Directory>

Your script is not a very good test of Publisher.

>   File "/var/www/python/index.py", line 8, in ?
> 
>     handler(req)

What are you expecting to happen?

Publisher already is a handler, so you don't need any special handler
code in your script (and you typically don't need to import
mod_python.apache to use it).

Your config is fine, but your test is flawed. Create a new script called
testing.py that contains only this:

def index():
    return '<html><body><h1>It worked!</h1></body></html>'

Then point your browser at:

 http://your.host/testing.py/index

If you want to write your own handler, carefully read the documentation
for your version of mod_python.




More information about the Mod_python mailing list