有思俱乐部学习园地

python教学3


python教学3

1.新建项目

选择python,选择项目的创建路径

添加一个HTML文件和python文件

然后把.js的文件导入到项目(文件需要去群上查找)

这里是所有的文件

2.学习python之BaseHTTPServer模块

htmltest.py代码如下


            from http.server import HTTPServer, BaseHTTPRequestHandler

host = ('0.0.0.0', 14731)  # 绑定自己电脑的地址,14731为端口号


class HTTPRequest(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)  # 设置返回的状态码
        self.send_header('Content-type', 'text/html')  # 指定文本类型,如html
        self.end_headers()  # 结束请求头
        path = self.path
        if path == '/':  # 如果环境等于/
            f = open('index.html', 'r', encoding='utf8')
            self.wfile.write(f.read().encode())
        elif path == '/1':  # 如果环境等于/1
            self.wfile.write('1'.encode())
        # pass


server = HTTPServer(host, HTTPRequest)
server.serve_forever()
            

index.html代码如下

            
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
yyy666
<table border="1">
    <tbody>
    <tr>
        <th>表头1</th>
        <th>表头2</th>
        <th>表头3</th>
    </tr>
    <tr>
        <td>行1内容1</td>
        <td>行1内容2</td>
        <td>行1内容3</td>
    </tr>
    <tr>
        <td>行2内容1</td>
        <td>行2内容2</td>
        <td>行2内容3</td>
    </tr>
    </tbody>
</table>
</body>
</html>

          

右键.py文件,运行

后面进行测试可以点击这个按钮进行刷新

运行成功

3.测试

打开浏览器访问绑定的IP地址和端口

这里是访问127.0.0.1:14731

工作人员

 
作者:周昌
信息录入:周昌