当前位置: 移动技术网 > IT编程>脚本编程>Python > python 练习 后台返回当前时间

python 练习 后台返回当前时间

2019年03月27日  | 移动技术网IT编程  | 我要评论

香港男女蒲典,月如隐於暗夜中,三观是哪三观

新建一个 current_time.html 文件, !cur_time! 用来替换

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
</head>
<body>
<h1>current_time: !cur_time!</h1>
</body>
</html>

新建一个 server.py 文件

from wsgiref.simple_server import make_server
import time

def current_time(request):
    cur_time = time.ctime(time.time())
    f=open("current_time.html","rb")
    data=f.read()

    # 替换当前的时间
    data=str(data,"utf8").replace("!cur_time!",str(cur_time))

    return [data.encode("utf8")]

def routers():
    urlpatterns = (
        ('/cur_time', current_time),
    )
    return  urlpatterns

def application(environ, start_response):
    start_response('200 ok', [('content-type', 'text/html')])

    urlpatterns = routers()
    path = environ["path_info"]
    func = none
    for item in urlpatterns:
        if item[0] == path:
            func = item[1]
            break
    if func:
        return func(environ)
    else:
        return ["<h1>404</h1>".encode("utf8")]



httpd = make_server('', 8888, application)

print('serving http on port 8888...')
httpd.serve_forever()

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网