Python Web简单加法器的实现--Python

坚持写博客来记录学习过程,哪怕学习的东西多么简单!下面是python中cgi相关知识。

Template.py(模板引擎文件)

#模板引擎
def start_response(resp="text/html"):
  return 'Content-type: '+resp+ '\n\n'

def start_form(the_url="",form_type="POST"):
  return '<form action="'+the_url+'"method="'+form_type+'">'

def end_form(submit_msg="求和"):
  return '<input type="submit" value="' +submit_msg+'">'

def input_label(name,placeholder="",value="",readonly=None):
if readonly is None:
  return '<input type="text" size="20" value="'+value+'" name="'+name+'" placeholder="'+placeholder+'">'
else:
  return '<input type="text" size="20" value="'+value+'" readonly="'+readonly+'" name="'+name+'" placeholder="'+placeholder+'">'

def context(word):
return word

def start_div(align,style):
  return '<div align="'+align+'"style="'+style+'">'
def end_div():
  return "</div>"
def img(src):
  return '<img src="'+src+'">

main.py

import cgitb,cgi
from Template import *
form1=cgi.FieldStorage()
num1=form1.getvalue("Num1")
num2=form1.getvalue("Num2")
num3=None
if not num1 is None and not num2 is None:
  num1=int(num1)
  num2=int(num2)
  num3=num1+num2
# print("Content-type:text/html \n\n")
print(start_response())
# print(img("../views/add.png"))
# print(start_div("center","margin-top:40px;"))
# print(img("../views/add.png"))
# print(end_div())
print(start_div("center","margin:50px 0 0 0"))
print(start_form())
print(input_label("Num1","参数1"))
print("+")
print(input_label("Num2","参数2"))
print("=")
if num3 is None:
  print(input_label("Num3","结果","","readonly"))
else:
print(input_label("Num3", "结果",str(num3), "readonly"))
print(end_form())
print(end_div())

posted @ 2018-03-11 16:48  牛新龙的IT技术博客  阅读(1623)  评论(0编辑  收藏  举报