Web应用程序是一种能够经过Web访问的应用程序,程序的最大好处是用户很容易访问应用程序,
用户只须要有浏览器便可,不须要再安装其余软件。
应用程序有两种模式C/S、B/S。C/S是客户端/服务器端程序,也就是说这类程序通常独立运行。
而B/S就是浏览器端/服务器端应用程序,这类应用程序通常借助浏览器来运行。
WEB应用程序通常是B/S模式。Web应用程序首先是“应用程序”,和用标准的程序语言,如C、C++等编写出来的程序没有什么本质上的不一样。
(1) Web应用程序的优势
网络应用程序不须要任何复杂的“展开”过程,只须要一个适用的浏览器;
网络应用程序一般耗费不多的用户硬盘空间,或者一点都不耗费;
它们不须要更新,由于全部新的特性都在服务器上执行,从而自动传达到用户端;
网络应用程序和服务器端的网络产品都很容易结合,如email功能和搜索功能;
由于它们在网络浏览器窗口中运行,因此大多数状况下它们是经过跨平台使用的 (例如Windows,Mac,Linux等等)
(2) Web应用程序的缺点
网络应用程序强调浏览器的适用性。若是浏览器方没有提供特定的功能,或者弃用特定的平台或操做系统版本(致使不适用),就会影响大量用户;
网络应用依靠互联网远程服务器端的应用文件。所以,当链接出问题时,应用将不能正常使用。
许多网络应用程序不是开源的,只能依赖第三方提供的服务,所以不能针对用户定制化、个性化,并且大多数状况下用户不能离线使用,于是损失了不少灵活性;
它们彻底依赖应用服务商的可及性。若是公司倒闭,服务器中止使用,用户也没法追索之前的资料。对比而看,即便软件制造商倒闭了,传统的安装软件也能够继续运行,尽管不能再更新或有其余用户服务;
类似地,提供方公司对软件和其功能有了更大的控制权。只要他们愿意就能为软件添加新特性,即便用户想等bugs先被解决再更新。跳过较差的软件版本也不可能了。公司能够强加不受欢迎的特性给用户,也能够随意减小带宽来削减开支。
公司理论上能够检索任何的用户行为。这有可能引发隐私安全问题。
(3) B/S架构优势
浏览器/服务器架构(Browser/Server,简称B/S)可以很好地应用在广域网上,成为愈来愈多的企业的选择。浏览器/服务器架构相对于其余几种应用程序体系结构,有以下3方面的优势:
这种架构采用Internet上标准的通讯协议(一般是TCP/IP协议)做为客户机同服务器通讯的协议。这样可使位于Internet任意位置的人都可以正常访问服务器。对于服务器来讲,
经过相应的Web服务和数据库服务能够对数据进行处理。对外采用标准的通讯协议,以便共享数据。
在服务器上对数据进行处理,就处理的结果生成网页,以方便客户端直接下载。
在客户机上对数据的处理被进一步简化,将浏览器做为客户端的应用程序,以实现对数据的显示。再也不须要为客户端单独编写和安装其余类型的应用程序。
这样,在客户端只须要安装一套内置浏览器的操做系统,直接安装一套浏览器,就能够实现服务器上数据的访问。而浏览器是计算机的标准设备。
<!--webServer--> ''' import socket import pymysql def index(request): return '<img src="https://gss2.bdstatic.com/9fo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike92%2C5%2C5%2C92%2C30/sign=5e3814acf9edab64607f4592965fc4a6/14ce36d3d539b600c0c465d0eb50352ac65cb74b.jpg"></img>' def login(request): with open('login.html','r',encoding='utf-8') as f : data=f.read() return data def time(request): import datetime now=datetime.datetime.now().strftime('%Y-%m-%d %X') with open('time.html','r',encoding='utf-8') as f : data=f.read() data=data.replace('@@time@@',now) return data def user_list(request): # 建立链接 conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='lqz') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) cursor.execute("select id,name,password from user") user_list = cursor.fetchall() cursor.close() conn.close() tr_list=[] for row in user_list: tr='<tr><td>%s</td><td>%s</td><td>%s</td></tr>'%(row['id'],row['name'],row['password']) tr_list.append(tr) with open('user_list.html','r',encoding='utf-8') as f: data=f.read() data=data.replace('@@body@@',''.join(tr_list)) return data def user_list_new(request): # 建立链接 conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='lqz') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) cursor.execute("select id,name,password from user") user_list = cursor.fetchall() cursor.close() conn.close() with open('user_list_new.html','r',encoding='utf-8') as f: data=f.read() from jinja2 import Template template=Template(data) response=template.render(user_list=user_list) # response=template.render({'user_list':user_list}) return response urls = [ ('/index', index), ('/login', login), ('/time', time), ('/user_list', user_list), ('/user_list_new', user_list_new), ] def run(): soc = socket.socket() soc.bind(('127.0.0.1', 8006)) soc.listen(5) while True: conn, port = soc.accept() data = conn.recv(1024) # data=data.decode('utf-8') print(data) data = str(data, encoding='utf-8') request_list = data.split('\r\n\r\n') head_list = request_list[0].split('\r\n') method, url, htt = head_list[0].split(' ') # conn.send(b'hello web') conn.send(b'HTTP/1.1 200 OK \r\n\r\n') print(url) func_name = None for u in urls: if url == u[0]: func_name = u[1] break if func_name: response = func_name(data) else: response = '404 not found' conn.send(response.encode('utf-8')) conn.close() if __name__ == '__main__': run()
<!--login.html--> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action=""> <p>用户名:<input type="text"></p> <p>密码:<input type="password"></p> </form> </body> </html>
<!--time.html--> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> @@time@@ </body> </html>
<!--user_list.html--> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用户列表</title> </head> <body> <table border="1"> <thead> <tr> <th>id</th> <th>用户名</th> <th>密码</th> </tr> </thead> <tbody> @@body@@ </tbody> </table> </body> </html>
<!--user_list_new--> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用户列表</title> </head> <body> <table border="1"> <thead> <tr> <th>id</th> <th>name</th> <th>password</th> </tr> </thead> <tbody> {% for user in user_list%} <tr> <td>{{user.id}}</td> <td>{{user.name}}</td> <td>{{user.password}}</td> </tr> {%endfor%} </tbody> </table> </body> </html>