Find a typos error when my code ping an IP Adresse

My code pings an IP address or a domain name, but I would like a message in case of typos: if an address does not exist.
Here is my code :

import subprocess
import platform

def ping(host):
    param = '-n' if platform.system().lower() == 'windows' else '-c'
    command = ['ping', param, '4', host]
    result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    if result.returncode == 0:
        print(f"Ping successful:\n{result.stdout}\n La cible est pingable et joignable.")
    if result.returncode == 1:
        print(f"Ping failed:\n{result.stderr}\n Hôte injoignable.\n Vérifier l'adresse IP ou le nom de domaine.")

ping(str(input("Nom de domaine ou adress ip :")))

Hello,

just an fyi for this line:

ping(str(input("Nom de domaine ou adress ip :")))

you do not need to wrap it with the str keyword as input is string by default. You can just write it as:

ping(input("Nom de domaine ou adress ip :"))

Regarding your question, if for some reason an error (an exception) is generated when using an IP address that does not exist, you can wrap it with a try / except pair.

Using the classical divide by zero as an example:

a = 10
b = 0

def divi(x, y):

    return x / y

# Try this one first to generate exception 
# then comment out when testing the one below
print(f'{a} divided by {b} is: ', divi(a, b)) 

# This time using the try / except pair to 'catch' exception:
try:
    print(f'{a} divided by {b} is: ', divi(a, b))

except ZeroDivisionError:
    print("\nCan't divide by zero.  Try again.")

Do you mean something like this?

if result.returncode == 2:
    if 'Name or service not known' in result.stderr: # Might be a different string on Windows
        print("The hostname or IP address you entered does not exist.")
   else:
        print(f"Ping failed:\n{result.stderr}")