簡體   English   中英

縮進錯誤Python無法正常工作

[英]Indentation Error Python Not Working

我試圖運行我的代碼,有一個

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

代碼應該檢查到網站的路由跟蹤...我知道...它不是很聰明,但是我被告知要做的是我使用pep8檢查了代碼,eveything似乎還不錯...

'''
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

感謝您的任何幫助... Btw pep8對第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所有except子句,也不要pass捕獲的異常,這會使它靜默失敗。

我沒有寫, exceptdef trcrt(dst)try

如果這是您的完整代碼,則需要檢查兩件事:1)您是否混用了制表符和空格? 確保將所有選項卡都轉換為空格(我建議每個選項卡4個空格)以進行縮進。 一個好的IDE將為您完成此任務。

2) trcrt(dst)中的try:除塊外沒有其他匹配項。

順便說一句,PEP8還將告訴您,函數名稱應小寫:用check_internet代替checkInternet ,...

我會給您同樣的建議,我會給與我一起工作的每個人:開始使用IDE為您標記PEP8和其他錯誤,周圍有很多情況。 它有助於發現這些錯誤,並訓練您編寫清晰易懂的Python代碼,並且在幾年后(如果您在其中添加注釋)也可以重新理解。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM