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

Posted Thanlon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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())
技术分享图片

以上是关于Python Web简单加法器的实现--Python的主要内容,如果未能解决你的问题,请参考以下文章

python 简单的Pytho WebSocket

python简单LWE 加解密方案实现,支持密文同态加法以及常数乘法

Web开发Python实现Web服务器(Flask打包部署上线)

python PyQt5 制作简单计算器

01 Web框架介绍

用Python在Flask框架下实现加法器,其中加法应用了python的子程序调用