无法理解为什么代码没有给出预期的结果?

问题描述 投票:0回答:0

我正在尝试根据 Head First Python 书制作一个简单的网络应用程序。但是嵌入在index.html文件中的python cgi脚本不是在点击超链接后生成html页面,而是返回python cgi脚本的代码。我曾尝试寻找发生这种情况的原因,但没有运气。如果有人能告诉我为什么会出现这个问题,那真的很有帮助吗? 这是 index.html:

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Welcome to Coach Kelly's Website</title>
    <link type="text/css" rel="stylesheet" href="style/coach.css" />
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
    <img src="images/coach-head.jpg">
    <h1>Welcome to Coach Kelly's Website.</h1>
    <p>
        For now, all that you'll find here is my athlete's <a href="cgi-bin/generate_list.py">timing data</a>. Enjoy!
    </p>
    <p>
        <strong>See you on the track!</strong>
    </p>
</body>

</html>
这是cgi脚本:

import athletemodel
import yate
import glob

data_files = glob.glob("data/*.txt")
athletes = athletemodel.put_to_store(data_files)

print(yate.start_response())
print(yate.include_header("Coach Kelly's List of Athletes"))
print(yate.start_form("generate_timing_data.py"))
print(yate.para("Select an athlete from the list to work with:"))

for each_athlete in athletes:
    print(yate.radio_button("which_athlete", athletes[each_athlete].name))

print(yate.end_form("Select"))

print(yate.include_footer({"Home": "/index.html"}))

这是 yate.py :

from string import Template

def start_response(resp="text/html"):
    return('Content-type: ' + resp + '\n\n')

def include_header(the_title):
    with open('templates/header.html') as headf:
        head_text = headf.read()
    header = Template(head_text)
    return(header.substitute(title=the_title))

def include_footer(the_links):
    with open('templates/footer.html') as footf:
        foot_text = footf.read()
    link_string = ''
    for key in the_links:
        link_string += '<a href="' + the_links[key] + '">' + key + '</a>&nbsp;&nbsp;&nbsp;&nbsp;'
    footer = Template(foot_text)
    return(footer.substitute(links=link_string))

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

def end_form(submit_msg="Submit"):
    return('<p></p><input type=submit value="' + submit_msg + '"></form>')

def radio_button(rb_name, rb_value):
    return('<input type="radio" name="' + rb_name +
                             '" value="' + rb_value + '"> ' + rb_value + '<br />')

def u_list(items):
    u_string = '<ul>'
    for item in items:
        u_string += '<li>' + item + '</li>'
    u_string += '</ul>'
    return(u_string)

def header(header_text, header_level=2):
    return('<h' + str(header_level) + '>' + header_text +
           '</h' + str(header_level) + '>')

def para(para_text):
    return('<p>' + para_text + '</p>') 

她是创建网络服务器的代码:

from http.server import HTTPServer, CGIHTTPRequestHandler

port = 8080

httpd = HTTPServer(('', port), CGIHTTPRequestHandler)
print("Starting simple_httpd on port: " + str(httpd.server_port))
httpd.serve_forever()
单击超链接后,我附上了实际输出的图像与我得到的图像。 主页:

点击计时数据后,我应该得到这个:

我得到的输出:

python apache web-applications cgi server-side
© www.soinside.com 2019 - 2024. All rights reserved.