Verifying if fastcgi is working

11 views
Skip to first unread message

David Montgomery

unread,
Feb 9, 2012, 3:10:59 AM2/9/12
to web.py
Hi,

I have fastcgi working using nginx. How can test a counter to see if
its really working?

Ii I do the below I get this error:

<type 'exceptions.UnboundLocalError'> at /pixel/
local variable 'count' referenced before assignment

If I take out counter the code works.


#!/usr/bin/python
# -*- coding: utf-8 -*-

import web
import redis
import urlparse
#chmod 755 index.py
#spawn-fcgi -d /home/ubuntu/workspace/rtbopsConfig/rtbPixelServer/ -f
/home/ubuntu/workspace/rtbopsConfig/rtbPixelServer/index.py -a
127.0.0.1 -p 9001

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

gobal count = 0
class index:
def GET(self):
env = web.ctx['environ']
QUERY_STRING = env['QUERY_STRING']
count = count + 1
return 'Hello pixel count qs %s %s' %
(count,str(urlparse.parse_qs(QUERY_STRING)))

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

Alex Quinn

unread,
Feb 18, 2012, 9:52:33 AM2/18/12
to web.py
Hi David,

The code below worked for me to just verify that FastCGI was working.
That was on Apache, but I can't imagine why that would matter. If
FastCGI is working, then after refreshing a few times you should see
the counter increase and the process ID stay the same. With plain
CGI, you'd see the process ID change and the counter would stay the
same.

The main problem was just that your "global count" statement needed to
be inside your GET() method.

I'm not sure what you were doing with the query string, so I replaced
that with a snippet that pretty-prints everything in web.ctx,
including the environment. The differences in the environment between
FastCGI and plain CGI tend to be subtle, but it's worth knowing what
to expect.

Sorry it took so long to get a response to this.

--Alex


____________________________________________________________
import os, web, pprint

urls = ('/', 'IndexPage')

count = 0

class IndexPage(object):
def GET(self):
global count
count += 1
return "\n\n".join([
"Count: %d"%count,
"Process ID: %d"%(os.getpid()),
pprint.pformat(dict(web.ctx))
])

app = web.application(urls, globals())

web.config.debug = True

if __name__=="__main__":
web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func,
addr)
app.run()
____________________________________________________________
Reply all
Reply to author
Forward
0 new messages