3 examples of 'python requests check status code' in Python

Every line of 'python requests check status code' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
110@staticmethod
111def _raise_for_status(response):
112 """Raises stored :class:`HTTPError`, if one occurred."""
113 http_error_msg = ''
114 if isinstance(response.reason, bytes):
115 try:
116 reason = response.reason.decode('utf-8')
117 except UnicodeDecodeError:
118 reason = response.reason.decode('iso-8859-1')
119 else:
120 reason = response.reason
121
122 if 400 <= response.status_code < 500:
123 http_error_msg = u'%s Client Error: %s for url: %s (%s)' % (response.status_code, reason, response.url, response.content.decode('utf-8'))
124
125 elif 500 <= response.status_code < 600:
126 http_error_msg = u'%s Server Error: %s for url: %s (%s)' % (response.status_code, reason, response.url, response.content.decode('utf-8'))
127
128 if http_error_msg:
129 raise requests.exceptions.HTTPError(http_error_msg, response=response)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

181def _url_status(url):
182 parse_obj = urllib.parse.urlparse(url)
183
184 timer = 1
185 for i in range(6):
186 try:
187 connection = http.client.HTTPConnection(parse_obj.netloc)
188 connection.request('HEAD', parse_obj.path)
189 break
190 except Exception as e:
191 print(url, e, 'sleep', timer)
192 time.sleep(timer)
193 timer *= 2
194 else:
195 return e
196
197 response = connection.getresponse()
198 connection.close()
199 return response.status
803def check_http_code(self, status, expected_statuses):
804 if status not in expected_statuses:
805 raise RiakError('Expected status %s, received %s' %
806 (expected_statuses, status))

Related snippets