pursue wind pursue wind
首页
Java
Python
数据库
框架
Linux
中间件
前端
计算机基础
DevOps
项目
面试
书
关于
归档
MacOS🤣 (opens new window)
GitHub (opens new window)
首页
Java
Python
数据库
框架
Linux
中间件
前端
计算机基础
DevOps
项目
面试
书
关于
归档
MacOS🤣 (opens new window)
GitHub (opens new window)
  • 基于Python轻松自建App服务器

    • 0App 与后端服务器通信方法简介
    • 1本小册要完成的通信场景功能
    • 2服务器端组件框架的选择与介绍
    • 3基于腾讯云的服务器端环境搭建
    • 4基于 Tornado 的 HTTP 服务器简介及代码组织框架
    • 5第一次数据请求 1:服务器接收用户注册信息
    • 6第一次数据请求 2:为用户处理模块增加 log 管理
    • 7第一次数据请求 3:将用户信息写入 MySQL 数据库
    • 8服务器接收客户端图片上传,并保存在硬盘中
    • 9服务器接收客户端请求,并返回 H5 页面
      • 调用逻辑
      • 服务器端实现
      • H5 页面代码
        • 新增 index.html 文件
        • 新增 index.css 文件
        • 上传 H5 页面图片
        • 客户端请求 H5 页面
      • 代码下载
      • 小结
    • 10搭建基于 Nginx 的代理服务器
    • 11基于 HTTPS 的数据加密
    • 12大型 HTTP 服务器架构演进路线及思路
    • 13总结
  • 基于Python实现微信公众号爬虫

  • Xpath
  • python3 翻译
  • python3循环创建数据库表
  • python实用30个小技巧
  • pywin32
  • Python
  • 基于Python轻松自建App服务器
pursuewind
2020-11-23
目录

9服务器接收客户端请求,并返回 H5 页面

# 服务器接收客户端请求,并返回 H5 页面

在前几节中,我们讲解了客户端与服务器端数据的交互及图片的上传加载,这一节将讲解 H5 页面的请求及加载。

在 App 客户端的设计中,一般的公司都会要求两个端,分别为 Android 和 iOS 端。如果是客户端负责页面的生成,那 Android 端和 iOS 端都将分别做重复的工作,另一个问题是,可能由于开发人员不一样,页面的设计有出入。现在主流的思想会倾向于使用 H5 嵌入客户端当中,H5 文件存放在服务器端,客户端只负责请求并加载。这样就给服务器端提出了一个问题:如何将服务器上的 H5 文件返回给客户端?本小节将解答这个问题。

# 调用逻辑

在第 6 小节的讲解中,用户的信息已注册并写入数据库,这一小节中,我们将模拟该用户登录请求,并在登录成功后,返回 App 首页(即本小节预设的 H5 页面)。下面是本小节涉及的请求流程图。

由于我们需要通过浏览器代替 App 客户端进行用户注册请求模拟,此次客户端请求将使用 GET 方法。请求进入服务器端的 main.py 后,将调用 url_router 转发到 users_url.py 中,在 users_urls.py 中,对应的 URL 将调用 users_views.py 的 LoginHandle 类。LoginHandle 为真正的代码处理逻辑,在校验用户信息正确的情况下,返回 index.html 页面给客户端,客户端加载该页面。

# 服务器端实现

由上面的调用逻辑图可知,我们将从 main.py 开始修改,由于在第 6 小节中, main.py 针对 users 的路由已配置,这里 main.py 不需要修改。接下来修改 users_url.py,在 users_url.py 中增加 LoginHandle 的调用,添加如下两行。

完整代码如下:

#! /usr/bin/python3
# -*- coding:utf-8 -*-


from __future__ import unicode_literals
from .users_views import (
    RegistHandle,
    LoginHandle
)

urls = [
    #从/users/regist过来的请求,将调用users_views里面的RegistHandle类
    (r'regist', RegistHandle),
    (r'login', LoginHandle)
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

接下来,添加真正的代码处理,修改 users_views.py,增加 LoginHandle 类代码如下:

class LoginHandle(tornado.web.RequestHandler):
    """handle /user/regist request
    :param phone: users sign up phone
    :param password: users sign up password
    """
    
    @property
    def db(self):
        return self.application.db
        
    def get(self):
        try:
            #获取入参
            phone = self.get_argument( "phone" )
            password = self.get_argument( "password" )
        except:
            #获取入参失败时,抛出错误码及错误信息
            logger.info("LoginHandle: request argument incorrect")
            http_response(self, ERROR_CODE['1001'], 1001)
            return 
        
        #从数据库 Users 表查找入参中的 phone 是否存在    
        ex_user = self.db.query(Users).filter_by(phone=phone).first()
        if ex_user:
            #如果手机号已存在,返回首页 H5 页面 index.html
            logger.debug( "LoginHandle: get user login: %s" %phone )
            self.render( "index.html" )
            self.db.close()
            return
        else:
            #用户不存在,提示用户未注册
            http_response( self, ERROR_CODE['1003'], 1003 )
            self.db.close()
            return
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

这里的新增错误码 1003 表示用户未注册,需在配置文件中添加此错误码,编辑 base.py 增加如下代码:

"1003": "用户尚未注册,请先注册",
1

至此,服务器端的代码逻辑已基本完成,现在唯一缺少的就是 index.html 这个文件。

# H5 页面代码

由于本小册的重点并不是讲解 H5,这里请读者直接按照指导将 H5 涉及的代码输入服务器端,本小册不做另外的讲解。

# 新增 index.html 文件

进入 “templates” 目录,创建并编辑 index.html 文件,输入如下代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>css网页布局</title>
    <link rel="stylesheet" href="../static/css/index.css">
</head>
<body>
<!--整体到部分,左到右,上到下-->
    <!--头部-->
    <div class="header">
        <div class="logo">
            <ul>
                <li>DEMO</li>
            </ul>
        </div>
        <div class="nav">
            <ul>
                <li>About</li>
            </ul>
        </div>
    </div>
    <!--主体-->
    <div class="main">
        <div class="top">
            <img src="../static/image/index/index.jpg" alt="topimg">
        </div>
        <!--遮罩层-->
        <div class="topplayer"></div>
        <!--最上层的内容-->
        <div class="topplayer-top">
            <div class="word">MY DEMO H5</div>
        </div>
        <div class="middle">
            <div class="m-top">
                <div class="demo-layer demo1">
                    <img src="../static/image/index/1.jpg" alt="DEMO1">
                    <div class="demo">DEMO1</div>
                </div>
                <div class="demo-layer demo1">
                    <img src="../static/image/index/2.jpg" alt="DEMO2">
                    <div class="demo">DEMO2</div>
                </div>
                <div class="demo-layer demo3">
                    <img src="../static/image/index/3.jpg" alt="DEMO3">
                    <div class="demo">DEMO3</div>
                </div>
                <div class="clear"></div>
            </div>
            <div class="m-middle">
                "Life is like riding a bicycle. To keep your balance, you must keep moving."
            </div>
            <div class="m-bottom">
                <div class="m-com">
                    <img src="../static/image/index/4.jpg" alt="4.jpg">
                    <div class="demo4">Cool Demo</div>
                    <div class="demo5">Make it cool</div>
                </div>
                <div class="m-com">
                    <img src="../static/image/index/5.jpg" alt="5.jpg">
                    <div class="demo4">Great Demo</div>
                    <div class="demo5">Make it great</div>
                </div>
                <div class="m-com">
                    <img src="../static/image/index/6.jpg" alt="6.jpg">
                    <div class="demo4">Wonderful Demo</div>
                    <div class="demo5">Make it wonderful</div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

# 新增 index.css 文件

进入 “static/css” 目录,创建并编辑 index.css 文件,输入如下代码:

* {
    margin: 0;
    padding: 0;
}
.header {
    width: 100%;
    height: 100px;
}
.header img {
    width: 300px;
    height: 85px;
    padding-left: 100px;
    padding-top: 8px;
}
.header .logo {
    float: left;
    margin-top: 40px;
    margin-left: 40px;
}
.header .nav {
    float: right;
}
.header .nav ul {
    margin-right: 20px;
}
.header .nav ul li {
    float: left;
    list-style: none;
    width: 80px;
    height: 100px;
    line-height: 100px;
    color: #7d7d7d;
    font-size: 15px;
    font-weight: bolder;
}
.main .top {
    width: 100%;
    height: 600px;
}
.main .top img {
    width: 100%;
    height: 600px;
}
.main .topplayer {
    position: absolute;
    top: 100px;
    background: #000000;
    width: 100%;
    height: 600px;
    opacity: 0.5; /* 透明度 */
}
.main .topplayer-top {
    width: 500px;
    height: 300px;
    position: absolute;
    top: 400px;
    margin-top: -150px;
    z-index: 2;
    right: 50%;
    margin-right: -250px;
}
.main .topplayer-top .word {
    padding-top: 100px;
    color: #ffffff;
    font-size: 45px;
    font-weight: bolder;
    text-align: center;
    font-family: "微软雅黑";
}
.main .topplayer-top button {
    width: 200px;
    height: 60px;
    margin-top: 50px;
    color: #ffffff;
    background: #f5704f;
    font-family: 微软雅黑;
    text-align: center;
    font-weight: bolder;
    font-size: 14px;
    border-radius: 8px; /* 圆角 */
    margin-left: 150px;
}
.main .middle {
    width: 1000px;
    margin: 0 auto;
}
.main .middle .m-top .demo-layer {
    float: left;
    width: 33.3%;
    padding-top: 50px;
    text-align: center;
}
.main .middle .m-top .demo-layer img {
    width: 100px;
    height: 100px;
}
.main .middle .m-top .demo-layer .demo {
    font-size: 20px;
    color: #7d7c7f;
    font-weight: bold;
    padding-top: 20px;
}
.main .middle .m-middle {
    font-size: 25px;
    color: #000000;
    font-weight: bold;
    padding-top: 50px;
    text-align: center;
    padding-bottom: 50px;
}
.clear {
    clear: both;
}
.main .middle .m-bottom .m-com {
    float: left;
    padding: 10px;
    text-align: center;
    font-weight: bold;
    font-size: 20px;
}
.main .middle .m-bottom .m-com img {
    width: 310px;
    height: 260px;
}
.main .middle .m-bottom .demo4 {
    padding-top: 20px;
    color: #7d7d7f;
}
.main .middle .m-bottom .demo5 {
    padding-top: 10px;
    color: #bdbdbc;
}
.main .bottom {
    width: 1000px;
    margin: 0 auto;
}
.footer {
    width: 100%;
    height: 100px;
    text-align: center;
    line-height: 100px;
    background: #292c35;
    color: white;
    font-family: "微软雅黑";
    font-size: 15px;
}
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

# 上传 H5 页面图片

进入 “static/image” 目录,创建 index 文件夹,上传 H5 页面图片。

mkdir index
cd index/
rz -be
1
2
3

具体如下图所示:

至此,H5 的页面准备工作已结束, 服务器端的代码已全部完成。接下来将测试请求是否成功。

# 客户端请求 H5 页面

由于 App 客户端嵌入 H5 页面和手机浏览器直接打开 H5 页面的效果一样,这里在手机的浏览器中直接输入请求 URL 进行测试,URL 为: http://150.109.33.132:8000/users/login?phone=18866668888&password=demo123456。请求成功后,可以看到加载的效果如下:

# 代码下载

到目前为止,服务器端代码及图片如下:
demo10 (opens new window)

# 小结

这一小节中,我们探讨了为什么建议客户端嵌套 H5 页面,并完成了客户端向服务器端请求 H5 页面的整个代码逻辑学习过程,希望读者能触类旁通,提高产品上线效率。

Last Updated: 2023/02/14, 18:02:00
8服务器接收客户端图片上传,并保存在硬盘中
10搭建基于 Nginx 的代理服务器

← 8服务器接收客户端图片上传,并保存在硬盘中 10搭建基于 Nginx 的代理服务器→

Theme by Vdoing | Copyright © 2019-2023 pursue-wind | 粤ICP备2022093130号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
  • 飙升榜
  • 新歌榜
  • 云音乐民谣榜
  • 美国Billboard榜
  • UK排行榜周榜
  • 网络DJ