Python3 ping错误... [英] Python3 ping error...

查看:151
本文介绍了Python3 ping错误...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Raspberry PI 3B ping一个地址,尝试从命令行ping 192.168.0.1,这似乎是响应。现在我需要更快地工作,所以我尝试了下面的Python(我远非专家)从而它似乎没有工作(惊喜!)我试图让它工作,添加'w'打开错误如果计数文件不存在,则在桌面上创建ErrorCount.log会导致脚本跛行而不是运行...所以我没有正确创建文本文件或者我是否在思考(或ponging)错误?



我尝试了什么:



  import  socket 

max_error_count = 10
error_count = 1


def increase_error_count():
快速处理虚假端口未打开错误
open(' ErrorCount.log' as f:
f:
error_count =行
error_count = int(error_count)
print 错误计数器: + str(error_count))
file = open(' ErrorCount.log'' w'
file.write(str(error_count + 1 ))
file.close()
if error_count == max_error_count:
发送电子邮件,推卸,松弛或做任何其他花哨的东西
print 发出通知
重置错误计数器,使其不会充满通知
file = open(< span class =code-string>' ErrorCount.log'' w'
file.write(' 0'
file.close()

sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.settimeout( 2
result = sock.connect_ex((' 192.168.0.1' 80 ))
如果 result == 0
print 端口打开
else
print 端口未打开
increase_error_count()

解决方案

进一步谷歌我发现了这个

  def  pingComputer():

import os
hostname = input( 输入ip地址:
response = os.system( ping -c 1 + str(hostname))
if response == 0
print hostname,' 已启动!'
else
print hostname,' 已关闭!



这会执行Ping吗?

哦是的它会!


I am trying to ping an address using a Raspberry PI 3B, tried ping 192.168.0.1 from the command line, that appeared to respond. Now I need to get to work faster so I tried the below Python (I am far from expert) from SO it did not appear to work (surprise!) I have tried to get it to work, adding a 'w' to open error count file if it does not exist, creating ErrorCount.log on the desktop causes the script to limp rather than run... So am not creating the text file properly or am I pining (or ponging) wrong?

What I have tried:

import socket

max_error_count = 10
error_count = 1


def increase_error_count():
    # Quick hack to handle false Port not open errors
    with open('ErrorCount.log') as f:
        for line in f:
            error_count = line
    error_count = int(error_count)
    print ("Error counter: " + str(error_count))
    file = open('ErrorCount.log', 'w')
    file.write(str(error_count + 1))
    file.close()
    if error_count == max_error_count:
        # Send email, pushover, slack or do any other fancy stuff
        print ("Sending out notification")
        # Reset error counter so it won't flood you with notifications
        file = open('ErrorCount.log', 'w')
        file.write('0')
        file.close()

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(2) 
result = sock.connect_ex(('192.168.0.1',80))
if result == 0:
        print ("Port is open")
else:
        print ("Port is not open")
        increase_error_count()

解决方案

Having further google I have found this

def pingComputer():

  import os
  hostname = input("Enter the ip address: ")
  response = os.system("ping -c 1 " + str(hostname))
  if response == 0:
    print hostname, 'is up!'
  else:
    print hostname, 'is down!


Will this perform the Ping?
Oh Yes it Will!!


这篇关于Python3 ping错误...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆