python实现账号密码登录

用Python实现账号密码登录

准备

无需数据库

程序简介

运行这个程序,注册后哪怕重新运行,注册的账号依然存在,我们可以把账号密码分别保存到2个txt中,使用的时候再读取txt

为了安全起见,我们可以对账号密码进行加密,在进行保存,读取时也将用户输入的内容进行加密,在进行读取,加密代码如下:

add = 0
s = 1
str_add = ''
inv_str_add = ''
buf = input('请输入账号:')
for i in buf:
    add += ord(i)
    str_add += str(ord(i))
for i in str_add:
    s += 1
for i in range(1, s):
    inv_str_add += str_add[-i]
print('加密前:{}\n加密后:{}' .format(buf, add + int(inv_str_add)))

因为要考虑周全,所以总体代码较长,代码如下:

s = input('您有账号吗(有回“1”,没有回“2”):')
if s == '2':
    print('正在为您注册账号···')
    add = 0
    s = 1
    str_add = ''
    inv_str_add = ''
    bu = input('请输入您的新用户名:')
    for i in bu:
        add += ord(i)
        str_add += str(ord(i))
    for i in str_add:
        s += 1
    for i in range(1, s):
        inv_str_add += str_add[-i]
    a1 = ('{}{}' .format(bu, add + int(inv_str_add)))
    p = input('请输入您的密码:')
    i = input('请再次输入您的密码:')
    if p==i:
        print('好的,已为您自动登录')
        e = open('用户名.txt','a')
        #r = '{'+'\''+y+'\''+':'+'\''+p+'\'''}'
        e.write(a1)
        e.write('\n')
        e.close()
        add = 0
        s = 1
        str_add = ''
        inv_str_add = ''
        for i in p:
            add += ord(i)
            str_add += str(ord(i))
        for i in str_add:
            s += 1
        for i in range(1, s):
            inv_str_add += str_add[-i]
        a = ('{}{}' .format(p, add + int(inv_str_add)))
        e1 = open('密码.txt','a')
        #r = '{'+'\''+y+'\''+':'+'\''+p+'\'''}'
        e1.write(a)
        e1.write('\n')
        e1.close()
        print(1)
    else:
        print('两次密码不一致,请重新输入')
        while True:
            print('正在为您注册账号···')
            add = 0
            s = 1
            str_add = ''
            inv_str_add = ''
            bu = input('请输入您的新用户名:')
            for i in bu:
                add += ord(i)
                str_add += str(ord(i))
            for i in str_add:
                s += 1
            for i in range(1, s):
                inv_str_add += str_add[-i]
            a1 = ('{}{}' .format(bu, add + int(inv_str_add)))
            p = input('请输入您的密码:')
            i = input('请再次输入您的密码:')
            if p==i:
                print('好的,已为您自动登录')
                e = open('用户名.txt','a')
                #r = '{'+'\''+y+'\''+':'+'\''+p+'\'''}'
                e.write(a1)
                e.write('\n')
                e.close()
                add = 0
                s = 1
                str_add = ''
                inv_str_add = ''
                for i in p:
                    add += ord(i)
                    str_add += str(ord(i))
                for i in str_add:
                    s += 1
                for i in range(1, s):
                    inv_str_add += str_add[-i]
                a = ('{}{}' .format(p, add + int(inv_str_add)))
                e1 = open('密码.txt','a')
                #r = '{'+'\''+y+'\''+':'+'\''+p+'\'''}'
                e1.write(a)
                e1.write('\n')
                e1.close()
                break
                print(1)
            else:
                print('两次密码不一致,请重新输入')
if s =='1':
    add = 0
    s = 1
    str_add = ''
    inv_str_add = ''
    bu = input('请输入您的用户名:')
    for i in bu:
        add += ord(i)
        str_add += str(ord(i))
    for i in str_add:
        s += 1
    for i in range(1, s):
        inv_str_add += str_add[-i]
    d = ('{}{}' .format(bu, add + int(inv_str_add)))
    u = open('用户名.txt','r')
    h = u.read()
    if d in h:
        add = 0
        s = 1
        str_add = ''
        inv_str_add = ''
        bu = input('请输入您的密码:')
        for i in bu:
            add += ord(i)
            str_add += str(ord(i))
        for i in str_add:
            s += 1
        for i in range(1, s):
            inv_str_add += str_add[-i]
        j = ('{}{}' .format(bu, add + int(inv_str_add)))
        e1 = open('密码.txt','r')
        f = e1.read()
        e1.close()
        if j in f:
            print('登录成功!')
            print(1)
        else:
            print('密码错误')
            add = 0
            s = 1
            str_add = ''
            inv_str_add = ''
            bu = input('请再次输入::')
            for i in bu:
                add += ord(i)
                str_add += str(ord(i))
            for i in str_add:
                s += 1
            for i in range(1, s):
                inv_str_add += str_add[-i]
            qw = ('{}{}' .format(bu, add + int(inv_str_add)))
            if qw in f:
                print('登录成功')
                print(1)
            else:
                print('正在为您注册账号···')
                add = 0
                s = 1
                str_add = ''
                inv_str_add = ''
                bu = input('请输入您的新用户名:')
                for i in bu:
                    add += ord(i)
                    str_add += str(ord(i))
                for i in str_add:
                    s += 1
                for i in range(1, s):
                    inv_str_add += str_add[-i]
                y = ('{}{}' .format(bu, add + int(inv_str_add)))
                p = input('请输入您的密码:')
                i = input('请再次输入您的密码:')
                if p==i:
                    e = open('用户名.txt','a+')
                    #r = '{'+'\''+y+'\''+':'+'\''+p+'\'''}'
                    e.write(y)
                    e.write('\n')
                    e.close()
                    add = 0
                    s = 1
                    str_add = ''
                    inv_str_add = ''
                    for i in p:
                        add += ord(i)
                        str_add += str(ord(i))
                    for i in str_add:
                        s += 1
                    for i in range(1, s):
                        inv_str_add += str_add[-i]
                    rt = ('{}{}' .format(p, add + int(inv_str_add)))
                    e1 = open('密码.txt','a+')
                    #r = '{'+'\''+y+'\''+':'+'\''+p+'\'''}'
                    e1.write(rt)
                    e1.write('\n')
                    e1.close()
                    print(1)
                else:
                    print('两次密码不一致,请重新输入')
                    while True:
                        print('正在为您注册账号···')
                        add = 0
                        s = 1
                        str_add = ''
                        inv_str_add = ''
                        bu = input('请输入您的新用户名:')
                        for i in bu:
                            add += ord(i)
                            str_add += str(ord(i))
                        for i in str_add:
                            s += 1
                        for i in range(1, s):
                            inv_str_add += str_add[-i]
                        a1 = ('{}{}' .format(bu, add + int(inv_str_add)))
                        p = input('请输入您的密码:')
                        i = input('请再次输入您的密码:')
                        if p==i:
                            print('好的,已为您自动登录')
                            e = open('用户名.txt','a')
                            #r = '{'+'\''+y+'\''+':'+'\''+p+'\'''}'
                            e.write(a1)
                            e.write('\n')
                            e.close()
                            add = 0
                            s = 1
                            str_add = ''
                            inv_str_add = ''
                            for i in p:
                                add += ord(i)
                                str_add += str(ord(i))
                            for i in str_add:
                                s += 1
                            for i in range(1, s):
                                inv_str_add += str_add[-i]
                            a = ('{}{}' .format(p, add + int(inv_str_add)))
                            e1 = open('密码.txt','a')
                            #r = '{'+'\''+y+'\''+':'+'\''+p+'\'''}'
                            e1.write(a)
                            e1.write('\n')
                            e1.close()
                            break
                            print(1)
                        else:
                            print('两次密码不一致,请重新输入')
    else:
        print('对不起,没有查询到此账号')
        n = input('您是否需要注册账号,(回答‘是’或‘否’):')
        if n =='是' :
            print('两次密码不一致,请重新输入')
            while True:
                print('正在为您注册账号···')
                add = 0
                s = 1
                str_add = ''
                inv_str_add = ''
                bu = input('请输入您的新用户名:')
                for i in bu:
                    add += ord(i)
                    str_add += str(ord(i))
                for i in str_add:
                    s += 1
                for i in range(1, s):
                    inv_str_add += str_add[-i]
                a1 = ('{}{}' .format(bu, add + int(inv_str_add)))
                p = input('请输入您的密码:')
                i = input('请再次输入您的密码:')
                if p==i:
                    print('好的,已为您自动登录')
                    e = open('用户名.txt','a')
                    #r = '{'+'\''+y+'\''+':'+'\''+p+'\'''}'
                    e.write(a1)
                    e.write('\n')
                    e.close()
                    add = 0
                    s = 1
                    str_add = ''
                    inv_str_add = ''
                    for i in p:
                        add += ord(i)
                        str_add += str(ord(i))
                    for i in str_add:
                        s += 1
                    for i in range(1, s):
                        inv_str_add += str_add[-i]
                    a = ('{}{}' .format(p, add + int(inv_str_add)))
                    e1 = open('密码.txt','a')
                    #r = '{'+'\''+y+'\''+':'+'\''+p+'\'''}'
                    e1.write(a)
                    e1.write('\n')
                    e1.close()
                    break
                    print(1)
            else:
                print('两次密码不一致,请重新输入')
                while True:
                    print('正在为您注册账号···')
                    add = 0
                    s = 1
                    str_add = ''
                    inv_str_add = ''
                    bu = input('请输入您的新用户名:')
                    for i in bu:
                        add += ord(i)
                        str_add += str(ord(i))
                    for i in str_add:
                        s += 1
                    for i in range(1, s):
                        inv_str_add += str_add[-i]
                    a1 = ('{}{}' .format(bu, add + int(inv_str_add)))
                    p = input('请输入您的密码:')
                    i = input('请再次输入您的密码:')
                    if p==i:
                        print('好的,已为您自动登录')
                        e = open('用户名.txt','a')
                        #r = '{'+'\''+y+'\''+':'+'\''+p+'\'''}'
                        e.write(a1)
                        e.write('\n')
                        e.close()
                        add = 0
                        s = 1
                        str_add = ''
                        inv_str_add = ''
                        for i in p:
                            add += ord(i)
                            str_add += str(ord(i))
                        for i in str_add:
                            s += 1
                        for i in range(1, s):
                            inv_str_add += str_add[-i]
                        a = ('{}{}' .format(p, add + int(inv_str_add)))
                        e1 = open('密码.txt','a')
                        #r = '{'+'\''+y+'\''+':'+'\''+p+'\'''}'
                        e1.write(a)
                        e1.write('\n')
                        e1.close()
                        break
                        print(1)
                    else:
                        print('两次密码不一致,请重新输入')
        else:
            while True:
                add = 0
                s = 1
                str_add = ''
                inv_str_add = ''
                bu = input('请输入 用户名:')
                for i in bu:
                    add += ord(i)
                    str_add += str(ord(i))
                for i in str_add:
                    s += 1
                for i in range(1, s):
                    inv_str_add += str_add[-i]
                d = ('{}{}' .format(bu, add + int(inv_str_add)))
                u = open('用户名.txt','r')
                h = u.read()
                u.close()
                e1 = open('密码.txt','r')
                h1 = e1.read()
                e1.close()
                if d in h:
                    add = 0
                    s = 1
                    str_add = ''
                    inv_str_add = ''
                    bu = input('请输入密码:')
                    for i in bu:
                        add += ord(i)
                        str_add += str(ord(i))
                    for i in str_add:
                        s += 1
                    for i in range(1, s):
                        inv_str_add += str_add[-i]
                    j = ('{}{}' .format(bu, add + int(inv_str_add)))
                    if j in h1:
                        print('登录成功!')
                        break
                        print(1)
                    else:
                        print('正在为您注册账号···')
                        add = 0
                        s = 1
                        str_add = ''
                        inv_str_add = ''
                        bu = input('请输入您的新用户名:')
                        for i in bu:
                            add += ord(i)
                            str_add += str(ord(i))
                        for i in str_add:
                            s += 1
                        for i in range(1, s):
                            inv_str_add += str_add[-i]
                        a1 = ('{}{}' .format(bu, add + int(inv_str_add)))
                        p = input('请输入您的密码:')
                        i = input('请再次输入您的密码:')
                        if p==i:
                            print('好的,已为您自动登录')
                            e = open('用户名.txt','a')
                            #r = '{'+'\''+y+'\''+':'+'\''+p+'\'''}'
                            e.write(a1)
                            e.write('\n')
                            e.close()
                            add = 0
                            s = 1
                            str_add = ''
                            inv_str_add = ''
                            for i in p:
                                add += ord(i)
                                str_add += str(ord(i))
                            for i in str_add:
                                s += 1
                            for i in range(1, s):
                                inv_str_add += str_add[-i]
                            a = ('{}{}' .format(p, add + int(inv_str_add)))
                            e1 = open('密码.txt','a')
                            #r = '{'+'\''+y+'\''+':'+'\''+p+'\'''}'
                            e1.write(a)
                            e1.write('\n')
                            e1.close()
                            break
                            print(1)
                        else:
                            print('两次密码不一致,请重新输入')
                else:
                    print('对不起,没有查询到此账号')
                    n = input('您是否需要注册账号,(回答‘是’或‘否’):')
                    if n =='是' :
                        print('对不起,没有查询到此账号')
                        n = input('您是否需要注册账号,(回答‘是’或‘否’):')
                        if n =='是' :
                            print('两次密码不一致,请重新输入')
                            while True:
                                print('正在为您注册账号···')
                                add = 0
                                s = 1
                                str_add = ''
                                inv_str_add = ''
                                bu = input('请输入您的新用户名:')
                                for i in bu:
                                    add += ord(i)
                                    str_add += str(ord(i))
                                for i in str_add:
                                    s += 1
                                for i in range(1, s):
                                    inv_str_add += str_add[-i]
                                a1 = ('{}{}' .format(bu, add + int(inv_str_add)))
                                p = input('请输入您的密码:')
                                i = input('请再次输入您的密码:')
                                if p==i:
                                    print('好的,已为您自动登录')
                                    e = open('用户名.txt','a')
                                    #r = '{'+'\''+y+'\''+':'+'\''+p+'\'''}'
                                    e.write(a1)
                                    e.write('\n')
                                    e.close()
                                    add = 0
                                    s = 1
                                    str_add = ''
                                    inv_str_add = ''
                                    for i in p:
                                        add += ord(i)
                                        str_add += str(ord(i))
                                    for i in str_add:
                                        s += 1
                                    for i in range(1, s):
                                        inv_str_add += str_add[-i]
                                    a = ('{}{}' .format(p, add + int(inv_str_add)))
                                    e1 = open('密码.txt','a')
                                    #r = '{'+'\''+y+'\''+':'+'\''+p+'\'''}'
                                    e1.write(a)
                                    e1.write('\n')
                                    e1.close()
                                    break
                break
            print(1)

运行结果:

 

 

  • 3
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python爬虫中,如果需要输入账号密码进行模拟登录,有两种常见的方法可以实现。第一种方法是使用模拟登录,即先使用账号密码模拟登录,得到相应的cookie,然后再使用该cookie登录网站。这种方法可以使用urllib库来实现,具体代码如下: import urllib.request import urllib.parse # 模拟登录获取cookie login_url = "http://example.com/login" # 登录页面的URL data = {'username': 'your_username', 'password': 'your_password'} # 账号密码 data = urllib.parse.urlencode(data).encode('utf-8') # 编码数据 req = urllib.request.Request(login_url, data=data) # 构造请求 response = urllib.request.urlopen(req) # 发送请求 cookie = response.headers['Set-Cookie'] # 获取cookie # 使用cookie登录网站 url = "http://example.com/protected_page" # 需要登录的页面URL headers = {'Cookie': cookie} # 设置请求头中的cookie req = urllib.request.Request(url, headers=headers) # 构造请求 content = urllib.request.urlopen(req) # 发送请求 html = content.read().decode('utf-8') # 获取页面内容 这样,通过模拟登录获取到的cookie可以在之后的请求中使用,以获取需要登录才能访问的页面。 另一种方法是直接在请求头中添加cookie,将cookie中包含的账号密码信息写入headers中,以实现登录。具体代码如下: import urllib.request url = "http://example.com/protected_page" # 需要登录的页面URL headers = {'Cookie': "your_cookie"} # 设置请求头中的cookie,将"your_cookie"替换为实际的cookie值 req = urllib.request.Request(url, headers=headers) # 构造请求 content = urllib.request.urlopen(req) # 发送请求 html = content.read().decode('utf-8') # 获取页面内容 这样,通过在请求头中添加cookie的方式,可以直接发送带有账号密码信息的请求,以实现登录并获取需要登录才能访问的页面。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [python账号密码的爬取](https://blog.csdn.net/weixin_39695028/article/details/80299078)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值