我的第一个python web 网站

复制代码
# -*- coding: utf-8 -*-

import http.server
import socketserver
from datetime import datetime

PORT = 8000

import sys

# ...

class MyHandler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/':
            # 如果路径是根路径,返回页面内容
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()

            try:
                now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                response_content = "我的第一个Python网页<br>当前时间:{}".format(now)
                self.wfile.write(response_content.encode())
                self.send_header('Content-type', 'text/html; charset=utf-8')

                # 添加打印语句
                print(f"GET request received. Path: {self.path}, Time: {now}")
                sys.stdout.flush()  # 刷新输出缓冲区
            except Exception as e:
                # 捕获并打印异常
                print(f"Error processing GET request: {e}")
                sys.stdout.flush()  # 刷新输出缓冲区
        else:
            # 否则按照默认的 SimpleHTTPRequestHandler 处理
            super().do_GET()


with socketserver.TCPServer(("0.0.0.0", PORT), MyHandler) as httpd:
    print("Serving at port", PORT)
    sys.stdout.flush()  # 刷新输出缓冲区
    httpd.serve_forever()
相关推荐
Python开发吖10 分钟前
【已解决】python的kafka-python包连接kafka报认证失败
开发语言·python·kafka
aini_lovee3 小时前
python在容器内克隆拉取git私有仓库
git·python·elasticsearch
安全系统学习5 小时前
网络安全之RCE简单分析
开发语言·python·算法·安全·web安全
蹦蹦跳跳真可爱5896 小时前
Python----神经网络发(神经网络发展历程)
人工智能·python·深度学习·神经网络·计算机视觉
chao_7897 小时前
标注工具核心代码解析——class AnnotationVie【canvas.py]
开发语言·python·qt5
struggle20258 小时前
tvm开源程序是适用于 CPU、GPU 和专用加速器的开放式深度学习编译器堆栈
人工智能·python·深度学习
仟濹8 小时前
「pandas 与 numpy」数据分析与处理全流程【数据分析全栈攻略:爬虫+处理+可视化+报告】
大数据·python·数据分析·numpy·pandas
测试杂货铺8 小时前
postman接口测试
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
四川兔兔8 小时前
pytorch 之 nn 库与调试
人工智能·pytorch·python
終不似少年遊*9 小时前
机器学习方法实现数独矩阵识别器
人工智能·python·opencv·机器学习·计算机视觉·矩阵