新建文件server.py, 代码
import http.server
import socketserver
# 设置服务器的地址和端口
host = "localhost"
port = 8000
# 创建请求处理器
handler = http.server.SimpleHTTPRequestHandler
# 创建服务器实例
with socketserver.TCPServer((host, port), handler) as server:
print(f"Server started at {host}:{port}")
# 启动服务器,一直运行直到手动停止
server.serve_forever()
cmd运行python server.py后,浏览器输入 http://localhost:8000 就可访问
博客介绍了使用Python创建服务器的简单操作。新建server.py文件,在命令行运行python server.py,之后在浏览器输入http://localhost:8000即可实现本地访问。

3万+

被折叠的 条评论
为什么被折叠?



