1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | # coding:utf-8 from wsgiref.simple_server import make_server #导入simple_server模块 #视图函数 def home( * args): return "home" def login( * args): return "login" def project( * args): return "project" patterns = { "/" :home, "/login" :login, "/project" :project} #路由关系的字典 #定义一个application,遵循wsgi协议; def app(env, start_response): #服务器接收到的客户端请求都会存储在env中,再传入到app进行处理,处理后再返回 url = env.get( "PATH_INFO" ) #从环境变量获取web端传来的url params = env.get( "QUERY_STRING" ) #从环境变量获取web端传入的参数 if (url is None ) or (url not in patterns.keys()): start_response( "404 not found" , [( "Content-Type" , "text/plain" )]) return [b "page not found" ] start_response( "200 ok" ,[( "Content-Type" , "text/plain" )]) resp = patterns.get(url) if resp is None : #key在路由关系里,但是velue为none的情况处理 start_response( "404 not found" , [( "Content-Type" , "text/plain" )]) return [b "page not found" ] return [resp(params).encode()] #执行视图函数 #实例化一个服务器设置ip为本机,端口为888,执行程序为上面的app server = make_server("", 5000 , app) #开启一个服务器,默认0.5秒轮询,接收客户端请求 server.serve_forever() |
启动:python simple_server.py
分类:
python
标签:
python web
, python 内置web
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2020-12-01 linux普通用户权限设置为超级用户权限
2020-12-01 linux-免密操作
2020-12-01 shell-用户权限操作