Help with CGI functions on python.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cuties
    New Member
    • Jan 2007
    • 8

    Help with CGI functions on python.

    Hi all....

    i'm very new to this programming language. i'm required to fulfill this task in the company i'm doing my practical. i hope i can get guide for my problem...
    Here is the script i already wrote but i'm having problem to move forward.

    #!C:\python24\p ython.exe

    ####Code for viewing database....or search function#######

    print 'Content-type: text/html\n\n'

    import cgi # importing cgi module

    import MySQLdb #importing MySQLdb module

    form = cgi.FieldStorag e() #assigning form to the cgi.FieldStorag e

    attribute = form.getvalue(" attribute") #assigning attribute to get value from the function called attribute(drop down box) in the form

    search = form.getvalue(" search") #assigning search to get value from the function called search(textbox) in the form

    conn =MySQLdb.connec t(host = 'localhost', db='LESS', user ='root', passwd='') #creating conn to connect to MySQL

    curs = conn.cursor() #creating a cursor called curs

    curs.execute("u se LESS") # exucuting the database

    table = "download_f ile" #assignin table to download_file which is retrieved from MySql "less" under tables
    #(download_file is a table name in less)

    query = "select * from %s where %s like '%s'" %(table, attribute, search) #assigin query to MySQL command of select

    #print query
    curs.execute(qu ery) #executing command set in the query function

    count = curs.rowcount #indicating the cursor to count the rows
    ######## start of HTML code ##########
    page= '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
    <html xmlns="http://www.w3.org/1999/xhtml
    <head><title>Vi ew Database</title>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link rel="stylesheet " href="http://localhost/Pictures/mm_travel2.css" type="text/css" />

    <style type="text/css">
    <!--
    .style1 {font-family: Georgia}
    .style2 {font-family: "Franklin Gothic Medium"}
    .style3 {
    font-family: "Franklin Gothic Medium";
    font-weight: bold;
    font-size: x-small;
    font-style: italic;
    }
    .style4 {font-family: Georgia; font-size: 12px; }
    .style5 {font-family: "Comic Sans MS"; font-weight: bold; font-size: large; color: #3300FF; }
    .style6 {
    font-size: large;
    font-family: "Comic Sans MS";
    font-style: italic;
    font-weight: bold;
    }
    .style7 {font-size: large; font-family: "Comic Sans MS"; font-style: italic; color: #CC66CC; }
    .style8 {font-family: "Times New Roman"; color: #000000; }
    .style9 {
    font-family: Arial;
    font-weight: bold;
    }
    .style10 {
    font-family: Roman;
    font-size: 18px;
    }
    .style11 {color: #000000}
    .style12 {
    font-family: "Times New Roman";
    font-style: italic;
    }
    .style13 {color: #000000; font-family: "Courier New"; }
    .style14 {font-size: large; font-weight: bold; color: #006600; font-family: Verdana; }
    .style15 {color: #006699;
    font-weight: bold;
    }
    .style16 {color: #006699; font-weight: bold; font-size: 16px; }
    -->
    </style>

    <script language="JavaS cript" type="text/javascript">

    var d=new Date();
    var monthname=new Array("January" ,"February","Ma rch","April","M ay","June","Jul y","August","Se ptember","Octob er","November", "December") ;
    var TODAY = monthname[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear() ;
    </script>
    </head>
    <body bgcolor="#C0DFF D">
    <span class="style15" ><img src="http://localhost/Pictures/f-secure.JPG" alt="Header image" width="153" height="144" border="0" /></span>
    <h1 align="center">
    <script language="JavaS cript1.2">
    var message="LESS File Collection!"
    var neonbasecolor=" #99FF66"
    var neontextcolor=" #CC3399"
    var flashspeed=100 //in milliseconds
    var n=0
    if (document.all|| document.getEle mentById){
    document.write( '<font color="'+neonba secolor+'">')
    for (m=0;m<message. length;m++)
    document.write( '<span id="neonlight'+ m+'">'+message. charAt(m)+'</span>')
    document.write( '</font>')
    }
    else
    document.write( message)
    function crossref(number ){
    var crossobj=docume nt.all? eval("document. all.neonlight"+ number) : document.getEle mentById("neonl ight"+number)
    return crossobj
    }
    function neon(){
    if (n==0){
    for (m=0;m<message. length;m++)
    crossref(m).sty le.color=neonba secolor
    }
    crossref(n).sty le.color=neonte xtcolor
    if (n<message.leng th-1)
    n++
    else{
    n=0
    clearInterval(f lashing)
    setTimeout("beg inneon()",1500)
    return
    }
    }
    function beginneon(){
    if (document.all|| document.getEle mentById)
    flashing=setInt erval("neon()", flashspeed)
    }
    beginneon()
    </script>

    <table width="104%" border="0" cellspacing="0" cellpadding="0" >
    <tr>
    <td colspan="4" bgcolor="#00336 6"><img src="http://localhost/Pictures/spacer.gif" alt="" width="1" height="1" border="0" /></td>
    </tr>

    <tr bgcolor="#CCFF9 9">
    <td>&nbsp;</td>
    <td colspan="3" id="dateformat " height="25"><a href="LESS.html ">HOME</a> ::<a href="Collectio n.html">Collect ion</a> ::<a href="Info.html ">Info</a> ::<a href="Sources.h tml">Sources</a> :: <script language="JavaS cript" type="text/javascript">
    document.write( TODAY); </script> </td>
    </tr>
    <tr>
    <td colspan="4" bgcolor="#00336 6"><img src="http://localhost/Pictures/spacer.gif" alt="" width="1" height="1" border="0" /></td>
    </tr>
    <tr>
    <td width="4">&nbsp ;</td>
    <td colspan="2" valign="top"><p >&nbsp;</p>
    <p>&nbsp;</p></td>
    <td width="590">&nb sp; </td>
    </tr>
    </table>
    '''
    print page
    print "<p><div align=\"center\ "><h1>Searc h with %s Results</h1>" %count
    print "<i>Searchi ng for %s with %s</i> </div></p>" %(attribute, search)
    print "<form method=post action=http://localhost/cgi-bin/Raulf/next.py>"
    print "<table border = 1 cellpadding =0 cellspacing = 0>"
    ##### End of HTML code ######

    ###### start of Html and python code for the header of the search table #######
    print "<tr bgcolor=\"#cccc cc\">"
    print "<td></td>"

    h = 0 #setting the values of the attribute as zero

    while h < 9: # total attribute is 0-8 so while h is less than 9
    desc = curs.descriptio n[h]
    print "<th>%s</th>" %desc[0]
    h += 1 #increase the h value.
    print "</tr>"
    print "<p align=right><in put type=submit value=Download> &nbsp; <input type=submit value=Delete></p>"

    ###### End of code #####

    ###### start of Html and python code for the result of the search table #######

    no_of_rows = 0 #setting the number of rows as zero



    while True: #using while for true

    row = curs.fetchone() #asking cursor to fetch one data at a time for a row
    if not row: break #saying if its not a row the break it.

    if (no_of_rows % 2): # for even no of row
    print "<tr bgcolor=\"#dfdf df\">" # if code is even, use such color for the cell
    print "<td><input type=checkbox name=tick value=",form["attribute"].value=='d_id', "></td>"
    i = 0
    while i < 9:
    print "<td>%s</td>" %row[i]
    i += 1
    else:
    print "<td><input type=checkbox name=tick value=", form["attribute"].value=='d_id', "></td>"
    i = 0
    while i < 9:
    print "<td>%s</td>" %row[i]
    i += 1
    no_of_rows += 1
    print "</tr>"
    print "</table>"

    print "<div align = \"center\">< a href=\"http://localhost/Raulf/show_all.html\" >Back</a>"
    print "<input type=submit value=Submit>"
    print "</form></body></html>"
    my problem is :

    1. how do i assign each checkbox to have equal value with the value of the d_id?

    2. when the checkbox is clicked or check and when the submit button is pressed, how can i produce a new page showin me all the d_id of the check box that is clicked?

    3. this is somethin like email function. when the checkbox is checked and delete button is pressed, how do i remove the checked files from my database?

    4. finally if the checked box is checked and the download button is pressed, how do i download that particular file into the assignned folder, in this case assignned folder is C:\temp. If multiple box are checked, the file should be zipped and download in the assignned folder. how could i reach to this?

    please assist me to this. i also will attach a snapshot.

    Actually the codes above is a cgi for this codes:
    <html>
    <head>
    <title>Source Downloader for LESS
    </title>
    <body>
    <div align="left"><h 1>Find record in LESS</h1></div>
    <form method="POST" action="http://localhost/cgi-bin/Raulf/show_all.py">
    <p>Select:
    <select name ="attribute" >
    <option value="d_id">Fi le ID</option>
    <option value="filename ">Filename</option>
    <option value="extensio n">Extension </option>
    <option value="size">Si ze(Bytes)</option>
    <option value="url">Dow nloaded from</option>
    <option value="location ">Location in LESS</option>
    <option value="md5">MD5 </option>
    <option value="SHA1">SH A1</option>
    <option value="time_add ">Date Entered</option>
    </select>
    <input type= text name="search">
    <br>
    <input type="submit" value="Submit">
    <input type="reset" value="Reset">
    </body>
    </html>

    very sorry. i'm extremely new to this and i have to complete this asap. can pls help me with the codes? thanks
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    hi

    can you put your code in "code" tags, not "quote" tags.
    thanks

    Comment

    • cuties
      New Member
      • Jan 2007
      • 8

      #3
      sorry abt tat error....

      Code:
      <html>
      <head>
      <title>Source Downloader for LESS
      </title>
      <body>
      <div align="left"><h1>Find record in LESS</h1></div>
      <form method="POST" action="http://localhost/cgi-bin/Raulf/show_all.py">
      <p>Select:
      <select name ="attribute">
      <option value="d_id">File ID</option>
      <option value="filename">Filename</option>
      <option value="extension">Extension</option>
      <option value="size">Size(Bytes)</option>
      <option value="url">Downloaded from</option>
      <option value="location">Location in LESS</option>
      <option value="md5">MD5</option>
      <option value="SHA1">SHA1</option>
      <option value="time_add">Date Entered</option>
      </select>
      <input type= text name="search">
      <br>
      <input type="submit" value="Submit">
      <input type="reset" value="Reset">
      </body>
      </html>
      Code:
      #!C:\python24\python.exe
      
      ####Code for viewing database....or search function#######
      
      print 'Content-type: text/html\n\n'
      
      import cgi # importing cgi module
      
      import MySQLdb #importing MySQLdb module
      
      form = cgi.FieldStorage() #assigning form to the cgi.FieldStorage
      
      attribute = form.getvalue("attribute") #assigning attribute to get value from the function called attribute(drop down box) in the form
      
      search = form.getvalue("search") #assigning search to get value from the function called search(textbox) in the form
      
      conn =MySQLdb.connect(host = 'localhost', db='LESS', user ='root', passwd='') #creating conn to connect to MySQL
      
      curs = conn.cursor() #creating a cursor called curs
      
      curs.execute("use LESS") # exucuting the database
      
      table = "download_file" #assignin table to download_file which is retrieved from MySql "less" under tables
      #(download_file is a table name in less)
      
      query = "select * from %s where %s like '%s'" %(table, attribute, search) #assigin query to MySQL command of select
      
      #print query
      curs.execute(query) #executing command set in the query function
      
      count = curs.rowcount #indicating the cursor to count the rows
      ######## start of HTML code ##########
      page= '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml
      <head><title>View Database</title>
      
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
      <link rel="stylesheet" href="http://localhost/Pictures/mm_travel2.css" type="text/css" />
      
      <style type="text/css">
      <!--
      .style1 {font-family: Georgia}
      .style2 {font-family: "Franklin Gothic Medium"}
      .style3 {
      font-family: "Franklin Gothic Medium";
      font-weight: bold;
      font-size: x-small;
      font-style: italic;
      }
      .style4 {font-family: Georgia; font-size: 12px; }
      .style5 {font-family: "Comic Sans MS"; font-weight: bold; font-size: large; color: #3300FF; }
      .style6 {
      font-size: large;
      font-family: "Comic Sans MS";
      font-style: italic;
      font-weight: bold;
      }
      .style7 {font-size: large; font-family: "Comic Sans MS"; font-style: italic; color: #CC66CC; }
      .style8 {font-family: "Times New Roman"; color: #000000; }
      .style9 {
      font-family: Arial;
      font-weight: bold;
      }
      .style10 {
      font-family: Roman;
      font-size: 18px;
      }
      .style11 {color: #000000}
      .style12 {
      font-family: "Times New Roman";
      font-style: italic;
      }
      .style13 {color: #000000; font-family: "Courier New"; }
      .style14 {font-size: large; font-weight: bold; color: #006600; font-family: Verdana; }
      .style15 {color: #006699;
      font-weight: bold;
      }
      .style16 {color: #006699; font-weight: bold; font-size: 16px; }
      -->
      </style>
      
      <script language="JavaScript" type="text/javascript">
      
      var d=new Date();
      var monthname=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
      var TODAY = monthname[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear();
      </script>
      </head>
      <body bgcolor="#C0DFFD">
      <span class="style15"><img src="http://localhost/Pictures/f-secure.JPG" alt="Header image" width="153" height="144" border="0" /></span>
      <h1 align="center">
      <script language="JavaScript1.2">
      var message="LESS File Collection!"
      var neonbasecolor="#99FF66"
      var neontextcolor="#CC3399"
      var flashspeed=100 //in milliseconds
      var n=0
      if (document.all||document.getElementById){
      document.write('<font color="'+neonbasecolor+'">')
      for (m=0;m<message.length;m++)
      document.write('<span id="neonlight'+m+'">'+message.charAt(m)+'</span>')
      document.write('</font>')
      }
      else
      document.write(message)
      function crossref(number){
      var crossobj=document.all? eval("document.all.neonlight"+number) : document.getElementById("neonlight"+number)
      return crossobj
      }
      function neon(){
      if (n==0){
      for (m=0;m<message.length;m++)
      crossref(m).style.color=neonbasecolor
      }
      crossref(n).style.color=neontextcolor
      if (n<message.length-1)
      n++
      else{
      n=0
      clearInterval(flashing)
      setTimeout("beginneon()",1500)
      return
      }
      }
      function beginneon(){
      if (document.all||document.getElementById)
      flashing=setInterval("neon()",flashspeed)
      }
      beginneon()
      </script>
      
      <table width="104%" border="0" cellspacing="0" cellpadding="0">
      <tr>
      <td colspan="4" bgcolor="#003366"><img src="http://localhost/Pictures/spacer.gif" alt="" width="1" height="1" border="0" /></td>
      </tr>
      
      <tr bgcolor="#CCFF99">
      <td>&nbsp;</td>
      <td colspan="3" id="dateformat" height="25"><a href="LESS.html">HOME</a> ::<a href="Collection.html">Collection</a> ::<a href="Info.html">Info</a> ::<a href="Sources.html">Sources</a> :: <script language="JavaScript" type="text/javascript">
      document.write(TODAY); </script> </td>
      </tr>
      <tr>
      <td colspan="4" bgcolor="#003366"><img src="http://localhost/Pictures/spacer.gif" alt="" width="1" height="1" border="0" /></td>
      </tr>
      <tr>
      <td width="4">&nbsp;</td>
      <td colspan="2" valign="top"><p>&nbsp;</p>
      <p>&nbsp;</p></td>
      <td width="590">&nbsp; </td>
      </tr>
      </table>
      '''
      print page
      print "<p><div align=\"center\"><h1>Search with %s Results</h1>" %count
      print "<i>Searching for %s with %s</i> </div></p>" %(attribute, search)
      print "<form method=post action=http://localhost/cgi-bin/Raulf/next.py>"
      print "<table border = 1 cellpadding =0 cellspacing = 0>"
      ##### End of HTML code ######
      
      ###### start of Html and python code for the header of the search table #######
      print "<tr bgcolor=\"#cccccc\">"
      print "<td></td>"
      
      h = 0 #setting the values of the attribute as zero
      
      while h < 9: # total attribute is 0-8 so while h is less than 9
      desc = curs.description[h]
      print "<th>%s</th>" %desc[0]
      h += 1 #increase the h value.
      print "</tr>"
      print "<p align=right><input type=submit value=Download>&nbsp; <input type=submit value=Delete></p>"
      
      ###### End of code #####
      
      ###### start of Html and python code for the result of the search table #######
      
      no_of_rows = 0 #setting the number of rows as zero
      
      
      
      while True: #using while for true
      
      row = curs.fetchone() #asking cursor to fetch one data at a time for a row
      if not row: break #saying if its not a row the break it.
      
      if (no_of_rows % 2): # for even no of row
      print "<tr bgcolor=\"#dfdfdf\">" # if code is even, use such color for the cell
      print "<td><input type=checkbox name=tick value=",form["attribute"].value=='d_id', "></td>"
      i = 0
      while i < 9:
      print "<td>%s</td>" %row[i]
      i += 1
      else:
      print "<td><input type=checkbox name=tick value=", form["attribute"].value=='d_id', "></td>"
      i = 0
      while i < 9:
      print "<td>%s</td>" %row[i]
      i += 1
      no_of_rows += 1
      print "</tr>"
      print "</table>"
      
      print "<div align = \"center\"><a href=\"http://localhost/Raulf/show_all.html\">Back</a>"
      print "<input type=submit value=Submit>"
      print "</form></body></html>"

      Comment

      • cuties
        New Member
        • Jan 2007
        • 8

        #4
        sorry for the error i caused. earlier i also said that i'll attach a snapshot but i'm not available to do it. anyway my question is something like how our email system works. Only extra tasks is der such as user can view results of all the checked output. for question no2 i did try writing a script but i jst don't get the results that i desire. here is the codes

        Code:
        #!C:\python24\python.exe
        
        print 'Content-type: text/html\n\n'
        
        import cgi
        
        import MySQLdb  
        
        form = cgi.FieldStorage() 
        
        tick = form.getvalue("tick")
        
        attribute = form.getvalue("attribute") 
        
        search = form.getvalue("search") 
        
        conn =MySQLdb.connect(host = 'localhost', db='LESS', user ='root', passwd='') 
        
        curs = conn.cursor() 
        
        curs.execute("use LESS")  
        
        page= '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml
        <head><title>Results of Checked Output</title>
        
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <link rel="stylesheet" href="http://localhost/Pictures/mm_travel2.css" type="text/css" />
        
        <style type="text/css">
        <!--
        .style1 {font-family: Georgia}
        .style2 {font-family: "Franklin Gothic Medium"}
        .style3 {
        	font-family: "Franklin Gothic Medium";
        	font-weight: bold;
        	font-size: x-small;
        	font-style: italic;
        }
        .style4 {font-family: Georgia; font-size: 12px; }
        .style5 {font-family: "Comic Sans MS"; font-weight: bold; font-size: large; color: #3300FF; }
        .style6 {
        	font-size: large;
        	font-family: "Comic Sans MS";
        	font-style: italic;
        	font-weight: bold;
        }
        .style7 {font-size: large; font-family: "Comic Sans MS"; font-style: italic; color: #CC66CC; }
        .style8 {font-family: "Times New Roman"; color: #000000; }
        .style9 {
        	font-family: Arial;
        	font-weight: bold;
        }
        .style10 {
        	font-family: Roman;
        	font-size: 18px;
        }
        .style11 {color: #000000}
        .style12 {
        	font-family: "Times New Roman";
        	font-style: italic;
        }
        .style13 {color: #000000; font-family: "Courier New"; }
        .style14 {font-size: large; font-weight: bold; color: #006600; font-family: Verdana; }
        .style15 {color: #006699;
        	font-weight: bold;
        }
        .style16 {color: #006699; font-weight: bold; font-size: 16px; }
        -->
        </style>
        
        <script language="JavaScript" type="text/javascript">
        
        var d=new Date();
        var monthname=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
        var TODAY = monthname[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear();
        </script>
        </head>
        <body bgcolor="#C0DFFD">
        <span class="style15"><img src="http://localhost/Pictures/f-secure.JPG" alt="Header image" width="153" height="144" border="0" /></span>
        <h1 align="center">
        <script language="JavaScript1.2">
        var message="LESS File Collection!"
        var neonbasecolor="#99FF66"
        var neontextcolor="#CC3399"
        var flashspeed=100  //in milliseconds
        var n=0
        if (document.all||document.getElementById){
        document.write('<font color="'+neonbasecolor+'">')
        for (m=0;m<message.length;m++)
        document.write('<span id="neonlight'+m+'">'+message.charAt(m)+'</span>')
        document.write('</font>')
        }
        else
        document.write(message)
        function crossref(number){
        var crossobj=document.all? eval("document.all.neonlight"+number) : document.getElementById("neonlight"+number)
        return crossobj
        }
        function neon(){
        if (n==0){
        for (m=0;m<message.length;m++)
        crossref(m).style.color=neonbasecolor
        }
        crossref(n).style.color=neontextcolor
        if (n<message.length-1)
        n++
        else{
        n=0
        clearInterval(flashing)
        setTimeout("beginneon()",1500)
        return
        }
        }
        function beginneon(){
        if (document.all||document.getElementById)
        flashing=setInterval("neon()",flashspeed)
        }
        beginneon()
        </script>
        
        <table width="104%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td colspan="4" bgcolor="#003366"><img src="http://localhost/Pictures/spacer.gif" alt="" width="1" height="1" border="0" /></td>
          </tr>
        
          <tr bgcolor="#CCFF99">
          	<td>&nbsp;</td>
          	<td colspan="3" id="dateformat" height="25"><a href="LESS.html">HOME</a> ::<a href="Collection.html">Collection</a> ::<a href="Info.html">Info</a> ::<a href="Sources.html">Sources</a> :: <script language="JavaScript" type="text/javascript">
              document.write(TODAY);	</script>	</td>
          </tr>
         <tr>
            <td colspan="4" bgcolor="#003366"><img src="http://localhost/Pictures/spacer.gif" alt="" width="1" height="1" border="0" /></td>
          </tr>
         <tr>
            <td width="4">&nbsp;</td>
            <td colspan="2" valign="top"><p>&nbsp;</p>
            <p>&nbsp;</p></td>
              <td width="590">&nbsp; </td>
          </tr>
        </table>
        '''
        print page
        
        if tick == True:
            print "The d_id", form["tick"].value, "is checked" 
        else: 
            print "Nothing is ticked"
        
        
        
        print "</body></html>"

        Comment

        Working...