Use "ping" on a Python site

Asked

Viewed 1,037 times

5

In Windows CMD it is possible to use the command ping to take the IP of certain websites, for example:

ping www.facebook.com.br

Is there any way to do something similar using Python 3.4?

  • If you want an implementation of the python PING command using socket only, you can find it here : http://www.g-loaded.eu/2009/10/30/python-ping/ Unfortunately, for the purpose of this website, it is in English. I used it in the past and it worked for me.

2 answers

5

On Linux, already inside the shell from Python 3.2.3, simply enough: :

import subprocess

a=subprocess.Popen("ping -c4 www.boadica.com.br", shell=True)

Must work also in Windows

  • 1

    I just don’t know why I wear it shell=True - you can avoid invoking the shell so check_output(["ping", "-c4", endereco]) thus less a process is executed, spending less resources and time.

4


I assume it’s windows, right? That’s fine, I guess, I can’t test it. I took from here.

from subprocess import check_output
check_output("ping www.facebook.com", shell=True)
  • Complementing that it would be possible to implement ping (ICMP) within the program itself, without invoking an external shell, but such a program needs to have root or administrator permissions, so it ends up being more interesting to use the same pre-existing ping.

  • I just don’t know why I wear it shell=True - you can avoid invoking the shell so check_output(["ping", endereco]) thus less a process is executed, spending less resources and time.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.