将 github 数据库添加到 Django 项目

时间:2021-04-24 06:33:57

标签: python django database github web

def HashBrut(request):
    
    sha1hash = request.POST.get('decoder','default')
    time.sleep(4)

    LIST_OF_COMMON_PASSWORDS = str(urlopen('https://raw.githubusercontent.com/danielmiessler/SecLists/master/Passwords/Common-Credentials/10-million-password-list-top-10000.txt').read(), 'utf-8')

    for guess in LIST_OF_COMMON_PASSWORDS.split('\n'):

        hashedGuess = hashlib.sha1(bytes(guess, 'utf-8')).hexdigest()
        if hashedGuess == sha1hash:
            val=hashedGuess
            print("The password is ", str(guess))
            ans=str(guess)
            quit()
      
        elif hashedGuess != sha1hash:
            print("Password guess ",str(guess)," does not match, trying next...")

    print("Password not in database, we'll get them next time.")
    params={'text':val,'text1':ans}
    return render(request,'Hashed.html',params)

我正在尝试从 django 项目中的 github 访问这个“LIST_OF_COMMON_PASSWORDS”。 python 函数在终端中运行良好,但是当我在 Django 项目的 views.py 文件中使用它并尝试在 web 文件上执行它时,它给出 '发生服务器错误。请与管理员联系。' 错误。我是 Django 的新手,我不知道出了什么问题。

1 个答案:

答案 0 :(得分:0)

您代码中的第一个问题是您编写了这一行 quit(),这基本上意味着关闭正在运行的 Python 进程!要打破循环,通常使用break 语句。接下来,如果密码不匹配,您可能会打印 10000 个!打印到控制台确实需要一些时间,打印 10000 次会使您的请求在服务器向客户端发送响应之前超时。如果没有匹配就不要打印,继续。另外,有可能永远不会定义 ansval,这也可能导致错误,请在开始时使用某个值定义它:

def HashBrut(request):
    ans, val = None, None
    sha1hash = request.POST.get('decoder','default')
    time.sleep(4)

    LIST_OF_COMMON_PASSWORDS = str(urlopen('https://raw.githubusercontent.com/danielmiessler/SecLists/master/Passwords/Common-Credentials/10-million-password-list-top-10000.txt').read(), 'utf-8')

    for guess in LIST_OF_COMMON_PASSWORDS.split('\n'):

        hashedGuess = hashlib.sha1(bytes(guess, 'utf-8')).hexdigest()
        if hashedGuess == sha1hash:
            val=hashedGuess
            print("The password is ", str(guess))
            ans=str(guess)
            # No `quit()` here! use `break`
            break
        # No print here, takes too much time
    print("Password not in database, we'll get them next time.")
    params={'text':val,'text1':ans}
    return render(request,'Hashed.html',params)