代码拉取完成,页面将自动刷新
from http.server import HTTPServer, SimpleHTTPRequestHandler
import socket
import sys
import webbrowser
import os
class CustomHandler(SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate')
self.send_header('Access-Control-Allow-Origin', '*')
SimpleHTTPRequestHandler.end_headers(self)
def run(port=8000):
try:
server_address = ('', port)
httpd = HTTPServer(server_address, CustomHandler)
print(f'Starting server on port {port}...')
print(f'Visit http://localhost:{port}')
# 自动打开浏览器
webbrowser.open(f'http://localhost:{port}')
httpd.serve_forever()
except socket.error as e:
if e.errno == 10013:
print(f"Error: Port {port} requires elevated privileges.")
print("Try using a port number above 1024")
sys.exit(1)
elif e.errno == 10048:
print(f"Error: Port {port} is already in use.")
print("Try a different port number")
sys.exit(1)
else:
raise e
except KeyboardInterrupt:
print("\nShutting down server...")
httpd.server_close()
sys.exit(0)
if __name__ == '__main__':
run(8000)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。