# -*- 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
相关推荐
武天旭5 小时前
AI安全--AI大模型的毒性偏见与安全对齐实践视觉小萌新6 小时前
AI Class 3 · Chapter 2-3:函数、异常、文件读写+JSON / JSONL 读写(小白版)小小张说故事7 小时前
Python 的 obj.x 到底按什么顺序找?拆开数据描述符、实例字典和 __getattr__ 那条优先级链用户11481867894848 小时前
水印的实现方式逻辑贾公子8 小时前
从单条内容到账号维度——AIGC 风险评分的工程实现木头左9 小时前
【Python】不点浏览器发博客:逆向 CSDN 创作中心接口做 md-publish 技能Tt有了t10 小时前
设计模式速查deepseek2310 小时前
Python日志拖慢请求?QueueHandler排队、丢失与退出实测用户912940468484210 小时前
Codex 从零到能交付(05):第一次改代码:生成一个文件清单脚本Dmz丶10 小时前
前端转 AI 100 天 Day 38:Token 管理与成本优化——别让 API 账单爆炸