码迷,mamicode.com
首页 > Web开发 > 详细

Web——form表单

时间:2019-08-28 22:33:54      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:container   containe   decode   密码   ali   ini   listen   __name__   obj   

HTML中的form表单有GET或POST方法。区别如下:

  • 【get:form表单里所填的值会附加在action指定的URL后面】
  • 【post:form表单里所填的值会附在headers上】

使用python创建一个web服务器,如果接收到method为get,则返回注册页面,让用户填写注册信息。一旦用户点下form中的注册按钮,客户端会发送POST请求,form中的值会跟在headers最后,接收到信息后返回一个打印用户输入信息的页面。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>QQ注册</title>
    <style type="text/css">
        .container{
            width: 100%;
            text-align: center;
        }
        .haomaTitle{
            width: 604px;
            height: 30px;
            line-height: 25px;
            font-size: 18px;
            margin: 10px auto;
            border-bottom: 1px #ddd solid;
        }
        table{
            margin: 0 auto;
        }
        .tip{
            text-align: right;
        }
        .btnSubmit{
            text-align: center;
        }
    </style>
</head>
<body>
    <div class=‘container‘>
        <div class=‘haomaTitle‘ id=‘hmtitle‘>注册账号</div>
        <form action=‘‘ method=‘post‘>
            <table>
                <tr>
                    <td class="tip">用户名:</td>
                    <td><input type="text" name="uname"></td>
                </tr>
                <tr>
                    <td class=‘tip‘>密码:</td>
                    <td><input type="password" name="pwd"></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" name="submit" value=‘注册‘></td>
                </tr>
            </table>
        </form>
    </div>
</body>
</html>
# register.py

from socket import *
from select import select

class ClientHandler(object):
    def __init__(self):
        """初始化类,创建流式套接字并绑定端口"""
        self.socket = socket(AF_INET, SOCK_STREAM)
        self.socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
        self.socket.bind((0.0.0.0, 5555))
        self.socket.listen(5)

    def serve_forever(self):
        """运行,使用select.select实现I/O多路复用"""
        rlist = [self.socket]
        wlist = []
        xlist = []

        while True:
            print(Waiting for connection...)
            rs, ls, xs = select(rlist, wlist, xlist)

            for r in rs:
                if r is self.socket:
                    conn, addr = self.socket.accept()
                    print("Connected from", addr)
                    rs.append(conn)
                else:
                    data = r.recv(1024).decode()
                    requestlines = data.splitlines()
                    method, url, _ = requestlines[0].split( )
                    headers = requestlines[1:]

                    if method == GET:
                        fp = open(html/04-register.html)
                        response = HTTP/1.1 200 OK\r\n
                        response += \r\n
                        response += fp.read()
                    elif method == POST:
                        params = {}
                        for arg in requestlines[-1].split(&):
                            key, value = arg.split(=)
                            params[key] = value
                        response = HTTP/1.1 200 OK\r\n
                        response += \r\n
                        response += <table>
                        for k, v in params.items():
                            print(k, v)
                            response += f<tr><td>{k}</td><td>{v}</td></tr>
                        response += </table>
                        print(response)

                    r.send(response.encode())



if __name__ == __main__:
    ClientHandler().serve_forever()

 

 

Web——form表单

标签:container   containe   decode   密码   ali   ini   listen   __name__   obj   

原文地址:https://www.cnblogs.com/noonjuan/p/11426810.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!