Before Django framework

Of Contents:

A content before, Django framework
Two, Django framework

A, d before the frame content Django

1.1 web application framework
Web applications-applications that can be accessed through the Web, the biggest benefit of the program is that users can easily access the application, 
    users only need to have a browser, do not need to install additional software. 
    
    Application has two modes C / S, B / S. C / S is a client / server applications, which means that such procedures are generally run independently. 
    And B / S is the browser client / server applications, such applications typically run by the browser. 
    WEB application generally B / S model. Web application first is the "application", and using standard programming languages, such as writing out of the C, C ++ and other procedures did not differ essentially what.

 

(1) the advantages of the Web application 
    web application does not require any complex "expand" process that requires only a suitable browser; 
    
    web applications typically spend a few users of hard disk space, or that is not consumed; 
    
    they do not need update, because all the new features are executed on the server, which automatically communicated to the client; 
    
    network applications and server networking products are easy to combine, such as email functions and search functions; 
    
    because they run in a web browser window, so in most cases they are cross-platform use (such as Windows, Mac, Linux, etc.)

 

(2) the shortcomings of the Web application 
    Web applications emphasize the applicability of the browser. If the browser provides no specific function, or abandon a particular platform or operating system version (lead NA), it will affect a large number of users; 
    
    network applications rely on Internet remote application file server. Thus, when the connection problem, the application may not function properly. 
    
    Many web applications are not open source, and can only rely on services provided by third parties and therefore can not be customized for the user, personalized, and in most cases users can not be used offline, and therefore lost a lot of flexibility; 
   
    they are entirely dependent on application service providers of accessibility. If the company goes bankrupt, stop using the server, the user can not be traced previous data. Compare and see, even if the software manufacturer went out of business, traditional software installation can continue to run, although no longer updated or other user services; 
    
    similarly, the provider of the software company and its functions have greater control. If they would be able to add new features to the software, even if the user want to wait for bugs to be solved before updating. Skip poor software version is also impossible. The company can impose undesirable characteristics to the user, but also free to reduce the bandwidth to cut spending. 
    
    The company is theoretically possible to retrieve any user behavior. This may cause privacy issues.

 

(3) B / S architecture advantages of 
    browser / server architecture (Browser / Server, referred to as B / S) can be well applications over the WAN, become more and more enterprises. Browser / Server architecture with respect to several other application architecture, the following advantageous aspects 3: 
    
    This architecture using the Internet standard communication protocol (usually TCP / IP protocol) protocol as a client to communicate with the server. This allows people anywhere in the Internet can access the server properly. For servers, 
  it can process the data through the corresponding Web service and database services. External standard communication protocol, to share data. 
    
    The results of the data is processed on the server, it is processed to generate web pages to facilitate client direct download. 
    
    Processing of the data is further simplified on the client, a browser client application to effect display of data. No longer need to write separate client and install other types of applications. 
  In this way, the client only needs to install an operating system built-in browser, direct the installation of a browser, you can enable access to data on the server. The browser is a standard device of the computer.
1.2 HTTP protocol
https://www.cnblogs.com/xt12321/p/10980551.html
1.3 pure handwriting simple web framework
<!--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>
    <<Password:>the 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

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/xt12321/p/10980177.html