如何使用python在本地网络中ping? [英] How to ping in local network using python?

查看:158
本文介绍了如何使用python在本地网络中ping?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两台计算机的网络192.168.1.0。 IP = 192.168.1.7的Raspberry Pi和IP = 192.168.1.4的笔记本电脑我想用python编写代码从raspberry pi ping笔记本电脑。我该怎么做呢?

我试着写这个,但它失败了。

I have a network 192.168.1.0 with two computer. Raspberry Pi with IP = 192.168.1.7 and laptop with IP = 192.168.1.4 I want to write code using python to ping laptop from raspberry pi. How can I make it?
I try to write this, but it failed.

import subprocess as sp
ip = "192.168.1.4"
status, result = sp.getstatusoutput("ping -c1 -w2" + ip)
if(status ==0):
    print("UP")
else:
    print("DOWN")



如何解决?


How can I fix it?

推荐答案

ping示例如下所示: http://stackoverflow.com/questions/316866/ping-a-网站IN- python [ ^ ]。



您只需要用您的IP地址字符串替换域名,例如192.168.1.7192.168.1.4。



-SA
The ping example is shown here: http://stackoverflow.com/questions/316866/ping-a-site-in-python[^].

You only need to replace domain names with your IP address strings, such as "192.168.1.7" "192.168.1.4".

—SA


这篇关于如何使用python在本地网络中ping?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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