mod_python

21 views
Skip to first unread message

jemi

unread,
Jan 12, 2006, 7:57:14 PM1/12/06
to web.py
I just wanted to make web.py run with mod_python2.4 (following hints
from webpy.org) and got stuck with a 'not found' message. I tried all
the possibilities i could think of (/codep, /codep/, /codep/something,
/something, /codep.py, /codep.py/, /codep.py/something) and all i got
was a 'not found' message.

At the moment I can get along with running web.py as an internal server
behind mod_proxy... but something more "mature" wouldn't be too bad.
: )

Is there any obvious pitfall you can think of?

Sergey Khenkin

unread,
Jan 13, 2006, 3:06:46 AM1/13/06
to jemi
Hello Jemi,

I've managed to configure web.py to run with Apache2, mod_python 3.1.4
and Python 2.4.1.
However I did it on Windows system.
Everything worked as described on http://webpy.org/install page except
for 2 things:
1. After wsgiref installation I had to extract the egg manually to
create an wsgiref subfolder in site-packages.
2. I had to replace wsgiref.modpy_gateway::handler with
wsgiref.modpython_gateway::handler in the .htaccess file. Correct
.htaccess is below.
After that accessing http://localhost/py/codep.py/ gave me 'Hello,
world!'.

My files located in DocumentRoot/py folder:
.htaccess
codep.py
web.py

-- .htaccess start --
AddHandler python-program .py
PythonHandler wsgiref.modpython_gateway::handler
PythonOption application codep::main
-- .htaccess end --

-- codep.py start --
import web

urls = (
'/', 'view'
)

class view:
def GET(self):
print "Hello, world!"

web.internalerror = web.debugerror

main = web.wsgifunc(web.webpyfunc(urls))
-- codep.py end --

Best regards,
Sergey Khenkin

jemi

unread,
Jan 13, 2006, 4:37:21 AM1/13/06
to web.py
Hi Sergey,

i have installed wsgiref from source via "python setup.py install" and
i already changed modpy_gateway to modpython_gateway. :)
Since my "not found" is a web.py output... installation seems to work
fine so far.

Now i shut down my running webpy process and copied your quoted scripts
to a fresh folder and the result is the same: "not found". The best
thing i could get was the browser offering an empty file for download.
could there be a problem with running as a virtualhost?

Nando Vieira

unread,
Jan 13, 2006, 6:48:16 AM1/13/06
to we...@googlegroups.com
Hi Sergey.

Is possible to run it as CGI?

Cheers,
-- Nando Vieira

Sergey Khenkin wrote:




_______________________________________________________
Yahoo! doce lar. Faça do Yahoo! sua homepage.
http://br.yahoo.com/homepageset.html


Sergey Khenkin

unread,
Jan 13, 2006, 7:50:55 AM1/13/06
to Nando Vieira, we...@googlegroups.com
Nando,

> Is possible to run it as CGI?

Yes, sure. As CGI and FastCGI according to the docs. Haven't tried that
though.

Best regards,
Sergey Khenkin

Nando Vieira

unread,
Jan 13, 2006, 8:03:53 AM1/13/06
to we...@googlegroups.com
But on the docs there's no example of how configuring Apache. Just lighttpd.
How to apply something similar to Apache?

| server.modules = ("mod_fastcgi", "mod_rewrite")
server.document-root = "/path/to/root/"
fastcgi.server = ( "/code.py" =>
(( "socket" => "/tmp/fastcgi.socket",
"bin-path" => "/path/to/root/code.py",
"max-procs" => 1
))
)

url.rewrite-once = (
"^/favicon.ico$" => "/static/favicon.ico",
"^/static/(.*)$" => "/static/$1",
"^/(.*)$" => "/code.py/$1",
)|


Cheers,
-- Nando Vieira


Sergey Khenkin wrote:

Sergey Khenkin

unread,
Jan 14, 2006, 10:30:50 AM1/14/06
to web.py
Hi Nando,

> But on the docs there's no example of how configuring Apache. Just lighttpd.
> How to apply something similar to Apache?

Well it seems like that.

> | server.modules = ("mod_fastcgi", "mod_rewrite")
> server.document-root = "/path/to/root/"

Install mod_fastcgi (see http://fastcgi.com/)
Install flup (http://www.saddi.com/software/flup/)
Set up DocumentRoot in httpd.conf
Then add this line after other LoadModule lines
LoadModule mod_fastcgi modules/mod_fastcgi.so
and uncomment the line with mod_rewrite if it's commented.

> fastcgi.server = ( "/code.py" =>
> (( "socket" => "/tmp/fastcgi.socket",
> "bin-path" => "/path/to/root/code.py",
> "max-procs" => 1
> ))
> )

Add this line to httpd.conf:
FastCgiServer /path/to/root/code.py -socket /tmp/fastcgi.socket
-maxProcesses 1

> url.rewrite-once = (
> "^/favicon.ico$" => "/static/favicon.ico",
> "^/static/(.*)$" => "/static/$1",
> "^/(.*)$" => "/code.py/$1",
> )|

Those are rewrite rules for mod_rewrite.
They can be configured in .htaccess file in the /path/to/root/
directory or in httpd.conf inside <Directory /path/to/root/> ...
</Directory> element.
See documentation on mod_rewrite.
In .htaccess or httpd.conf add this line
AddHandler fastcgi-script py

Well, I suppose it's something like that :)
The problem for you and me is that we work on Windows and we have an
issue with Flup namely with flup.server.fcgi_base module which uses
socket.fromfd() function.
And if we refer to socket.py we can see that socketpair() and fromfd()
functions are not available on all platforms (e.g. Windows).

So I believe it's impossible to use web.py with FastCGI on Windows at
the moment. It would be great if Aaron shed some light on that.

Best regards,
Sergey Khenkin

dter...@gmail.com

unread,
Jan 16, 2006, 2:16:53 PM1/16/06
to web.py
If you're already using flup, it's trivial to patch web.py to support
SCGI, which works fine with apache mod_scgi and TCP socket to
localhost. You can download my copy at http://meat.net/src/web.py --
I've submitted a patch to Aaron for inclusion.

peter

unread,
Jan 18, 2006, 5:13:12 PM1/18/06
to web.py
Hi Jemi,

I can confirm Sergeys technique on a Mandriva 10.2 box with Apache2,
mod_python2 & python 2.4.1 Heres my notes:


1) Read Graham Dumpletons notes on getting mod_python working with
apache. You can find the notes at
http://www.dscpl.com.au/articles/modpython-001.html


2) View apache log files access_log & error_log in separate consoles
using
'tail -f /path/to/apache/logs' to view what apache reports

*In console 1, type...
$ tail -f /var/log/httpd/access_log
*In console 2 type ...
$ tail -f /var/log/httpd/error_log


3) Use wget to check the output of apache.

'wget http://localhost/py/codep.py'


4) Add 'web.header("Content-Type","text/html; charset=utf-8")'
above the print statement to allow me to view the results in
firefox. You can confirm the number of characters apache
is outputting on a 200 response in conjunction with the 'hello world'
If you dont add the web.header the reponse to the browser will
force a download instead of rendering.

-- codep.py start --
import web
urls = (
'/', 'view'
)
class view:
def GET(self):

web.header("Content-Type","text/html; charset=utf-8")


print "Hello, world!"
web.internalerror = web.debugerror
main = web.wsgifunc(web.webpyfunc(urls))
-- codep.py end --


It think its important to have numerous ways to run web.py apps (do we
have a name) using apache. In the light of Sergeys code example do we
have to modify the lighttpd examples to run in apache?

Regs Peter Renshaw

peter

unread,
Jan 23, 2006, 1:14:41 AM1/23/06
to web.py
In step 3, the line should read:

'wget http://localhost/py/codep.py/'

Forgot the end forward slash.

jemi

unread,
Jan 23, 2006, 3:45:39 AM1/23/06
to web.py
if you follow the instructions given on webpy.org then you end up using
"codep.py" instead of "code.py". :)

Sergey Khenkin

unread,
Jan 13, 2006, 9:31:17 AM1/13/06
to Nando Vieira, we...@googlegroups.com
Nando,

> But on the docs there's no example of how configuring Apache. Just lighttpd.
> How to apply something similar to Apache?

> | server.modules = ("mod_fastcgi", "mod_rewrite")
> server.document-root = "/path/to/root/"

This looks just like Apache configuration stored in httpd.conf

> fastcgi.server = ( "/code.py" =>
> (( "socket" => "/tmp/fastcgi.socket",
> "bin-path" => "/path/to/root/code.py",
> "max-procs" => 1
> ))
> )

This must be FastCGI configuration.
Haven't tried it yet but http://www.fastcgi.com/ and
http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html should
explain how to configure FastCGI on Apache.
I'll try to do it as soon as I have time.

> url.rewrite-once = (
> "^/favicon.ico$" => "/static/favicon.ico",
> "^/static/(.*)$" => "/static/$1",
> "^/(.*)$" => "/code.py/$1",
> )|

And these are just rewrite rules for mod_rewrite which can be
specified in .htaccess:
RewriteEngine on
RewriteRule ^/favicon.ico$ /static/favicon.ico
...


Best regards,
Sergey Khenkin

Reply all
Reply to author
Forward
0 new messages