意外的Python缩进错误

-7 投票
2 回答
5424 浏览
提问于 2025-04-18 05:14

下面的代码让我很烦,我在StackOverflow和谷歌上找了很多资料,但还是没找到解决办法。我算是个不错的Python程序员,但到现在为止,我还没遇到过解决不了的错误。

我试了各种方法,但这段代码还是让我遇到了问题:

IndentationError: unexpected unindent 

这很奇怪,因为通常的错误是“意外的缩进”,这意味着代码的空格有问题。很多人都提到过这个,所以我仔细检查了整个代码,结果还是一样的错误。我确认每个地方都用四个空格对齐,但还是没用……求助!

这个脚本是出于安全原因和网络应用的需要。

#!/usr/bin/python

#!/usr/bin/python
#Greets:SEC4EVER Members.
import hashlib
import base64
import socket
print"""

___  ___ _    _____ _____
|  \/  || |  |_   _|_   _|
| .  . || |    | |   | |
| |\/| || |    | |   | |
| |  | || |____| |  _| |_
\_|  |_/\_____/\_/  \___/
v0.1
"""


def main():
 print '1 - SHA1  Decrypter'; print
print '2 - MD5  Decrypter'
print '3 - Base64 Decrypter'
print '4 - /etc/passwd users extractor'
print '5 - Port Scanner'
elect = input("Select :")

if select==1:
      sha1()
elif select==2:
      md5()
elif select==3:
      base64()
elif select==4:
      extr()
elif select==5:
      scanner()


def   sha1():
    try:
        sha1 = raw_input("\t\n\nSHA1 Hash:")
        dictionary = open("pwds.txt","r")

        for passwd in dictionary.read().split('\n'):
            if hashlib.sha1(passwd).hexdigest() == sha1:

                print("\n\t[OK]"+sha1+" : "+passwd+"\n")


        else:
            print "\n\tFailed; Password not found in dictionary"
            sha1()
    except(IOError):
        print "pwds.txt not found!"
main()
def md5():
    try:
        md5 = raw_input("\t\n\nMD5 Hash:")
        dictionary = open("pwds.txt","r")
        for passwd in dictionary.read().split('\n'):
            if hashlib.md5(passwd).hexdigest() == md5:
                print("\n\t[OK]"+md5+" : "+passwd+"\n")


        else:
            print "\n\tFailed; Password not found in dictionary"
            main()
    except(IOError):
        print "pwds.txt not found!"

def base64():
    try:
     code = raw_input('\n\nBase64:\n\n')
     deco = base64.b64decode(code)
     print '\nDecoded!:\n\n',deco,'\n'
     main()

     def extr():
      try:
       xer = raw_input("/etc/passwd content:")
       passwd_content = xer.split('\n')

       for line in passwd_content :
        y = line.find(':')
       print line[0:y]
       main()

       def scanner():
        try:
         sec4 =raw_input('IP Address:')
         for port in range(1,400):
          s0ck = socket.socket()
         s0ck.settimeout(0.5)
         ip = sec4
         response = s0ck.connect_ex((ip, port))
         if response:
          print ("%d\tclose" %port)
         else:
          print ("%d\topen" %port)
         s0ck.close()

if __name__ !== '__main__': main()

2 个回答

1

你的 def main() 代码的缩进不正确,应该是:

def main():
    print '1 - SHA1  Decrypter'; print
    print '2 - MD5  Decrypter'
    print '3 - Base64 Decrypter'
    print '4 - /etc/passwd users extractor'
    print '5 - Port Scanner'
    elect = input("Select :")

    if select==1:
        sha1()
    elif select==2:
        md5()
    elif select==3:
        base64()
    elif select==4:
        extr()
    elif select==5:
        scanner()

另外,请至少使用 2 个空格来缩进代码,这样更容易阅读。

更新你的评论:

在这种情况下,正如 @alexce 所说,是因为 try 块没有匹配好。

5

在你写的 base64() 函数里,你打开了好几个 try/except/finally 的结构,但实际上你没有写 exceptfinally,只有很多 try 的部分。

另外,如果你打算继续成为一个“相当不错的 Python 程序员”,建议你使用 4 个空格来缩进代码,并尽量遵循 PEP8 的风格指南。

撰写回答