0

I want to get the response code from a web server, but sometime I get code 200 even if the page doesn't exist and I don't know how to deal with it.

I'm using this code:

def checking_url(link):
    try:  
            link = urllib.request.urlopen(link)
            response = link.code
    except urllib.error.HTTPError as e:
            response =  e.code
    return response

When I'm checking a website like this one: https://www.wykop.pl/notexistlinkkk/

It still returns code 200 even if the page doesn't exist. Is there any solution to deal with it?

I found solution, now gonna test it with more websites I had to use http.client.

2
  • But that url "exists" - it returns 200 OK even in browser... Try google.com/foobarblah Commented Jul 2, 2018 at 13:00
  • On this website http.client gave me good answer, if something gonna went wrong I will write, anyway thanks for help Commented Jul 2, 2018 at 17:06

2 Answers 2

1

You are getting response code 200, because the website you are checking has automatic redirection. In the URL you gave, even if you specify a non-existing page, it automatically redirects you to the home page, rather than returning a 404 status code. Your code works fine.

Sign up to request clarification or add additional context in comments.

Comments

0
import urllib2


thisCode = None 

try:
    i = urllib2.urlopen('http://www.google.com')
    thisCode = i.code
except urllib2.HTTPError, e:
    thisCode = e.code


print thisCode

1 Comment

Did you test your code on example which one I gave?

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.