A simple way to “ping” a server in Python is by sending an HTTP request and checking the response. While this is not an ICMP ping, it’s often more practical for verifying that a web service is reachable.
For ICMP ping in Python we are going to use package: Python package ping3 available in Github
The httpbin service is ideal for testing HTTP requests.
Basic HTTP Request Example
import requests
url = "https://httpbin.org/get"
try:
response = requests.get(url, timeout=3)
if response.status_code == 200:
print("Server is reachable")
else:
print("Server responded with status:", response.status_code)
except requests.RequestException:
print("Server is not reachable")
Parallel Ping and Requests in Python
The code below will run multiple requests in parallel. The input is simple dataframe:
| rank | site | netloc | |
|---|---|---|---|
| 0 | 1 | https://google.com | google.com |
| 1 | 2 | https://yahoo.com | yahoo.com |
The code:
import pandas as pd
import multiprocessing as mp
from ping3 import ping
import requests
import json
df = pd.DataFrame({'rank': [1,2], 'site': ['https://google.com', 'https://yahoo.com'], 'netloc': ['google.com', 'yahoo.com'] })
pool = mp.Pool(processes=mp.cpu_count())
def func( arg ):
idx,row = arg
site = row['site']
netloc = row['netloc']
out1 = ping(netloc)
out2 = json.loads(requests.get('https://httpbin.org/anything').text)
ip_resolved = out2.get('origin')
output = {'light': out1, 'full': out2, 'ip_resolved': ip_resolved, 'netloc': netloc}
return output
with mp.Pool(mp.cpu_count()) as pool:
langs = pool.map( func, [(idx,row) for idx,row in df[['site', 'netloc']].iterrows()])
df_out = pd.DataFrame(langs)
df_out
print(df_out.to_markdown())
df_out.to_csv('output.csv', index=False)
the ouput file will be something like:
| light | full | ip_resolved | netloc | |
|---|---|---|---|---|
| 0 | 0.00225139 | {'args': {}, 'data': '', 'files': {}, 'form': {}, 'headers': {'Accept': '/', 'Accept-Encoding': 'gzip, deflate, br, zstd', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.32.4', 'X-Amzn-Trace-Id': 'Root=1-6973df8d'}, 'json': None, 'method': 'GET', 'origin': '192.168.0.1', 'url': 'https://httpbin.org/anything'} | 192.168.0.1 | google.com |
| 1 | 0.1147 | {'args': {}, 'data': '', 'files': {}, 'form': {}, 'headers': {'Accept': '/', 'Accept-Encoding': 'gzip, deflate, br, zstd', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.32.4', 'X-Amzn-Trace-Id': 'Root=1-6973df8d'}, 'json': None, 'method': 'GET', 'origin': '192.168.0.1', 'url': 'https://httpbin.org/anything'} | 192.168.0.1 | yahoo.com |
Run Ping command on Linux
You may get error:
PermissionError: [Errno 13] Permission denied
if you run command Python package ping3 in Linux.
Solution 1
Follow the instruction in this page: TROUBLESHOOTING.md
Solution 2
Run the script as sudo:
sudo /home/user/anaconda3/bin/python test.py
You may need to locate your python installation by:
$ which python
result
/home/user/anaconda3/bin/python
Check this article: How to Run a Python Script as Root - Linux + Windows
Ping with os.system
We can also do ping by using the os.system:
import os
hostname = "google.com"
response = os.system(f"ping {hostname}")
response
Result is:
PING google.com (142.251.142.110) 56(84) bytes of data.
64 bytes from lcsofa-an-in-f14.1e100.net (142.251.142.110): icmp_seq=1 ttl=119 time=2.05 ms
64 bytes from lcsofa-an-in-f14.1e100.net (142.251.142.110): icmp_seq=2 ttl=119 time=1.88 ms
64 bytes from lcsofa-an-in-f14.1e100.net (142.251.142.110): icmp_seq=3 ttl=119 time=2.33 ms
64 bytes from lcsofa-an-in-f14.1e100.net (142.251.142.110): icmp_seq=4 ttl=119 time=1.99 ms
64 bytes from lcsofa-an-in-f14.1e100.net (142.251.142.110): icmp_seq=5 ttl=119 time=1.88 ms
--- google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4005ms
rtt min/avg/max/mdev = 1.879/2.025/2.326/0.163 ms
2
How to ICMP ping in Python?
ICMP ping is a network diagnostic tool that uses the Internet Control Message Protocol (ICMP) to test the reachability of a host on a network. It sends an ICMP Echo Request to the target device and waits for an ICMP Echo Reply, helping to determine if the device is reachable and measure the round-trip time for the packets.
Why Use HTTP Ping?
- Works even when ICMP ping is blocked
- Confirms both network and service availability
- Useful for monitoring APIs and web services
This approach is commonly used in health checks, monitoring scripts, and automation workflows where HTTP availability matters more than raw network reachability.