Home > Software engineering >  Printing a domain that does not exist
Printing a domain that does not exist

Time:10-21

Is there a way where I can print out that the domain does not exist instead of giving me an error like this

ConnectionError: HTTPSConnectionPool(host='www.enroll.connect.web.co.id', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f4f558003d0>: Failed to establish a new connection: [Errno -2] Name or service not known'))

This is the code that i use to check whether the domain exist or not

request = requests.get('https://www.enroll.connect.web.co.id')
if request.status_code == 200:
    print('Web site exists')
else:
    print('Web site does not exist')

The logic should've been if the status is 200 then that means the website existed, but if it's not 200 it should've print out that the website does not exist.

Thx!

CodePudding user response:

Use Exception Handling: code:

import requests

try:
    request = requests.get('https://www.enroll.connect.web.co.id')
    if request.status_code == 200:
        print('Web site exists')
except :       
    print('Web site does not exist')

CodePudding user response:

In this case I prefer to use an exception and just bypass it

import requests
try:
    request = requests.get('https://www.akjsbdas,la.zxcvksam')
    if request.status_code == 200:
        print('Web site exists')
except:
    print('Web site does not exist')
  • Related