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

# Connects to the database server scripts_servdb.py
# displays number of records and prompts for sort order.
# upon submit calls the script_sortall.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 PromptSortOrder():
   """
   Display a form to ask user for
   field to sort on and the order.
   """
   print """
<form action="http://localhost/cgi-bin/scripts_sortall.py">
<p>
sort by
<input type="radio" name="sort" value = 0 checked> type
<input type="radio" name="sort" value = 1> date
<input type="radio" name="sort" value = 2> name
<br>
order is
<input type="radio" name="order" value = True checked> ascending
<input type="radio" name="order" value = False> descending
</p>
<p> <input type="submit"> </p>
</body>
</html>
"""

def main():
   """
   Connects and prints data of server.
   """
   PrintHeader('sorting 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
   PromptSortOrder()
   client.close()

main()
