• configparser和hashlib模块


    import configparser
    config=configparser.ConfigParser()
    #创建文件和内容
    # config["DEFAULT"]={"serveraliveinterbval":"45",
    # "Compression":"yes",
    # "compressionlevel":"9"}
    # config["bitbuvket"]={}
    # config["bitbuvket"]["user"]="hg"
    # config["topsecret.server.com"]={}
    # topsecret=config["topsecret.server.com"]
    # topsecret["host port"]="50022"
    # topsecret["forwardx11"]="no"
    # config["DEFAULT"]["forwardx11"]="yes"
    # with open("example.ini","w") as configfile:
    # config.write(configfile)


    # config.read("example.ini")#读取文件内容
    #---------------------------------------查询

    # sesc=config.sections()# 对内容进行划分,得到所有的章节名
    # print(sesc)# 以列表形式打印章节名
    # #['bitbuvket', 'topsecret.server.com']
    # print("bitbuvket" in sesc)
    # #True
    # print(config["bitbuvket"]["user"])
    # #hg
    # print(config["DEFAULT"]["Compression"])
    # #yes
    # for i in config["bitbuvket"]:
    # print(i)
    # # user
    # # serveraliveinterbval
    # # compression
    # # compressionlevel
    # # forwardx11

    # opts=config.options("topsecret.server.com")
    # print(opts)#以列表形式打印topsecret.server.com章节里面的Key(默认值也会打印,但要去除重复名称的)
    # #['host port', 'forwardx11', 'serveraliveinterbval', 'compression', 'compressionlevel']

    # item=config.items("topsecret.server.com")
    # print(item)# 以列表形式打印topsecret.server.com章节的(key, value)
    # #[('serveraliveinterbval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'no'), ('host port', '50022')]

    # str_val = config.get("topsecret.server.com", "host port")
    # print(str_val)# 返回"topsecret.server.com"章节里面key为"host port"的值,返回为string类型
    # #50022

    #-------------------------------------增加--
    # config.add_section("yuan")#增加一个yuan的章节
    # config.set("yuan","k1","11111")#在yuan的章节下添加一个键值对(如果已经存在就不能添加)

    #----------------------------------------删除

    # config.remove_section("topsecret.server.com")#删除topsecret.server.com章节
    # config.remove_option("bitbuvket","user")#删除"bitbuvket"中"user"的键值对
    # config.write(open("example.ini","w"))


    #==========================================hashlib加密===================
    import hashlib
    has=hashlib.md5()#md5对象,md5不能反解,但是加密是固定的值,就是关系是一一对应.所以有缺陷,可能被对撞出来
    has.update("abdadmin".encode("utf8"))#对那个字符串进行加密
    print(has.hexdigest())#21232f297a57a5a743894a0e4a801fc3 #拿到加密字符

    has1=hashlib.sha256()#不同算法,hashlib很多加密算法
    has1.update("123456789".encode("utf8"))
    print(has1.hexdigest())#15e2b0d3c33891ebb0f1ef609ec419420c20e320ce94c65fbc8c3312448eb225


    hash3 = hashlib.md5(bytes('abd',encoding='utf-8'))
    ''' 如果没有参数,所以md5遵守一个规则,生成同一个对应关系,如果加了参数,
    就是在原先加密的基础上再加密一层,这样的话参数只有自己知道,防止被撞库,
    因为别人永远拿不到这个参数
    '''
    hash3.update(bytes('admin',encoding='utf-8'))
    print(hash3.hexdigest())#9aea3c0a6c51555c1a4d0a5e9b689ded


    #这里写一个利用md5进行用户登陆网站进行注册之后密码加密的基本事例,加深理解
    #hashliblib简单实用
    def md5(arg):
    md5_pwd=hashlib.md5()
    md5_pwd.update(arg.encode("utf8"))
    return md5_pwd.hexdigest()

    def login(user,pwd):
    with open("text","r") as f:
    for i in f:
    u,p=i.strip().split(":")
    m=md5(p)
    if user==u and pwd==m:
    return True
    def register(user,pwd):
    with open("text","a") as f:
    temp=":".join([user,pwd])
    f.write(temp)

    i=input("""
    1.表示登陆:
    2.表示注册:""")
    if i=="1":
    user=input("请输入账号")
    pwd=input("请输入密码")
    pwd=md5(pwd)
    if login(user,pwd):
    print("登陆成功")
    else:
    print("账号或密码错误")
    elif i=="2":
    user=input("请输入账号")
    pwd=input("请输入密码")
    register(user,pwd)
    else:
    print("输入有误")
  • 相关阅读:
    虚拟环境和包
    1105471854403716
    Sunshine on my shoulders
    ERROR: Command errored out with exit status 1:
    JAVA调用SAP ODATA服务
    reacthooks学习
    mtalb 密度图 制作
    vue3.0 + vite + ts 完成自动导入 vue API 和 自动导入组件
    【报告笔记】作物顺式调控模块的挖掘与利用
    【WDL】5. 实践与建议
  • 原文地址:https://www.cnblogs.com/liuwenwen/p/12923730.html
Copyright © 2020-2023  润新知