beautypg.com

Example server source code, Kdt750 apl guide – AML KDT750 Price Verification System User Manual

Page 22

background image

KDT750 APL Guide

3 - 4

Example Server Source Code

import os

import cgi

import urllib

form = cgi.FieldStorage()

inputdata = ""

### Note that urllib.quote method is used to URL encode all outbound data

# Handle button presses (just reply with the button name)

if form.has_key('BUT') :

button = form["BUT"]

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

# Header is important

print "FIELD1=” + urllib.quote ( “You pressed button " + button.value )

print "@FIELD1=” + urllib.quote ( “100,165,440,35,200,220" )

# Handle MSR read

elif form.has_key('MSR') :

fld = form["MSR"]

inputdata = fld.value

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

print "MAGDATA=" + urllib.quote( inputdata )

print "@MAGDATA=” + urllib.quote ( “20,250,600,45,0,50"

# Handle internal bar code read

elif form.has_key('BAR') :

fld = form["BAR"]

inputdata = fld.value

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

# Handle the database lookup

price = do_database( "SELECT Cost FROM 'Inventory' WHERE PN='" + inputdata )

des = do_database( "SELECT Desc FROM 'Inventory' WHERE PN='" + inputdata )

if price != "" :

# URL encode the data and dump it out

print "PRC=" + urllib.quote ( price )

print "DES=" + urllib.quote ( des )

print "@DES=" + urllib.quote ( "45,150,500,35,255,220" )

print "PN=" + urllib.quote ( "P/N: " + inputdata )

print "@PN=" + urllib.quote ( "200,200,240,35,35,50" )

print "RELAY=" + urllib.quote ( "1,3,2" )

else :

# Display an error to the user

print "FIELD1=" + urllib.quote ( " I'm sorry, I couldn't" + '\n' +

" find that item." + '\n' + '\n' + "Would you like assistance?" );

print "@FIELD1=" + urllib.quote ( "125,145,510,250,219,255" )

print "FIELD2=" + urllib.quote ( "Press for assistance" + '\n' + "v" )

print "@FIELD2=" + urllib.quote ( "15,370,340,75,255,220" )

else :

# No valid ITEM names, send blank reply to show an error

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