Python CGI 取代了我的 html
Python CGI replaces my html
我正在用 python 自学 CGI,但我无法找到关于如何将 python 程序的输出插入我当前的 html 而不是替换的任何参考资料整个现有页面。
这是 python:
#!python
import cgi, cgitb
cgitb.enable()
form = cgi.FieldStorage()
def loan(r, term, pv):
if r is not None:
r = float(r)
if term is not None:
term = float(term)
if pv is not None:
pv = float(pv)
rate = r / 12
numPeriods = 12 * term
rate /= 100
monthlyPayment = (pv * rate) / (1 - (1 + rate) ** (numPeriods * -1))
return round(monthlyPayment, 2)
def getInput():
r = form.getvalue("rate")
term = form.getvalue("term")
pv = form.getvalue ("amount")
monthlyPayment = loan(r, term, pv)
print("<p>Monthly Payment: %.2f<p>" % monthlyPayment)
print "Content-type: text/html\r\n\r\n"
print """
<div>
<h1> Python CGI </h1>
<br />
"""
getInput()
print """
<br />
</div>
"""
和 HTML:
<DOCTYPE html>
<html>
<head>
<title> Python CGI Test </title>
</head>
<body>
<h1> Mortage Loan Calculator CGI</h1>
<form action ="/cgi-bin/myPython.py" method ="get">
Rate: <input type = "text" name = "rate"/> <br />
Term: <input type = "text" name = "term"/> <br />
Amount <input type = "text" name = "amount"/> <br />
<button type = "submit" > submit </button>
</form>
</body>
</html>
我想将 python 脚本中的 html 插入到表单下方。
这是服务器端,这意味着一旦加载页面,从 Python 重新加载内容的唯一方法是重新加载或 AJAX。在将内容发送回用户之前,所有代码将 运行。
我正在用 python 自学 CGI,但我无法找到关于如何将 python 程序的输出插入我当前的 html 而不是替换的任何参考资料整个现有页面。
这是 python:
#!python
import cgi, cgitb
cgitb.enable()
form = cgi.FieldStorage()
def loan(r, term, pv):
if r is not None:
r = float(r)
if term is not None:
term = float(term)
if pv is not None:
pv = float(pv)
rate = r / 12
numPeriods = 12 * term
rate /= 100
monthlyPayment = (pv * rate) / (1 - (1 + rate) ** (numPeriods * -1))
return round(monthlyPayment, 2)
def getInput():
r = form.getvalue("rate")
term = form.getvalue("term")
pv = form.getvalue ("amount")
monthlyPayment = loan(r, term, pv)
print("<p>Monthly Payment: %.2f<p>" % monthlyPayment)
print "Content-type: text/html\r\n\r\n"
print """
<div>
<h1> Python CGI </h1>
<br />
"""
getInput()
print """
<br />
</div>
"""
和 HTML:
<DOCTYPE html>
<html>
<head>
<title> Python CGI Test </title>
</head>
<body>
<h1> Mortage Loan Calculator CGI</h1>
<form action ="/cgi-bin/myPython.py" method ="get">
Rate: <input type = "text" name = "rate"/> <br />
Term: <input type = "text" name = "term"/> <br />
Amount <input type = "text" name = "amount"/> <br />
<button type = "submit" > submit </button>
</form>
</body>
</html>
我想将 python 脚本中的 html 插入到表单下方。
这是服务器端,这意味着一旦加载页面,从 Python 重新加载内容的唯一方法是重新加载或 AJAX。在将内容发送回用户之前,所有代码将 运行。