Alexander Eser
Alexander Eser

Reputation: 121

Python URL Gets Method returns status 200, even though it is 404

I am using the following code to return the status of an URL

import requests
answer = requests.get('http://www.website.com')
answer.status_code
>>>200

Which gives me back 200.

However, the website should return 404.

answer.content
>>>b'<html><head>\r\n<title>404 Not Found</title>\r\n</head><body>\r\n<h1>Not   Found</h1>\r\n<p>The requested URL index.php was not found on this server.</p>\r\n<hr>\r\n<address>Apache/2.2.22 (Linux) Server at Port <small onclick="document.getElementById(\'login\').style.display = \'block\';">80</small></address>\r\n</body></html><div id="login" style="display:none;"><pre align=center><form method=post>Password: <input type=password name=pass><input type=submit value=\'>>\'></form></pre></div>'

Can someone tell me where the discrepancy stems from and how I can fix this to get answer.status_code = 404 as result instead of 200? I have no direct access to the server, but I can ask the administrator.

Thank you!

Upvotes: 5

Views: 2519

Answers (1)

宏杰李
宏杰李

Reputation: 12168

Requests Doc

Redirection and History By default Requests will perform location redirection for all verbs except HEAD.

We can use the history property of the Response object to track redirection.

The Response.history list contains the Response objects that were created in order to complete the request. The list is sorted from the oldest to the most recent response.

For example, GitHub redirects all HTTP requests to HTTPS:

>>> r = requests.get('http://github.com')

>>> r.url
'https://github.com/'

>>> r.status_code
200

>>> r.history
[<Response [301]>]

If you're using GET, OPTIONS, POST, PUT, PATCH or DELETE, you can disable redirection handling with the allow_redirects parameter:

>>> r = requests.get('http://github.com', allow_redirects=False)

>>> r.status_code
301

>>> r.history
[]

If you're using HEAD, you can enable redirection as well:

>>> r = requests.head('http://github.com', allow_redirects=True)

>>> r.url
'https://github.com/'

>>> r.history
[<Response [301]>]

Upvotes: 2

Related Questions