# -*- 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 web 网站
温正实2024-03-06 8:54
相关推荐
hhzz7 小时前
Python大数据实战(十六):音乐推荐系统——基于协同过滤算法构建个性化歌单引擎SunnyDays10118 小时前
Python PDF 转 Markdown 详解(转换整个文档,特定页面,表格,扫描 PDF)2601_954706498 小时前
云手机 API 自动化实战:Python 批量控机、挂机脚本完整实现Alan_69112 小时前
聊聊 Swagger/Postman/Apifox 的真实选型与 Python 项目实战智慧物业老杨13 小时前
物业绿化数智化权责管控方案:基于工单平台的双层权限追溯模块落地实践Metaphor69213 小时前
使用 Python 在 Word 文档中添加批注FreakStudio14 小时前
WIZnet 开源方案分享:W55MH32 开发板 + 小智 AI实现以太网版本的小智机器人会周易的程序员14 小时前
使用 pybind11 封装 C++ 日志库 microLog 为 Python 模块一只专注api接口开发的技术猿14 小时前
电商评论自动化监控与情感数据分析完整落地教程(附可直接运行 Python 代码)HAPPY酷14 小时前
【ROS2】 Conda 环境搭建(命令行版)