# -*- 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
相关推荐
老白干2 小时前
jjwt 0.9.1 在 JDK 11+ 上的两个“坑”与完整解决方案笨鸟先飞,勤能补拙9 小时前
AI 赋能网络安全:技术全景、成熟度评估与实战案例长和信泰光伏储能9 小时前
京津冀光伏发电:绿色能源的未来之路浦信仿真大讲堂10 小时前
从重复操作到自动化闭环:如何让 CST 与 Python 真正协同起来Gu Gu Study10 小时前
ScoutLoop开放域深度研究引擎(agent的初步设计想法)卷无止境11 小时前
写代码这件事,到底该讲究点什么?卷无止境11 小时前
循环复杂度到底在算什么,Python 代码怎么才能写得让人一看就懂lpfasd12311 小时前
MediaCrawler 项目深度分析Dxy123931021612 小时前
Python项目打包成EXE完整教程(PyInstaller实战避坑)bamb0012 小时前
一个项目带你入门AI应用开发01