How get status code if an error occurred python requests? - python

I want to get response status code if an error occurred
try:
r = requests.post('site.com')
except (requests.RequestException) as e:
print(r.status_code)
i can't do that
I also know about
r.raise_for_status()
But Exception raised before request so i can't do that

Related

Python requests keeps returning 401 client error

I have some python test code that is requesting web pages from a embedded controller. For general .htm pages it works but when I request a .txt file stored on it I get a
"401 Client Error: Unauthorized for url: http://192.168.61.30/fs/work/sys/crashdump/log0.txt"
HTTP error
The code is as follows:-
for url in ['http://192.168.61.30/get_version.htm']:
try:
cookies = {'Auth': access_token}
response = requests.get(url, cookies=cookies)
# If the response was successful, no Exception will be raised
response.raise_for_status()
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}') # Python 3.6
except Exception as err:
print(f'Other error occurred: {err}') # Python 3.6
else:
print("*****Access obtained******")
print(response.content);
print('Success!')
for url in ['http://192.168.61.30/fs/work/sys/crashdump/log0.txt']:
try:
cookies = {'Auth': access_token}
response = requests.get(url, cookies=cookies)
# If the response was successful, no Exception will be raised
response.raise_for_status()
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}') # Python 3.6
except Exception as err:
print(f'Other error occurred: {err}') # Python 3.6
else:
print("*****Access obtained******")
print(response.content);
print('Success!')
The 1st one works fine, however the 2nd one always fails with 401 client error and as far as I can work out never gets as far as the web server. Looking at the returned headers I don't see any authorisation requests. I have also disabled the proxies.
If I send the same request via a web browser I get no problems
The headers response is
{'Server': 'HPCi Controller Web server', 'Connection': 'close', 'X-Frame-Options': 'SAMEORIGIN', 'Content-Type': 'text/html'}
I have tried adding WWW-Authentication: Basic to the headers, but no success

python requests library .json() + django 500

When I use the requests library with django and I get a 500 error back. response.json() gives me this error:
response = requests.post(....)
print("-----------------------block found!!!-----------------------")
print(response.status_code)
print(response.json())
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char
0)
Is there a way to represent a django 500 response with the requests library in a readable manner?
Assuming you get an HTTP 500 response, You would definitely receive an empty source, Which prevents the .json() function from taking place.
Why not write an exception clause to handle the exceptions, like below:
try:
response = requests.post(....)
print("-----------------------block found!!!-----------------------")
print(response.status_code)
print(response.json())
except HTTPError as e:
print('Error has occurred: ', e.response.status_code)

Sending the failed JSON objects into a file when the post requests fail

I have a JSON post request that I am sending to an api for each row in a dataframe. I want to throw the unsuccessful JSON objects into another text file so I can re-process them once the entire dataframe has been looped over completely.
This is the sample code that I have currently for checking different kinds of exceptions:
for i in df.index:
print "This is a JSON object."
payload='''{"individualInfo":[%s]}''' %(df.loc[i].to_json(orient='columns'))
print payload
try:
r = requests.post(api_url, data=payload, timeout=(0.2,20))
print r.json()
print r.raise_for_status()
except requests.exceptions.HTTPError as errh:
print "HTTP Error: %s" %errh
except requests.exceptions.ConnectionError as errc:
print "Error Connecting: %s" %errc
except requests.exceptions.Timeout as errt:
print "Timeout error: %s" %errt
I want each payload to be thrown into 2 different files based on whether it got posted successfully or not.

How to use exceptions for different cases with python requests

I have this code
try:
response = requests.post(url, data=json.dumps(payload))
except (ConnectionError, HTTPError):
msg = "Connection problem"
raise Exception(msg)
Now i want the following
if status_code == 401
login() and then try request again
if status_code == 400
then send respose as normal
if status_code == 500
Then server problem , try the request again and if not successful raise EXception
Now these are status codes , i donn't know how can i mix status codes with exceptions. I also don't know what codes will be covered under HttpError
requests has a call called raise_for_status available in your request object which will raise an HTTPError exception if any code is returned in the 400 to 500 range inclusive.
Documentation for raise_for_status is here
So, what you can do, is after you make your call:
response = requests.post(url, data=json.dumps(payload))
You make a call for raise_for_status as
response.raise_for_status()
Now, you are already catching this exception, which is great, so all you have to do is check to see which status code you have in your error. This is available to you in two ways. You can get it from your exception object, or from the request object. Here is the example for this:
from requests import get
from requests.exceptions import HTTPError
try:
r = get('http://google.com/asdf')
r.raise_for_status()
except HTTPError as e:
# Get your code from the exception object like this
print(e.response.status_code)
# Or you can get the code which will be available from r.status_code
print(r.status_code)
So, with the above in mind, you can now use the status codes in your conditional statements
https://docs.python.org/2/library/urllib2.html#urllib2.URLError
code
An HTTP status code as defined in RFC 2616. This numeric value
corresponds to a value found in the dictionary of codes as found in
BaseHTTPServer.BaseHTTPRequestHandler.responses.
You can get the error code from an HTTPError from its code member, like so
try:
# ...
except HTTPError as ex:
status_code = ex.code

requests.HTTPError uncaught after a requests.get() 404 response

I'm having a slight problem with the requests library.
Say for example I have a statement like this in Python:
try:
request = requests.get('google.com/admin') #Should return 404
except requests.HTTPError, e:
print 'HTTP ERROR %s occured' % e.code
For some reason the exception is not being caught. I've checked the API documentation for requests but it's a bit slim. Is there anyone who has more experience with the library that might be able to help me out?
Interpreter is your friend:
import requests
requests.get('google.com/admin')
# MissingSchema: Invalid URL u'google.com/admin': No schema supplied
Also, requests exceptions:
import requests.exceptions
dir(requests.exceptions)
Also notice that by default requests doesn't raise exception if status is not 200:
In [9]: requests.get('https://google.com/admin')
Out[9]: <Response [503]>
There is raise_for_status() method that does it:
In [10]: resp = requests.get('https://google.com/admin')
In [11]: resp
Out[11]: <Response [503]>
In [12]: resp.raise_for_status()
...
HTTPError: 503 Server Error: Service Unavailable
Running your code in python 2.7.5:
import requests
try:
response = requests.get('google.com/admin') #Should return 404
except requests.HTTPError, e:
print 'HTTP ERROR %s occured' % e.code
print e
Results in:
File "C:\Python27\lib\site-packages\requests\models.py", line 291, in prepare_url
raise MissingSchema("Invalid URL %r: No schema supplied" % url)
requests.exceptions.MissingSchema: Invalid URL u'google.com/admin': No schema supplied
To get your code to pick up this exception you need to add:
except (requests.exceptions.MissingSchema) as e:
print 'Missing schema occured. status'
print e
Note also it is not a missing schema but a missing scheme.

Categories

Resources