• socketserver模块三次登陆验证,身份验证


    帅爆太阳的男人

    1,socketserver是解决TCP服务器和多个客户端进行通信

    • 服务器:
    import socketserver
    
    
    class MySocket(socketserver,BaseRequestHandle):  # 里边的参数也是固定的,不用改变
        def handle(self):  # 方法的名字时固定的,必须是这个名字
            msg = self.request.recv(1024).decode("utf-8")
            print(msg)
            self.request.send(msg.upper().encode("utf-8"))
    
    
    # 号里的两个参数第一个是地址,第二个是类名(增加一个线程)
    server = socketserver.ThreadingTCPServer(("127.0.0.1",8001),Mysocket)
    server.ser_forver()  # 开启一个永久性的服务
    • 客户端:
    import socket
    
    sk = socket.socket()
    sk.connect(("127.0.0.1",8001))
    msg_s = input(">>>")
    sk.send(msg_s.encode("utf-8"))
    print(sk.recv(1024).decode("utf-8"))
    sk.close()

    2,三次登录验证:

    • client:
    import socket
    import hashlib
    import json
    sk = socket.socket()
    sk.connect(("127.0.0.1",8080))
    dic = {"status":False,"username":username,"password":None}
    c = 3
    while c:
        username = input("请输入用户名:")
        password = input("请输入密码:")
        md5_obj = hashlib.md5(password.encode("utf-8"))
        md5_obj.update(username.encode("utf-8"))
        pawd_m = md5_obj.hexdigest()
        dic["username"] = username
        dic["password"] = pawd_m
        str_dic = json.dumps(dic)
        sk.send(str_dic.encode("utf-8"))
        res_dic = sk.recv(1024).decode("utf-8")
        result = json.loads(res_dic)
        if result["status"]:
            print("登录成功")
        else:
            print("失败, %s" % result["reason"])
        c = c - 1
        sk.close()
    • 服务器
    import socketserver
    import json
    import hashlib
    
    
    def zhuce():
        pass
    
    
    class MySocket(socketserver.BaseRequestHandler):
        def handle(self):
            sor = b"wusir"
            while 1:
                str_dic = self.request.recv(1024).decode("utf-8")
                if not str_dic:
                    break
                dic = json.loads(str_dic)
                if not dic["status"]:
                    with open("info", "r", encoding="utf-8") as f:
                        for info in f:
                            username, pawd_txt = info.strip().split("|")
                            if username == dic["username"]:
                                md5_obj = hashlib.md5(sor)
                                md5_obj.update(dic["password"].encode("utf-8"))
                                pawd = md5_obj.hexdigest()
                                if pawd_txt == pawd:
                                    dic["status"] = True
                                else:
                                    dic["reason"] = "密码错误"
                                    break
                            else:
                                dic["reason"] = "用户不存在"
                                zhuce()
                                str_dic = json.dumps(dic)
                                self.request.send(str_dic.encode("utf-8"))
                        else:
                            self.request.send("已经登录成功".encode("utf-8"))
    
    
    server = socketserver.TCPServer(("127.0.0.1", 8080), MySocket)
    
    server.serve_forever()

    3,进度条

    import time  # 导入时间模块
    for i in range(51):  # 因为每次输出的是每个循环数乘以2,所以要达成100%,所以循环遍历50就可以了
        time.sleep(0.12)  # 每当循环出一个数就成序就在这休眠0.12秒
        print("
    " + i * "=" + ">" + str(i * 2) + "%", end="")  # 固定写法(
    之前的内容不管是什么都不会输出)
  • 相关阅读:
    解密时遇到 填充无效 无法被移除
    固态硬盘SSD,机械硬盘HDD,4K速度对比。
    onsubmit ajax return false 无效
    chrome flash
    ubuntu base make 未找到命令
    winrar 压缩命令
    查看耗时长,CPU 100% 的SQL
    【转】SQL Server日志文件过大 大日志文件清理方法 不分离数据库
    安装老版本redis .NET 客户端
    python2.0_day22_web聊天室二
  • 原文地址:https://www.cnblogs.com/ljc-0923/p/9505939.html
Copyright © 2020-2023  润新知