0

I'm coding a short code to check if the url is valid or not. However, when I'm checking for invalid URL it does not respond with any error-ish status code. It just don't run the code.

Can I get some help with this code below:

import requests
import string

# Welcome to IsItDown.py!
# Please write a URL or URLs you want to check. (separated by a comma)
#
# input()
#
# <check for valid URL>
#
# Do you want to start over? (y/n)
# if y:
# elif n: print(Okay. Bye!)
# else: That's not a valid answer

def url_check(urls):
  for url in urls:
    resp = requests.get(url)
    if resp.status_code == 200:
      print(f"{url} is up!")
    else:
      raise Exception(f"{url} is down!")


print("Welcome to IsItDown.py!")

urls = ['google.com .', '   na1v2e12312r.com ']

for index, item in enumerate(urls):
  item = item.strip(string.punctuation).strip()
  if "http" not in item:
    item = "http://" + item
  urls[index] = item

print(urls) 

try:
  url_check(urls)

except Exception as e:
  print(e)
7
  • It just don't run the code. Really? What output do you get? Is an exception thrown that you're catching? Maybe you could be catching the exception and handling it? Commented Dec 17, 2020 at 17:04
  • @Random Davis HTTPConnectionPool(host='na1v2e12312r.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7efc88f33b50>: Failed to establish a new connection: [Errno -2] Name or service not known')) Commented Dec 17, 2020 at 17:06
  • So why can't you catch NewConnectionError and handle that case? Commented Dec 17, 2020 at 17:06
  • I just want to know the status code for an invalid URL to print out "URL is down". But the code doesn't get further because of the error. Commented Dec 17, 2020 at 17:07
  • 1
    You only get a status code if you were able to connect to the url. But you weren't able to do that, so there is no status code. Commented Dec 17, 2020 at 17:11

1 Answer 1

1

Since this is the error you get when you try to access an invalid URL:

NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7efc88f33b50>: 
    Failed to establish a new connection: [Errno -2] Name or service not known')

That means that in order to catch the error and indicate that the URL is invalid, you'd have to use try and except like you're currently doing, except instead you'd catch that specific error:

try:
    resp = requests.get(url)
except NewConnectionError:
    print(f'Invalid URL: "{url}"')
    # at this point, you can continue the loop to the next URL if you want
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.