Python Forum
HTML+CGI (if __name__ == '__main__')
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HTML+CGI (if __name__ == '__main__')
#1
Hi, I am new to Python + CGI. my application is to searching multiple file using parallel processing. each process return the output any time. I want to process that ourput and place in HTML table. here is mycode.
what I abserved the html table is updating proper if print before "if __name__ == '__main__':".

Can you point me what is the mistake in the script?


Top CGI (page.cgi):
#!/usr/bin/python
import cgi, cgitb
cgitb.enable()
print ("Content-type:text/html\r\n\r\n");
print ('');

print ('<html>');
print ('<form action = "./searchEngine.cgi" method = "post">');
print ('First Name: <input type = "text" name = "first_name">  <br />');
print ('Last Name: <input type = "text" name = "last_name" />');
print ('<input type = "submit" value = "Submit" />');
print ('</form>');
print ('</html>');
searchEngine.cgi:
#!/usr/bin/python

import multiprocessing as mp
import re
import os
import sys
import linecache
import cgi, cgitb
cgitb.enable()

results = [];
# Create instance of FieldStorage 
#form = cgi.FieldStorage() 

# Get data from fields
#first_name = form.getvalue('first_name')
#last_name  = form.getvalue('last_name')

def FileSearch(fileName,pattern):
    fName = "";
    lineNo = "";
    regNam = ""
    f = open(fileName,"r+" )
    for lines in f.readlines():
        matchObj = re.search(pattern,lines)
        regName = re.search("^time",lines)
        fileN = re.search("^[place]",lines)
        st = str.split(lines,":");
        if regName:
            regN = len(st);
            regNam = st[regN-1];
        if fileN:
            fileNN = len(st);
            fName = st[fileNN-2];
            lineNo = st[fileNN-1];
        if matchObj:
            lineNo = int(st[0]);
            tp = linecache.getline(fName,lineNo);
            return(regNam.strip() ,fName.strip(),st[0],tp.strip());
    f.close();

def collect_result(result):
        global results
        sd = result;
        if sd != None:
            results.append(result);
            print('<tr> <td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>' % ( sd[0], sd[1],sd[2],sd[3]));   [b[i]]#### this print is not working[/i]….[/b]

def main():
     ### print('<tr> <td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>' % ( sd[0], sd[1],sd[2],sd[3]));   ## this is working
    if __name__ == '__main__':
        global results;
        pool = mp.Pool(mp.cpu_count());
        f = open("./list","r" );
        data = f.readlines();
        pool = mp.Pool()
        for i, row in enumerate(data):
            pool.apply_async(FileSearch,args=(row.strip(),"name"), callback=collect_result);
        pool.close();
        pool.join();  # Wait for all subprocesses to finish.
        f.close();
        for sd in results:
            if sd != None:
                print("<tr> <td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>" % ( sd[0], sd[1],sd[2],sd[3]));


def htmlHead():
    print ("Content-type:text/html\r\n\r\n");
    print ('');
    print('<html>');
    print('<head>');
    print('<title>Python CGI test</title>');
    print('</head>');
    print('<body>');
    print('<table id="myTable1" border = "1" width = "35%" style="margin-top:10px; margin-left:100px;">');
    print('<tr>');
    print('<th id=100> Name </th>');
    print('<th id=101> Place </th>');
    print('<th id=102> Time </th>');
    print('</tr>');

def htmlTail():
    print('</table>');
    print('</body>');
    print('</html>');

htmlHead();
main();
htmlTail();
Reply


Messages In This Thread
HTML+CGI (if __name__ == '__main__') - by mahesh - Feb-19-2019, 11:49 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  HTML multi select HTML listbox with Flask/Python rfeyer 0 4,759 Mar-14-2021, 12:23 PM
Last Post: rfeyer
  Python3 + BeautifulSoup4 + lxml (HTML -> CSV) - How to loop to next HTML/new CSV Row BrandonKastning 0 2,422 Mar-22-2020, 06:10 AM
Last Post: BrandonKastning

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020