使用Raspberry Pi检查Internet连接的Python

使用Raspberry Pi检查Internet连接的Python,python,raspberry-pi,internet-connection,Python,Raspberry Pi,Internet Connection,我正在用一些覆盆子皮做一个安全摄像系统。每个摄像头系统都有一个指示灯,显示存在互联网连接。为此,我使用了在中找到的代码,并将其修改为 #! /usr/bin/python import socket import fcntl import struct import RPi.GPIO as GPIO import time pinNum = 8 GPIO.setmode(GPIO.BCM) #numbering scheme that corresponds to breakout board

我正在用一些覆盆子皮做一个安全摄像系统。每个摄像头系统都有一个指示灯,显示存在互联网连接。为此,我使用了在中找到的代码,并将其修改为

#! /usr/bin/python

import socket
import fcntl
import struct
import RPi.GPIO as GPIO
import time
pinNum = 8
GPIO.setmode(GPIO.BCM) #numbering scheme that corresponds to breakout board and pin layout
GPIO.setup(pinNum,GPIO.OUT) #replace pinNum with whatever pin you used, this sets up that pin as an output
#set LED to flash forever

def check_connection():

    ifaces = ['eth0','wlan0']
    connected = []

    i = 0
    for ifname in ifaces:

        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        try:
            socket.inet_ntoa(fcntl.ioctl(
                    s.fileno(),
                    0x8915,  # SIOCGIFADDR
                    struct.pack('256s', ifname[:15])
            )[20:24])
            connected.append(ifname)
            print "%s is connected" % ifname 
            while True:
                GPIO.output(pinNum,GPIO.HIGH)
                time.sleep(0.5)
                GPIO.output(pinNum,GPIO.LOW)
                time.sleep(0.5)
                GPIO.output(pinNum,GPIO.LOW)
                time.sleep(2.0)

        except:
            print "%s is not connected" % ifname

        i += 1

    return connected
已连接\u ifaces=检查\u连接

但是,当我通过Pi运行代码时,错误显示:

pi@raspberrypi ~/Desktop $     while True:
>               ^
> TabError: inconsistent use of tabs and spaces in indentation
> ^C
有人知道这个问题吗?抱歉,如果这是一个基本的,我是Python编程新手。简言之,我的希望是,当出现internet连接时,针脚8上的指示灯将亮起


谢谢

Python对缩进非常挑剔,因为它使用缩进对代码块进行分组。代码似乎到处都在使用4个空格或制表符进行缩进,除了第30行到第35行,其中只有两个空格。确保缩进在整个代码中是一致的,它将解决您的问题

更新:对于您的新错误,请确保您使用的是空格还是制表符。检查代码的每一行,或者每个制表符使用4个空格,或者确保它使用制表符来达到正确的缩进

有关固定缩进的详细信息: 另外,请查看以下内容:


在修复此代码时,请尝试通过谷歌搜索有关缩进和Python的更多信息。

谢谢@Tyler Ferraro-我更新了上面的问题,您的解决方案已经奏效,现在显示了一个不同的错误。看起来您正在混合缩进的制表符和空格。不要使用制表符或更好的空格进行缩进。尝试使用python-tt运行代码“python-tt”很好,向我展示了您所说的“TabError:缩进中制表符和空格的使用不一致”。我不知道代码有什么问题。它现在一直在使用标签。我已经更新了上面的代码。我得到的错误是“while true;”我看不出代码有什么问题——不要混合使用制表符和空格进行缩进:这是Python语法的一部分。我看到你问题中的代码使用空格和制表符来表示indentation@MediaStreet你能编辑你的答案来显示你提到的新错误吗?嗨@Winny很抱歉耽搁了。如果您转到,您可以查看Py文件和我得到的错误。提前谢谢!