Reputation: 35
So I'm trying to ping a website such as Microsoft or Google and print out the results, but when I run the script it just says: "IP address must be specified.". I've tried looking around to see why this is happening, but can't seem to narrow down a solution.
Here's my code:
import subprocess
print('Ping www.microsoft.com')
print()
address = 'www.microsoft.com'
subprocess.call(['ping', '-c 3', address])
Am I doing something wrong? If so, any help or explanation would be greatly appreciated!
Upvotes: 1
Views: 2133
Reputation: 13661
To show the output of the subprocess call you can use check_output
method: See this answer for details
import subprocess
def ping():
print('Ping www.microsoft.com')
print()
address = 'www.microsoft.com'
print(subprocess.check_output(['ping', '-c', '3', address]).decode())
ping()
Output:
Ping www.microsoft.com
PING e13678.dspb.akamaiedge.net (23.53.160.151) 56(84) bytes of data.
64 bytes from a23-53-160-151.deploy.static.akamaitechnologies.com (23.53.160.151): icmp_seq=1 ttl=55 time=83.6 ms
64 bytes from a23-53-160-151.deploy.static.akamaitechnologies.com (23.53.160.151): icmp_seq=2 ttl=55 time=83.5 ms
64 bytes from a23-53-160-151.deploy.static.akamaitechnologies.com (23.53.160.151): icmp_seq=3 ttl=55 time=83.7 ms
--- e13678.dspb.akamaiedge.net ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 83.567/83.648/83.732/0.067 ms
Upvotes: 1