localhost:8000/cgi-bin/hello.cgi
โครงสร้างของ HTML
#!/usr/bin/env python # -*- coding: UTF-8 -*- #!/usr/bin/python import cgi print "Content-type: text/html;charset=utf-8 \n\n" print "<html>" print "<head><title>Test Calculate</title></head>" print "<body>" print """ <div align="center"><font color="DodgerBlue3" size="25" face="Comic Sans MS"><b> Building Software Systems II </b></font></div> """ print """ <div align="center"><font color="DodgerBlue3" size="5" face="Comic Sans MS"><b> การพัฒนาระบบซอฟต์แวร์ 2 </b></font></div> """ print '<div align="center"><h1>Hello Calculate Program!</h1></div>' print '<div align="center"><form method="post" action="hello.cgi">' print '<p><input type="text" name="input1"/></p>' print '<input type="submit" name="+" value="บวก"/>' print '<input type="submit" name="-" value="ลบ"/>' print '<input type="submit" name="*" value="คูณ"/>' print '<input type="submit" name="/" value="หาร"/>' print '<p><input type="text" name="input2"/></p>' print '</form></div>' form = cgi.FieldStorage() input1 = form.getvalue("input1") input2 = form.getvalue("input2") print '<div align="center">' try: if form.getvalue("+"): print '<h1>'+input1+" + "+input2+" = "+str(int(input1)+int(input2))+'</h1><br/>' elif form.getvalue("-"): print '<h1>'+input1+" - "+input2+" = "+str(int(input1)-int(input2))+'</h1><br/>' elif form.getvalue("*"): print '<h1>'+input1+" * "+input2+" = "+str(int(input1)*int(input2))+'</h1><br/>' elif form.getvalue("/"): if int(input1) == 0 or int(input2) == 0: print '<h1>Error</h1>' else: print '<h1>'+input1+" / "+input2+" = "+str(float(input1)/float(input2))+'</h1><br/>' except: print '<h1>Input Error</h1>' print '</div>' print "</body>" print "</html>"
ผลการทดสอบ
Input 3 + 6
Input 39 - 19
Input 3 - 22
Input 5 * 20
Input 0 / 10
Input 5 / 8
Input 4 + y
ไม่มีความคิดเห็น:
แสดงความคิดเห็น