Problems with web.py running as cgi

448 views
Skip to first unread message

nki...@gmail.com

unread,
Feb 20, 2006, 1:08:03 PM2/20/06
to web.py
Hi I am trying to get web.py running as a cgi process. I am doing this
because my hosting provider doesn't support fastcgi, unfortunately, but
I still wanted to use the web.py framework. I am pretty new at this
process, apologies in advance for stupid questions/mistakes.

I am testing on my home Windows system with Apache and python 2.4
installed. I am using the helloworld program from the web.py homepage
named as hello.py. It looks like this:

--start hello.py --
#! c:/Python/python.exe -u
##!/usr/bin/python

import web

urls = (
'/(.*)', 'hello'
)

class hello:
def GET(self, name):
i = web.input(times=1)
if not name: name = 'world'
for c in xrange(int(i.times)): print 'Hello,', name+'!'

if __name__ == "__main__": web.run(urls)
-- end hello.py --

This works fine if I run hello.py from the command line, and starts up
a localhost:8080 webserver. However, my trouble has been getting it
working served through Apache as cgi. I keep getting Internal Server
500 error messages. I noticed from the error logs that it was trying
to reference a flup module. As per the "franny hack"
(http://groups.google.com/group/webpy/tree/browse_frm/thread/73c33e3bb0504a22/eb5e468d46cb1d74?rnum=1&_done=%2Fgroup%2Fwebpy%2Fbrowse_frm%2Fthread%2F73c33e3bb0504a22%2F956847616de70fdf%3F#doc_956847616de70fdf)
I downloaded flup into my python Lib directory and installed it using
"python setup.py install". Still didn't work. I edited the fcgi.py
code and added "from __future__
import generators", though since I'm running Python 2.4 I didn't think
this would matter. Still didn't work. Here's the error log message:

----start error log-------
[Mon Feb 20 09:08:23 2006] [error] [client 127.0.0.1] Premature end of
script headers: hello.py
[Mon Feb 20 09:08:23 2006] [error] [client 127.0.0.1] Traceback (most
recent call last):

[Mon Feb 20 09:08:23 2006] [error] [client 127.0.0.1] File
"C:/Program Files/Apache Group/Apache2/htdocs/survey/hello.py", line
16, in ?

[Mon Feb 20 09:08:24 2006] [error] [client 127.0.0.1] if __name__
== "__main__": web.run(urls)

[Mon Feb 20 09:08:24 2006] [error] [client 127.0.0.1] File
"C:\Program Files\Apache Group\Apache2\htdocs\survey\web.py", line 908,
in run

[Mon Feb 20 09:08:24 2006] [error] [client 127.0.0.1] return
runwsgi(wsgifunc(webpyfunc(inp, fvars, autoreload), *middleware))

[Mon Feb 20 09:08:24 2006] [error] [client 127.0.0.1] File
"C:\Program Files\Apache Group\Apache2\htdocs\survey\web.py", line 915,
in runwsgi

[Mon Feb 20 09:08:24 2006] [error] [client 127.0.0.1] return
runfcgi(func)

[Mon Feb 20 09:08:24 2006] [error] [client 127.0.0.1] File
"C:\Program Files\Apache Group\Apache2\htdocs\survey\web.py", line
1035, in runfcgi

[Mon Feb 20 09:08:24 2006] [error] [client 127.0.0.1] return
MyServer(func, multiplexed=True).run()

[Mon Feb 20 09:08:24 2006] [error] [client 127.0.0.1] File
"C:\Python\Lib\site-packages\flup\server\fcgi.py", line 107, in run

[Mon Feb 20 09:08:24 2006] [error] [client 127.0.0.1] sock =
self._setupSocket()

[Mon Feb 20 09:08:24 2006] [error] [client 127.0.0.1] File
"C:\Python\Lib\site-packages\flup\server\fcgi_base.py", line 965, in
_setupSocket

[Mon Feb 20 09:08:24 2006] [error] [client 127.0.0.1] sock =
socket.fromfd(FCGI_LISTENSOCK_FILENO, socket.AF_INET,

[Mon Feb 20 09:08:24 2006] [error] [client 127.0.0.1] AttributeError:
'module' object has no attribute 'fromfd'

------end error log----------


I'm likely doing something very simple wrong here, but am at a loss as
to what it is. Also, I'd like to get this working on my hosting
provider (tried it there too but it doesn't work and I don't get the
detailed error logs) so any comments on changes I'll need to make there
as well would be welcome.

Thanks!
-Niki

Jon Rosebaugh

unread,
Feb 20, 2006, 1:32:32 PM2/20/06
to we...@googlegroups.com
On 2/20/06, nki...@gmail.com <nki...@gmail.com> wrote:
> I am testing on my home Windows system with Apache and python 2.4

The very last line of the traceback shows that line 965 of
flup.fcgi_base.py is attempting to call socket.fromfd(). This
functionality is not present in Windows, so that's the immediate cause
of your problem. As for how to get web.py working under CGI on
windows, your guess is as good as mine.

Aaron Swartz

unread,
Feb 20, 2006, 1:55:05 PM2/20/06
to we...@googlegroups.com
To get it to work on Windows, you'll have to comment out these lines
in fcgi_base.py:

sock = socket.fromfd(FCGI_LISTENSOCK_FILENO, socket.AF_INET,
socket.SOCK_STREAM)
try:
sock.getpeername()
except socket.error, e:
if e[0] == errno.ENOTSOCK:
# Not a socket, assume CGI context.
isFCGI = False
elif e[0] != errno.ENOTCONN:
raise

and underneath them place a line saying:

isFCGI = False

Hope this helps.

nki...@gmail.com

unread,
Feb 21, 2006, 7:41:58 PM2/21/06
to web.py
Thank you, this worked perfectly!

Cuyler

unread,
Mar 23, 2006, 3:20:22 PM3/23/06
to web.py
nki...@gmail.com wrote:
> Thank you, this worked perfectly!

I just tested this with IIS and it works as well (under XP and 2003).

Reply all
Reply to author
Forward
0 new messages