我的第一个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()
相关推荐
soumns丶涛1 小时前
“conda”不是内部或外部命令,也不是可运行的程序或批处理文件
windows·python·jupyter·conda
一休哥助手1 小时前
使用 LROPoller 处理 Azure 文档分析时的常见问题及解决方案
后端·python·flask
noravinsc1 小时前
django models 多条件检索
后端·python·django
初尘屿风1 小时前
基于Python的Flask微博话题舆情分析可视化系统设计与实现+毕业论文+指导搭建视频
开发语言·python·flask
编织幻境的妖2 小时前
python的Tkinter小程序上传Excel并下载Text
python·小程序·excel
江梦寻2 小时前
在 M1 Mac 上解锁 TensorFlow GPU 加速:从环境搭建到实战验证
人工智能·pytorch·python·深度学习·macos·tensorflow·gpu算力
落落落sss2 小时前
spring-data-mongoDB
java·服务器·数据库·后端·python·mongodb·spring
MindMaster User2 小时前
centos 7 安装python3 及pycharm远程连接方法
python·pycharm·centos
瓦力的狗腿子3 小时前
Starlink卫星动力学系统仿真建模第十讲-基于SMC和四元数的卫星姿态控制示例及Python实现
开发语言·python·算法
艾醒(AiXing-w)3 小时前
Linux系统管理(十七)——配置英伟达驱动、Cuda、cudnn、Conda、Pytorch、Pycharm等Python深度学习环境
linux·python·深度学习