On Sun, Jan 24, 2010 at 10:53 AM, Menglong Tan <tanmenglong@gmail.com> wrote:
all.  Im just tring to get web.py work with the help of spawn-fcgi with yaws. But I encountered a problem:

<server localhost>
        port = 80
        listen =
        appmods = </test, yaws_appmod_fcgi>
        docroot = /usr/local/var/yaws/www
        fcgi_app_server =
        fcgi_trace_protocol = true

#!/usr/bin/env python
import web

urls = ("/.*", "hello")
app = web.application(urls, globals())

class hello:
    def GET(self):
        return 'Hello, world!'

if __name__ == '__main__':
    web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)

run spawn-fcgi:
spawn-fcgi -d /root/test -f /root/test/index.py -a -p 3000

but when I visited http://localhost/test, it returned a blank page with 200.
Then I modified the index.py, remove the if __name__ == '__main__':

visited again, returned an exception: 

<type 'exceptions.ValueError'> at /apps/testapp

signal only works in main thread

Im new to fcgi, so turn to maillist for help after searching the whole Internet without getting anything.

I'm certainly no expert, but I've experimented a bit and found that if you restore index.py to the way you originally had it, and then add this line to class hello's GET method, just before the return statement:

        web.header('Content-Type', 'text/plain')

the returned string will be served back correctly to the web client in chunked transfer mode. If you want to avoid chunked transfer, do this:

class hello:
    def GET(self):
        out = 'Hello, world!'
        web.header('Content-Type', 'text/plain')
        web.header('Content-Length', len(out))
        return out

Any decent HTTP client should be able to handle either returned format, though.

With this in place, I see the following if I run curl as my client:

$ curl -D /dev/tty http://localhost:8000/test
HTTP/1.1 200 OK
Server: Yaws/1.87 Yet Another Web Server
Date: Sun, 24 Jan 2010 17:56:44 GMT
Content-Length: 13
Content-Type: text/plain

Hello, world!

You can make the string into HTML and return text/html as the content type as well:

class hello:
    def GET(self):
        out = '<html><body>Hello, world!</body></html>'
        web.header('Content-Type', 'text/html')
        web.header('Content-Length', len(out))
        return out

hope this helps,