[Tutor] Question on open and read html files and psycopg

Ms Soo Chong s4046441 at student.uq.edu.au
Tue Sep 14 12:41:09 CEST 2004


Hi all,

Thanks for your reply (Lee Harr).

I have a question regarding python script opening and reading HTML files. Can python 
read a html file containing frames? Because when I include this portion in my script:

-------------------------------------------------------------------------------------
fp=open("/var/www/cgi-bin/sf/frame.html", "r")
    listOfLines= fp.readlines()
    for eachline in listOfLines:
        print eachline.strip()
    fp.close()
-------------------------------------------------------------------------------------

This is the frame.html:

-------------------------------------------------------------------------------------
<html>
<head>
<title>T4 Web Browser</title></head>
<frameset rows="80,*" BORDER=0>
<frame name="banner" scrolling="no" noresize target="contents" src="title_bar.html"> <frameset cols="150,*" border=1>
<frame name="contents" target="main" src="side_bar.html" scrolling="auto">
<frame name="main" src="trial1.html" scrolling="auto">
</frameset>
<noframes><p>Hi, the page you are attempting to enter has frames and if you're reading this message, you don't have the ability to see it. In order to view this  page, you will need to update your browser.&nbsp;<p>Updates are available from 
<a href="http://www.microsoft.com/">www.microsoft.com</a> for Internet Explorer or <a href="http://www.netscape.com/">www.netscape.com</a> for Netscape Navigator.
</noframes>
</frameset>
</html>

--------------------------------------------------------------------------------------

what the web browser returned is a web page containing the frames that I have set up 
but not the contents of them. The frame.html consists of three more html files - title_bar.html, side_bar.html and trial1.html. That is trial1.py is capable of opening
up and reading the frame.html file and display the frames but can't read the rest. Thus, when i changed the frame.html to any of the html files, trial1.py runs perfectly.
I just wonder if anyone can tell me whether I'm right that python can't read html files
within another html file. (I hope someone get what I said);O 
Or please explain to me why does this happened? Thanks for any help.


One more question on psycopg

Finally, my machine is installed with psycopg and I'm wanna to try out it. Because,
I'm a newbie so I find it difficult to write a program that is capable of querying the database and display the result via a web browser whenever a user do a keyword search.
I have a postgreSQL DB up and running, my problem is with the code. Are there anyone that is willing to share his/her source code that does similar stuffs as mine do?
Any help will be greatly appreciate. Thank you.


Cheers,
Shufen






-------------- next part --------------
#!/usr/bin/env python
#Author: Chong Soo Fern
#Created on: 31/08/04

import sys
sys.stderr = sys.stdout

#Import the CGI module.
import cgi

#Turn on CGI debugging info.
import cgitb; cgitb.enable()


#Required header that tells the browser how to render the HTML.
print"Content-Type: text/html\n\n"

#Get the content from the HTML file.
try:
    fp=open("/var/www/cgi-bin/sf/trial1.html", "r")
    listOfLines= fp.readlines()
    for eachline in listOfLines:
        print eachline.strip()
    fp.close()
except Exception:
    print 'hello 2'


#Get the form data, if any
form = cgi.FieldStorage()

#No form data means this is the first access; output the form.

if not form.has_key("data"):
    print"""<FORM METHOD="POST" ACTION="/cgi-bin/sf/trial1.py">

<P>
Type your query here:<BR>
<TEXTAREA NAME=data VALUE="" ROWS=3 COLS=90 TYPE=text></TEXTAREA><BR>
<P>
<INPUT TYPE=submit VALUE="Submit Query"><BR>
</FORM>"""

else:
    #We do have form data; just show it.
    print "You typed this:"
    
    my_query = form.getvalue("data")
    print my_query
    # It seems that we need to have an appropriate username that matches
    # an entry in the postgresql table of users.
    import os
    username = os.environ.get('USER')
    # print "username: ", username, type(username)
    if username == None:
        # Assume that the web server has started this script and has the
        # username 'apache'.  To allow this to access the database, we had
        # to create a postgresql user of that name and have no password.
        # This new user is also able to create tables and new users,
        # otherwise the access seems to be blocked.  (This might be a
        # security problem but only for our database tables.)
        username = 'apache'

    # Now, we can get to the database...
    import pg
    db = pg.connect("moncdata", user=username, passwd=None)
    qresult = db.query(my_query)
    listOfResults = qresult.dictresult()
    # Have a look at http://www.pygresql.org/README.txt to get documentation
    # on the pgqueryobject methods.
    # Make sure that we have a string.
    resultString = repr(listOfResults)
    print "<P>Raw result obtained from database:</P>"
    print resultString

    print ""
    print "<P>Example of pulling the list of dictionary results apart.</P>"
    for record in listOfResults:
        print "<P><table>"
        for k in record.keys():
            print "<tr> <td>key:</td> <td>", k, "</td> <td>value:</td> <td>", \
                  record[k], "</td> </tr>"
        print "</table></P>"
    db.close()

#HTML end
print"</BODY></HTML>"




        




More information about the Tutor mailing list