我的第一个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()
相关推荐
唐叔在学习8 分钟前
Python移动端应用消息提醒开发实践
开发语言·python
好家伙VCC8 分钟前
**发散创新:基于Python与OpenCV的视频流帧级分析实战**在当前人工智能与计算机视觉飞速发展的背景下
java·人工智能·python·计算机视觉
xiaotao1319 分钟前
阶段零:IDE选择 与 Jupyter Notebook / Lab 使用
ide·人工智能·python·jupyter
Pocker_Spades_A15 分钟前
Python快速入门专业版(五十七)——POST请求与模拟登录:从表单分析到实战(以测试网站为例)
开发语言·python
Highcharts.js19 分钟前
企业级数据可视化|BI 仪表板数据中台工业监控平台的选择分析
人工智能·python·信息可视化·数据挖掘·数据分析·highcharts
橙露24 分钟前
Seaborn 高级可视化:统计图表制作与报告导出
python
2601_9544345525 分钟前
2026年专业深度测评:入门电钢琴品牌排名前五权威发布
大数据·人工智能·python
道清茗29 分钟前
【RH294知识点汇总】第 3 章 《 管理变量和事实 》1
开发语言·python
shy^-^cky32 分钟前
Python OpenCV 边缘检测效果对比
python·opencv·计算机视觉·边缘检测·sobel·canny·roberts
老歌老听老掉牙37 分钟前
从芯厚误差到几何约束:一个核心误差函数的深度重构与解析
python