一 Web應用程序是什么
Web應用程序是一種可以通過Web訪問的應用程序,程序的最大好處是用戶很容易訪問應用程序,用戶只需要有瀏覽器即可,不需要再安裝其他軟件
應用程序有兩種模式C/S、B/S。C/S是客戶端/服務器端程序,也就是說這類程序一般獨立運行。而B/S就是瀏覽器端/服務器端應用程序,這類應用程序一般借助IE等瀏覽器來運行。WEB應用程序一般是B/S模式。Web應用程序首先是“應用程序”,和用標准的程序語言,如C、C++等編寫出來的程序沒有什么本質上的不同。然而Web應用程序又有自己獨特的地方,就是它是基於Web的,而不是采用傳統方法運行的。換句話說,它是典型的瀏覽器/服務器架構的產物。
Web應用程序的優點
-
網絡應用程序不需要任何復雜的“展開”過程,你所需要的只是一個適用的瀏覽器;
-
網絡應用程序通常耗費很少的用戶硬盤空間,或者一點都不耗費;
-
它們不需要更新,因為所有新的特性都在服務器上執行,從而自動傳達到用戶端;
-
網絡應用程序和服務器端的網絡產品都很容易結合,如email功能和搜索功能;
-
因為它們在網絡瀏覽器窗口中運行,所以大多數情況下它們是通過跨平台使用的 (例如Windows,Mac,Linux等等)
Web應用程序的缺點
- 網絡應用程序強調瀏覽器的適用性。如果瀏覽器方沒有提供特定的功能,或者棄用特定的平台或操作系統版本(導致不適用),就會影響大量用戶;
- 網絡應用依靠互聯網遠程服務器端的應用文件。因此,當連接出問題時,應用將不能正常使用。
- 許多網絡應用程序不是開源的,只能依賴第三方提供的服務,因此不能針對用戶定制化、個性化,而且大多數情況下用戶不能離線使用,因而損失了很多靈活性;
- 它們完全依賴應用服務商的可及性。如果公司倒閉,服務器停止使用,用戶也無法追索以前的資料。對比而看,即使軟件制造商倒閉了,傳統的安裝軟件也可以繼續運行,盡管不能再更新或有其他用戶服務;
- 相似地,提供方公司對軟件和其功能有了更大的控制權。只要他們願意就能為軟件添加新特性,即使用戶想等bugs先被解決再更新。跳過較差的軟件版本也不可能了。公司可以強加不受歡迎的特性給用戶,也可以隨意減少帶寬來削減開支。
- 公司理論上可以檢索任何的用戶行為。這有可能引起隱私安全問題。
B/S架構優點
瀏覽器/服務器架構(Browser/Server,簡稱B/S)能夠很好地應用在廣域網上,成為越來越多的企業的選擇。瀏覽器/服務器架構相對於其他幾種應用程序體系結構,有如下3方面的優點:
- 這種架構采用Internet上標准的通信協議(通常是TCP/IP協議)作為客戶機同服務器通信的協議。這樣可以使位於Internet任意位置的人都能夠正常訪問服務器。對於服務器來說,通過相應的Web服務和數據庫服務可以對數據進行處理。對外采用標准的通信協議,以便共享數據。
- 在服務器上對數據進行處理,就處理的結果生成網頁,以方便客戶端直接下載。
- 在客戶機上對數據的處理被進一步簡化,將瀏覽器作為客戶端的應用程序,以實現對數據的顯示。不再需要為客戶端單獨編寫和安裝其他類型的應用程序。這樣,在客戶端只需要安裝一套內置瀏覽器的操作系統,直接安裝一套瀏覽器,就可以實現服務器上數據的訪問。而瀏覽器是計算機的標准設備
總結一下,本質上:瀏覽器是一個socket客戶端,服務器是一個socket服務端
二 基於SOCKET寫一個web應用
py文件
import socket def server_run(): soc = socket.socket() soc.bind(('127.0.0.1', 8008)) soc.listen(5) while True: conn, addr = soc.accept() recv_data = conn.recv(1024) print(recv_data) # 1 直接在send里寫,發送給客戶端 # conn.send(b'HTTP/1.1 200 OK\r\n\r\n<h1>hello web</h1><img src="https://gss2.bdstatic.com/9fo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike92%2C5%2C5%2C92%2C30/sign=5e3814acf9edab64607f4592965fc4a6/14ce36d3d539b600c0c465d0eb50352ac65cb74b.jpg"></img>') #2 打開一個html文件,發送給客戶端 # with open('index.html','r',encoding='utf-8') as f: # data=f.read() # conn.send(('HTTP/1.1 200 OK\r\n\r\n%s'%data).encode('utf-8')) # 3 動態網頁,字符串替換 import time now=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) print(now) with open('index.html','r',encoding='utf-8') as f: data=f.read() data=data.replace('@@@',now) conn.send(('HTTP/1.1 200 OK\r\n\r\n%s'%data).encode('utf-8')) conn.close() if __name__ == '__main__': server_run()
index.html 文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h2>@@@</h2> <img src="https://gss2.bdstatic.com/9fo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike92%2C5%2C5%2C92%2C30/sign=5e3814acf9edab64607f4592965fc4a6/14ce36d3d539b600c0c465d0eb50352ac65cb74b.jpg" alt=""> </body> </html>
三 手擼簡單web框架

''' a socket服務端 b 根據url不同返回不同的內容 url---視圖函數 c 字符串返回給用戶 特殊字符替換 Web框架種類: a b c Tornado 別人的a b c django(a用的wsgiref) 別人a b 別人c flask(c用的jinja2) 另一種分類: Django和其它 ''' 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()

<!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>

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> @@time@@ </body> </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>

<!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>