使用python本地服务器将数据显示为html文件

使用python本地服务器将数据显示为html文件,python,html,localserver,Python,Html,Localserver,我有一个简单的python本地服务器,其中包含2个变量,这些变量的数据我想访问到index.html文件中 我的代码 这里是ulta_palta_列表和name变量,我想在像django框架这样的索引文件下访问它们 index.html 插座更适合这个? 谢谢,因此您可以将index.html的内容作为名为file_to_open的变量中的字符串 顺便说一句,我建议重命名它。然后你可以在index.html中使用占位符。 前 谢谢@Justin Ezequiel。对于使用诸如for循环之类的条件

我有一个简单的python本地服务器,其中包含2个变量,这些变量的数据我想访问到index.html文件中

我的代码 这里是ulta_palta_列表和name变量,我想在像django框架这样的索引文件下访问它们

index.html 插座更适合这个?
谢谢

,因此您可以将index.html的内容作为名为file_to_open的变量中的字符串 顺便说一句,我建议重命名它。然后你可以在index.html中使用占位符。 前


谢谢@Justin Ezequiel。对于使用诸如for循环之类的条件,我必须编写哪些语法?
from http.server import HTTPServer, BaseHTTPRequestHandler

ulta_palta_list = ['Pagla', 'Janina', 'Bangladesh']
name = "someone"

class DataHeadServer(BaseHTTPRequestHandler):

    def do_GET(self):
        if self.path == '/':
            self.path = '/index.html'
        try:
            file_to_open = open(self.path[1:]).read()
            self.send_response(200)
        except:
            file_to_open = "File Not Found"
            self.send_response(404)
        self.end_headers()
        self.wfile.write(bytes(file_to_open, 'utf-8'))


httpd = HTTPServer(('localhost', 8080), DataHeadServer)
httpd.serve_forever()
print("Server Running")
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home</title>
</head>
<body>
<h1>Hello, Python</h1>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home</title>
</head>
<body>
<h1>Hello, Python</h1>
<p>the list = {ulta_palta_list}</p>
<p>the name = {name}</p>
</body>
</html>
file_to_open = file_to_open.format(ulta_palta_list=ulta_palta_list, name=name)