Darshan Deshmukh Darshan Deshmukh - 8 days ago 6
Python Question

How to check partial response from a http server

My RSS JSON response looks like this

{
"kind": "demo",
...
"items": [
{
"title": "First title",
"comment": "First comment.",
"characteristics": {
"length": "short",
"accuracy": "high",
"followers": ["Jo", "Will"],
},
"status": "active",
...
},


I wanted to fetch partial response in case there is a delay in returning entire body due to any issues

Answer

You can use requests library on python. You can easily check for partial response using the following code snippet,

import requests

def get_feed(trials=1):
    response = requests.get('http://example.com/rss')
    if response.status_code == 200:
        # the response is good! do something
        pass
    elif response.status_code == 206:
        # partial response... maybe try again,
        if trials > 5:
            # tried 5 times, but no response.
            return None
        else:
            return get_feed(trials+1)
Comments