缩进错误 Python 无效

Indentation Error Python Not Working

我正在尝试 运行 我的代码,并且有一个

File "C:/trcrt/trcrt.py", line 42
def checkInternet():
^
IndentationError: unexpected unindent

代码应该检查网站的跟踪路由...我知道...它不是很聪明,但它是我被告知要做的 我使用 pep8 检查了代码,一切似乎都很好...

'''
Developer: Roei Edri
File name: trcrt.py
Date: 24.11.17
Version: 1.1.0
Description: Get an url as an input and prints the traceroute to it.
'''

import sys
import urllib2
i, o, e = sys.stdin, sys.stdout, sys.stderr 
from scapy.all import *
from scapy.layers.inet import *
sys.stdin, sys.stdout, sys.stderr = i, o, e


def trcrt(dst):
    """
    Check for the route for the given destination
    :param dst: Final destination, in a form of a website.
    :type dst: str
    """
    try:
        pckt = IP(dst=dst)/ICMP()                        # Creates the 
                                                         # packet
        ip = [p for p in pckt.dst]                       # Gets the ip
        print "Tracerouting for  {0}  :  {1}".format(dst, ip[0])
        for ttl in range(1, 40):
            pckt = IP(ttl=ttl, dst=dst)/ICMP()
            timeBefore = time.time()
            reply = sr1(pckt, verbose=0, timeout=5)
            timeAfter = time.time()
            timeForReply = (timeAfter - timeBefore)*1000
            if reply is not None:
                print "{0} : {1} ; Time for reply: {2}".format(ttl, 
                reply.src, timeForReply)
                if reply.type == 0:
                    print "Tracerout Completed"
                    break
            else:
                print "{0} ... Request Time Out".format(ttl)


def checkInternet():
    """
    Checks if there is an internet connection
    :return: True if there is an internet connection
    """
    try:
        urllib2.urlopen('http://45.33.21.159', timeout=1)
        return True
    except urllib2.URLError as IntError:
        return False

感谢您的帮助... 顺便说一句 pep8 说 "module level import not at top of file" 对于第 12,13 行

try 块缺少其 except 子句。

try:
    pckt = IP(dst=dst)/ICMP()                        # Creates the 
                                                     # packet
    ip = [p for p in pckt.dst]                       # Gets the ip
    print "Tracerouting for  {0}  :  {1}".format(dst, ip[0])
    for ttl in range(1, 40):
        pckt = IP(ttl=ttl, dst=dst)/ICMP()
        timeBefore = time.time()
        reply = sr1(pckt, verbose=0, timeout=5)
        timeAfter = time.time()
        timeForReply = (timeAfter - timeBefore)*1000
        if reply is not None:
            print "{0} : {1} ; Time for reply: {2}".format(ttl, 
            reply.src, timeForReply)
            if reply.type == 0:
                print "Tracerout Completed"
                break
        else:
            print "{0} ... Request Time Out".format(ttl)

except:   # Here : Add the exception you wish to catch                                    
    pass  # handle this exception appropriately 

作为一般规则,不要使用 catch all except 子句,也不要 pass 捕获异常,它会静静地失败。

如果这是您的完整代码,则需要检查两件事: 1) 你混用了制表符和空格吗?确保所有制表符都转换为空格(我建议每个制表符 4 个空格)以进行缩进。一个好的 IDE 会为你做这件事。

2) trcrt(dst) 中的 try: 没有匹配的 except 块。

PEP8 也会顺便告诉你,函数名应该是小写的: check_internet 而不是 checkInternet,...

我会给你同样的建议,我给所有和我一起工作的人:开始使用 IDE 标记 PEP8 和其他错误,周围有多个。它有助于发现这些错误,并训练您编写干净的 Python 代码,这些代码易于阅读,并且(如果您在其中添加注释)几年后也可以重复使用和理解。