在 python 中对我的服务器执行 ping 操作时,我不断收到误报

I keep getting false negatives when pinging my server in python

我制作了一些软件,通​​过时不时地 ping 服务器来告诉我服务器何时关闭,然后通过 att 电子邮件向我发送短信,转发到我的 phone 号码。但问题是当它工作正常时我不断收到误报和警报,我不知道如何修复它这是我的代码。这也是最好的方法吗?

import RPi.GPIO as GPIO

def turnOn(GPIOPin):
    print("on")
    GPIO.setmode(GPIO.BCM)

    GPIO.setup(GPIOPin, GPIO.OUT)
    GPIO.output(GPIOPin, GPIO.HIGH)

def turnOff(GPIOPin):
    print("off")
    GPIO.setmode(GPIO.BCM)

    GPIO.setup(GPIOPin, GPIO.OUT)
    GPIO.output(GPIOPin, GPIO.LOW)
    GPIO.cleanup()


try:
    turnOn(26)

    import platform    # For getting the operating system name
    import subprocess  # For executing a shell command

    '''
    Green LED: GPIO.17
    Red LED: GPIO.04
    White LED: GPIO.26
    '''
    import smtplib
    carriers = {
        'att':    '@mms.att.net',
        'tmobile':' @tmomail.net',
        'verizon':  '@vtext.com',
        'sprint':   '@page.nextel.com'
    }

    


    def send(message):
            # Replace the number with your own, or consider using an argument\dict for multiple people.
        to_number = 'phonenum{}'.format(carriers['att'])
        auth = ('', '')

        # Establish a secure session with gmail's outgoing SMTP server using your gmail account
        server = smtplib.SMTP( "mail.anonyomail.com", 587 )
        server.starttls()
        server.login(auth[0], auth[1])

        # Send text message through SMS gateway of destination number
        server.sendmail( auth[0], to_number, message)


    def ping(host):
        """
        Returns True if host (str) responds to a ping request.
        Remember that a host may not respond to a ping (ICMP) request even if the host name is valid.
        """

        # Option for the number of packets as a function of
        param = '-n' if platform.system().lower()=='windows' else '-c'

        # Building the command. Ex: "ping -c 1 google.com"
        command = ['ping', param, '1', host]

        return subprocess.call(command) == 0

    import os
    from twilio.rest import Client

    if ping("anonyomail.com") == True:
        send("Anonyomail RasperryPI server is running")
        print("Sent text message telling CEO app is working")
        turnOn(17)

    import time
    i = 0
    e = 0
    while True:
        time.sleep(60)
        if ping("anonyomail.com") == False:
            if i > 0:
                send("Anonyomail RasperryPI server is down!!!!")
            else:
                i += 1
            print("Sent text message telling CEO app is not working")
            turnOn(4)
        elif ping("anonyomail.com") == True:
            i = 0
            e = 0
            continue
        else:
            if e > 0:
                send("Something is wrong with the rasperrypi or the software monioring the status")
            else:
                e += 1
            print("Nah fam")
            turnOn(4)
finally:
    turnOff(26)
    turnOff(4)
    turnOff(17)

我就是这样解决问题的

import RPi.GPIO as GPIO
import time

time.sleep(30)

print("Server Status Monitor Version 1.1.1")
def turnOn(GPIOPin):
    print("on")
    GPIO.setmode(GPIO.BCM)

    GPIO.setup(GPIOPin, GPIO.OUT)
    GPIO.output(GPIOPin, GPIO.HIGH)

def turnOff(GPIOPin):
    print("off")
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(GPIOPin, GPIO.OUT)
    GPIO.output(GPIOPin, GPIO.LOW)
    GPIO.cleanup()


try:
    turnOn(26)

    import platform    # For getting the operating system name
    import subprocess  # For executing a shell command

    '''
    Green LED: GPIO.17
    Red LED: GPIO.04
    White LED: GPIO.26
    '''
    import smtplib
    carriers = {
        'att':    '@mms.att.net',
        'tmobile':' @tmomail.net',
        'verizon':  '@vtext.com',
        'sprint':   '@page.nextel.com'
    }

    


    def send(message):
            # Replace the number with your own, or consider using an argument\dict for multiple people.
        to_number = ''.format(carriers['att'])
        auth = ('', '')

        # Establish a secure session with gmail's outgoing SMTP server using your gmail account
        server = smtplib.SMTP( "mail.anonyomail.com", 587 )
        server.starttls()
        server.login(auth[0], auth[1])

        # Send text message through SMS gateway of destination number
        server.sendmail( auth[0], to_number, message)

    def sendEmail(message):
         # Replace the number with your own, or consider using an argument\dict for multiple people.
        to_number = ''
        auth = ('', '')

        # Establish a secure session with gmail's outgoing SMTP server using your gmail account
        server = smtplib.SMTP( "mail.anonyomail.com", 587 )
        server.starttls()
        server.login(auth[0], auth[1])

        # Send text message through SMS gateway of destination number
        server.sendmail( auth[0], to_number, message)

    def ping(host):
        """
        Returns True if host (str) responds to a ping request.
        Remember that a host may not respond to a ping (ICMP) request even if the host name is valid.
        """

        # Option for the number of packets as a function of
        param = '-n' if platform.system().lower()=='windows' else '-c'

        # Building the command. Ex: "ping -c 1 google.com"
        command = ['ping', param, '1', host]

        return subprocess.call(command) == 0

    import os

    status = ping("192.168.1.199")

    if status == True:
        send("Anonyomail RasperryPI server is running")
        sendEmail("Anonyomail RasperryPI server is running")
        print("Sent text message telling CEO app is working")
        turnOn(17)

    import time
    i = 0
    e = 0
    while True:
        time.sleep(60)
        status = ping("192.168.1.199")
        if status == False:
            if i > 0:
                send("Anonyomail RasperryPI server is down!!!!")
                sendEmail("Anonyomail RasperryPI server is down!!!!")               
            else:
                i += 1
            print("Sent text message telling CEO app is not working")
            turnOff(17)
            turnOn(4)
            turnOn(26)
        elif status == True:
            i = 0
            e = 0
            turnOff(4)
            turnOn(17)
            turnOn(26)
            continue
        else:
            if e > 0:
                send("Something is wrong with the rasperrypi or the software monioring the status")
                sendEmail("Something is wrong with the rasperrypi or the software monioring the status")               
            else:
                e += 1
            print("Nah fam")
            turnOn(4)
            turnOn(26)
            turnOff(17)

finally:
    turnOff(26)
    turnOff(4)
    turnOff(17)