0

I want to check if a website exists. I use the request module to make a get request and check the status code after the request was made.

    def check_website_exist(self, url):
        result = True
        request = requests.get("http://"+url)
        print(request.status_code)
        if request.status_code == 200:
            output.info("website found!")
            return result
        else:
            output.error("website not found!")
            result = False
            return result

When I make a request for the site 'www.isdfugpdohsiughsdopiughdsfoiguf.com' I get the status code 200, even though the site doesn't exists. Why do I get a 200 status code, but the website doesn't exist?

LLJ97
  • 500
  • 3
  • 6
  • 19
  • 1
    Can't reproduce: ```requests.get('http://www.isdfugpdohsiughsdopiughdsfoiguf.com')``` raises ```ConnectionError```. Try opening this url in your browser. Also you can examine ```request.content```. – Eugene Primako Jun 28 '18 at 16:09
  • When I open the url in my browser it doesn't find the site. But I get the same result with urllib2.urlopen. I always get, that the site was found – LLJ97 Jun 28 '18 at 16:12

1 Answers1

1

Here is an example of how to do it. you are not catching the possible errors.

import requests
from requests.exceptions import ConnectionError
try:
    url = "http://www.isdfugpdohsiughsdopiughdsfoiguf.com"
    request_url = requests.get(url)
    print(request_url.status_code)
except ConnectionError:
    print("No exist")
Eddwin Paz
  • 2,842
  • 4
  • 28
  • 48
  • Just tried it. I'm still getting a *exists* with the url *'www.isdfugpdohsiughsdopiughdsfoiguf.com* – LLJ97 Jun 28 '18 at 16:15
  • @LLJ97 are you getting it with any non-existent hostname or just that one? – khachik Jun 28 '18 at 16:20
  • @khachik I'm getting it with every non-existent hostname. Just tried it in an online python compiler and I got an error. – LLJ97 Jun 28 '18 at 16:23
  • @LLJ97 I fixed the code and edited the answer as well tried it and it worked. Mark it as +1 if it solved your problem. – Eddwin Paz Jun 28 '18 at 20:28
  • @eddwinpaz sorry, it didn't work. Must be a problem with my pc or my network. But you're answer is still a good answer, so i'm gonna mark it anyways – LLJ97 Jun 28 '18 at 20:33
  • @LLJ97 Please check you python version this is python3 maybe you are using python2.7 – Eddwin Paz Jul 10 '18 at 14:15