#!/Library/Frameworks/Python.framework/Versions/Current/bin/python
# L-26 MCS 275 Fri 14 Mar 2008 : scripts_showall.py

from socket import *

hostname = 'localhost'  # on same host
number = 11267          # same port number
buffer = 80             # size of the buffer

def PrintHeader(title):
   """
   writes title and header of page
   """
   print """Content-type: text/html

<html>
<head>
<title>%s</title>
</head> 
<body>""" % title

def RetrieveTable(s,n):
   """
   Retrieves table of n records,
   using socket s to communicate.
   """
   print "<table>"
   for i in range(0,n):
      data = s.recv(buffer)
      d = data.split(':')
      print "<tr>"
      print "<td>%d</td>" % i
      print "<td>%s</td>" % d[0]
      print "<td>%s</td>" % d[1]
      print "<td>%s</td>" % d[2]
      print "</tr>"
   print "</table>"

def main():
   """
   Connects and prints data of server.
   """
   PrintHeader('showing all scripts')
   server_address = (hostname, number)
   client = socket(AF_INET, SOCK_STREAM)
   client.connect(server_address)
   data = client.recv(buffer)
   n = int(data)
   print "<B>Number of scripts : %d</B>" % n
   RetrieveTable(client,n)
   client.close()

main()
