This will help if you have provided some data, for example the example URL you want to request. At the same time, it seems to me that, as a rule, you check if you had the correct URL after your POST request using the following algorithm based on redirect errors or HTTP 404:
if original_url == returned request url: correct url to a correctly made request else: wrong url and a wrongly made request
If so, then you can use the HTTP HEAD request (another type of HTTP request, for example GET, POST, etc.) in the Python requests library to get only the header, not the body page. Then you check the response code and the redirect URL (if any) to see if you have been requested to a valid URL.
For instance:
def attempt_url(url): '''Checks the url to see if it is valid, or returns a redirect or error. Returns True if valid, False otherwise.''' r = requests.head(url) if r.status_code == 200: return True elif r.status_code in (301, 302): if r.headers['location'] == url: return True else: return False elif r.status_code == 404: return False else: raise Exception, "A status code we haven't prepared for has arisen!"
If this is not exactly what you are looking for, additional information about your requirements will help. At the very least, it gives you a status code and headers without pulling out all the page data.
source share