# -*- 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
相关推荐
IT空门:门主5 小时前
Spring AI 2.1.0 更新了什么:从 2.0 升上来的完整梳理AI创界者6 小时前
Python 进阶实战(七十章):遗留系统重构、渐进式绞杀与技术债治理实战ZhengEnCi7 小时前
电脑操作 Agent 一直点不中:截图没更新时,提示词该怎么改才能停止瞎试Wellijohn9 小时前
dict、tuple、dataclass 还是 slots?我把 6 种数据载体的 8 项开销全测了一遍bufengwangluo9 小时前
爬虫和调用 API 有什么区别?为什么优先选 APIDmz丶9 小时前
前端转 AI 100 天 Day 54:RAG 项目实战(三)——多轮对话 + 会话管理console.log('npc')10 小时前
Python第9课 类学习笔记,前端js语法对照geovindu11 小时前
python:Logistic Regressiongeovindu13 小时前
python: Random Forest