[Tutor] cgi client-pull

Vineeth S vineeth at gmail.com
Mon Sep 13 14:45:24 CEST 2004


Hi all,

I have been trying to get a cgi script to display a wait status until
a process completes, and then display the results.

so the way i figured i could do this was :
call the cgi script
check if there is a flag set, if there is a flag set do a client pull
with a refresh in the headers and a url to the same script
if the flag is not, do a fork-exec to get the actual job running

for some reason, this is not working, the request just waits for the
process including the fork-exec to finish. i even tried an explicit
exit on the process.

i am pasting the code below. (a weekend has been eaten away :-( )

tia
vineeth

#!/usr/bin/python
import cgi
from math import sqrt
import sys
import os

def genPrimes(num):

        primes = []

        for toChek in range(3, num):
                primeFlag = 1
                for div in range(2, int(sqrt(toChek))+1):
                        if not (toChek % div):
                                primeFlag = 0
                                break
                if primeFlag:
                        primes.append(toChek)
        return primes


form = cgi.FieldStorage()



if form.has_key("repFlag"):
        if os.access("/tmp/done", os.F_OK):
                print "Content-Type: text/html\n\n"
                print "<html>"
                print "<body>"
                print "The output"

                for num in open("/tmp/op","r").readlines():
                        print "%s" % num.strip()
                print "</body>"
                print "</html>"
        else:
                print "Content-Type: text/html\n\n"
                print """
                        <html>
                        <head>
                        <META HTTP-EQUIV="Refresh" CONTENT="1;
URL=http://10.1.38.233/cgi-bin/sessions.py?repFlag=1&sid=s123">
                        </head>
                        <body>
                                Please wait the results will be
fetched in a short time. HeHeHe.
                        </body>
                        </html>
              """
                sys.stdout.flush()



else:       
        try:
                os.remove("/tmp/done")
        except OSError:
                pass

        print "Content-Type:  text/html\n\n"

        print """
                        <html>
                        <head>
                        <META HTTP-EQUIV="Refresh" CONTENT="1;
URL=http://10.1.38.233/cgi-bin/sessions.py?repFlag=1&sid=s123">
                        </head>
                        <body>
                                Please wait the results will be
fetched in a short time
                        </body>
                        </html>
              """
        sys.stdout.flush()

        pid = os.fork()
        if pid == 0:
               
os.execlp('python','python','getPrimes.py',form['num'].value.strip())
        else:
                sys.exit()


More information about the Tutor mailing list