Django 框架以前

返回主目錄:Django框架

內容目錄:

1、Django框架以前的內容

1、Django框架以前d的內容

1.1 web應用程序的架構
 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服務和數據庫服務能夠對數據進行處理。對外採用標準的通訊協議,以便共享數據。
    
    在服務器上對數據進行處理,就處理的結果生成網頁,以方便客戶端直接下載。
    
    在客戶機上對數據的處理被進一步簡化,將瀏覽器做爲客戶端的應用程序,以實現對數據的顯示。再也不須要爲客戶端單獨編寫和安裝其餘類型的應用程序。
  這樣,在客戶端只須要安裝一套內置瀏覽器的操做系統,直接安裝一套瀏覽器,就能夠實現服務器上數據的訪問。而瀏覽器是計算機的標準設備。
1.2 HTTP協議
http://www.javashuo.com/article/p-siwbohye-bv.html
1.3 純手寫簡單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()
WebServer

 

<!--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>
login.html

 

<!--time.html-->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

@@time@@
</body>
</html>
time.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.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>
user_list_new.html
相關文章
相關標籤/搜索