We can make use of the subprocess module to call a subprocess to perform a ping command.
Example 1: Ping an IP Address
>>> import subprocess
>>>
>>> def ping_ip(ip_address, count=5):
... try:
... output = subprocess.check_output(['ping', '-c', str(count), ip_address])
... return output.decode('utf-8')
... except subprocess.CalledProcessError as e:
... return e.output.decode('utf-8')
...
>>> server_ip_address = '8.8.8.8'
>>>
>>> ping_output = ping_ip(server_ip_address)
print(ping_output)
>>>
>>> print(ping_output)
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: icmp_seq=0 ttl=118 time=7.364 ms
64 bytes from 8.8.8.8: icmp_seq=1 ttl=118 time=9.279 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=118 time=9.535 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=118 time=8.679 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=118 time=5.167 ms
Example 2: Pinging using Domain
import subprocess
def ping_domain(domain_address, count=5):
try:
output = subprocess.check_output(['ping', '-c', str(count), domain_address])
return output.decode('utf-8')
except subprocess.CalledProcessError as e:
return e.output.decode('utf-8')
server_domain_address = 'code2care.org'
ping_output = ping_domain(server_domain_address)
print(ping_output)
Output:
PING code2care.org (172.67.164.66): 56 data bytes
64 bytes from 172.67.164.66: icmp_seq=0 ttl=58 time=11.716 ms
64 bytes from 172.67.164.66: icmp_seq=1 ttl=58 time=6.696 ms
64 bytes from 172.67.164.66: icmp_seq=2 ttl=58 time=5.827 ms
64 bytes from 172.67.164.66: icmp_seq=3 ttl=58 time=6.884 ms
64 bytes from 172.67.164.66: icmp_seq=4 ttl=58 time=11.144 ms
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Python,
- How to convert int to ASCII in Python
- How to make use of SQLite Module in Python?
- Split a String into Sub-string and Parse in Python
- Python: Pandas Rename Columns with List Example
- How to run Python file from Mac Terminal
- How to Exit a Loop in Python Code
- Python: How to Plot a Histogram using Matplotlib and data as list
- MD5 Hashing in Python
- Jupyter: Safari Cant Connect to the Server localhost:8888/tree
- Fix: AttributeError: str object has no attribute decode. Did you mean: encode?[Python]
- How to Read a binary File with Python
- How to add two float numbers in Python
- Python: How to install YAML Package
- Python: How to Save Image from URL
- What is Markdown in Jupyter Notebook with Examples
- How to change the Python Default version
- 33: Python Program to send an email vid GMail
- How to comment code in Python
- How to Fix AttributeError in Python
- Fix: error: Jupyter command `jupyter-nbconvert` not found [VSCode]
- How to comment out a block of code in Python
- List of All 35 Reserved Keywords in Python Programming Language 3.11
- Import Other Python Files Examples
- Python: How to add Progress Bar in Console with Examples
- 3 Ways to convert bytes to String in Python
More Posts:
- Tailwind CSS Hello World Example - CSS
- Dynamically Obtaining Browser Screen Width and Height with jQuery [Updated 2023] - jQuery
- Fix- Microsoft Word Pages Appear Black - Microsoft
- Bash For Loop Example - Bash
- Setting Java_Home Environment variable on Windows Operating System - Java
- How to Add Edit with Notepad++ Option to Windows 10 or 11 Right Click Menu Options - Windows-11
- This Toast was not created with Toast.makeText() : Android RuntimeException - Android
- sudo is not recognized as an internal or external command - Windows