zoukankan      html  css  js  c++  java
  • 08 返回动态页面web框架

    08 返回动态页面web框架 

    动态页面:

                 网页的内容是动态变化的,不是一直不变的(静态页面:每次显示的内容都是一样) 

    服务器serverpython程序(动态页面版本):

     1 import socket
     2 
     3 import time
     4 
     5 server=socket.socket()
     6 
     7 server.bind(("127.0.0.1",8888))
     8 
     9 server.listen()
    10 
    11 # 返回页面显示每次访问时间,这是一个动态的,主要在返回内容是替换某些特定内容
    12 
    13 def func_html(conn):
    14 
    15     with open("index.html","rb")as f:
    16 
    17         data=f.read().decode("utf-8")
    18 
    19     # 将网页中的#time#时间占位字符串进行替换后再返回(每次请求响应返回的结果都不一样)
    20 
    21     t=time.strftime("%Y-%m-%d %X")
    22 
    23     data=data.replace("#time#",str(t))
    24 
    25     conn.send(data.encode("utf-8"))
    26 
    27  
    28 
    29 def func_ico(conn):
    30 
    31     with open("favicon.ico","rb")as f:
    32 
    33         conn.send(f.read())
    34 
    35  
    36 
    37 def response_back(conn,path,func_mappers):
    38 
    39     conn.send(b"HTTP/1.1 200 ok 
    
    ")
    40 
    41     for mapper in func_mappers:
    42 
    43         if path==mapper[0]:
    44 
    45             mapper[1](conn)
    46 
    47     conn.close()
    48 
    49  
    50 
    51 func_mappers=[
    52 
    53     ("/",func_html),
    54 
    55     ("/favicon.ico",func_ico)
    56 
    57 ]
    58 
    59   
    60 
    61 if __name__ == '__main__':
    62 
    63     while 1:
    64 
    65         conn,client_addr=server.accept()
    66 
    67         http_request=conn.recv(1024).decode("utf-8")
    68 
    69         path=http_request.split("
    ")[0].split(" ")[1]
    70 
    71         print("parh>>>",path)
    72 
    73        
    74 
    75         response_back(conn,path,func_mappers)
    服务器server端python程序(动态页面版本)

    客户端浏览器访问:

                 在浏览器地址栏直接输入地址端口:127.0.0.18888

    index.html


     1 <!DOCTYPE html>
     2 
     3 <html lang="en">
     4 
     5 <head>
     6 
     7     <meta charset="UTF-8">
     8 
     9     <link rel="icon" href="favicon.ico">
    10 
    11     <style>
    12 
    13         span{
    14 
    15             background-color: yellow;
    16 
    17             color: red;
    18 
    19             font-size: 20px
    20 
    21         }
    22 
    23     </style>
    24 
    25     <title>动态页面</title>
    26 
    27 </head>
    28 
    29 <body>
    30 
    31     <div>
    32 
    33         <h1>本次访问时间为:
    34 
    35             <span>#time#</span>
    36 
    37         </h1>
    38 
    39     </div>
    40 
    41 </body>
    42 
    43 </html>
    index.html

        

  • 相关阅读:
    python3 接口测试 一般方法
    python2 接口测试一般方法.
    Postman 官网教程,重点内容,翻译笔记,
    Unity3D --对撞机/碰撞器 介绍
    MyBatis之配置文件与操作
    Eclipse连接mysql数据库jdbc下载(图文)
    MyBatis概述和环境的搭建
    样式加载不出来,浏览器控制台报错:Resource interpreted as Stylesheet but transferred with MIME type text/html
    bootstrap之navbar
    JSP中的内置标记(JSP动作)和 EL表达式
  • 原文地址:https://www.cnblogs.com/open-yang/p/11182811.html
Copyright © 2011-2022 走看看