Receive a keyboard URL and drip it into Python

Asked

Viewed 161 times

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

  • No. I looked at the program working on https://repl.it/@acwoss/Nocturnalsoulfuldeclaration

  • 1

    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.

  • Okay, I’m checking the answers. Thank you

  • 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.

  • What I put after is not code. It is to illustrate what the program should show when executed.

  • Update the question with your code as well as the obtained result. If you have an error, post the error message.

  • 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

  • have to have the option to type the site to see if it exists

  • 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.

  • Sponse == 0 does not mean the site does not respond?

  • "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.

  • 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"

Show 8 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.