# -*- 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
相关推荐
默_笙3 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的qq_426003963 天前
启动playwright录制codegen生成自动化测试脚本虎头金猫3 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺长沙三为智能科技3 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单伞伞悦读3 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__只睡四小时3 天前
Canvas 弹道联机实战:700 行 + 固定时间步长奇思妙想聪明勤奋的小羊3 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派lpfasd1233 天前
2026年第38周GitHub趋势周报IZero073 天前
Jev 与 Laya