0
- Receives a URL typed via keyboard
- Ping the respective URL
- Returns the ip (if any) of the entered URL
Ex.: Type a URL: www.google.com
Pinging www.google.com [172.217.162.164] with 32 bytes of data: Reply from 172.217.162.164: bytes=32 time=64ms TTL=54
Reply from 172.217.162.164: bytes=32 time=56ms TTL=54
Reply from 172.217.162.164: bytes=32 time=55ms TTL=54
Reply from 172.217.162.164: bytes=32 time=57ms TTL=54
Ping statistics for 172.217.162.164:
Packets: Sent = 4, Received = 4, Lost = 0 (0% Oss),
Approximate round trip times in Milli-Seconds:
Minimum = 55ms, Maximum = 64ms, Average = 58ms
www.google.com | 172.217.162.164
Do you wish to continue? [S]im [N]ao
Now whenever I run the program it gives the answer "up" regardless of what is typed in the site field, with this occurs the following message:
Enter the site: www.ggghhh.com.br www.ggghhh.com.br is up! Traceback (Most recent call last): File "C: Users cwo Downloads python TRABALHO.py", line 22, in ip = socket.gethostbyname(hostname) socket.gaierror: [Errno 11004] getaddrinfo failed
import os
import subprocess
import socket
list_site = []
list_ip = []
hostname = ""
continuar = ""
while continuar != "nao":
hostname = str(input("Digite o site: "))
list_site.append(hostname)
response = os.system("ping -c4 " + hostname)
if response == 0:
print (hostname, 'is down!')
ip = 0
list_ip.append(0)
else:
print (hostname, 'is up!')
ip = socket.gethostbyname(hostname)
list_ip.append(ip)
continuar = str(input("Deseja digitar mais algum site? "))
for i in range(len(list_site)):
print(list_site[i], list_ip[i])
See if this helps you: https://answall.com/a/290379/5878
– Woss
No. I looked at the program working on https://repl.it/@acwoss/Nocturnalsoulfuldeclaration
– Homero
If none of the indicated questions help you, look for [Dit] your better describing the problem. If you really are different, we can reopen the question without any problems.
– Woss
Okay, I’m checking the answers. Thank you
– Homero
It was not very clear what the purpose of your editing was. You seem to have only described it more fully, but the problem remains the same. Did you manage to ping? Ask the question what code you did.
– Woss
What I put after is not code. It is to illustrate what the program should show when executed.
– Homero
Update the question with your code as well as the obtained result. If you have an error, post the error message.
– Woss
Revise your conditions. I don’t understand why
os.system
returns 0 the site is down. I also recommend testing with a domain that exists or handles exceptions that are released by the functions used when the domain does not exist. I tested it here and it worked perfectly: https://repl.it/@acwoss/Overduetruthfulentropy– Woss
have to have the option to type the site to see if it exists
– Homero
That’s why I said you should review the conditions you used, because logic itself works, it’s just not checking whether or not it exists in the right way.
– Woss
Sponse == 0 does not mean the site does not respond?
– Homero
"On Windows, the Return value is that returned by the system shell after running command. [...] which Returns the Exit status of the command run." That is, 0 means that the command ran error-free.
– Woss
even if I reverse the order and place if Response == 0: print (hostname, 'is up!') ip = socket.gethostbyname(hostname) list_ip.append(ip) Else: print (hostname, 'is down!') ip = 0 list_ip.append(ip) continue = str(input("Do you want to type any more site? ") , so it always shows "down"
– Homero