Generate http error from python3 requests
<p>I have a  simple long poll thing using <code>python3</code> and the <code>requests</code> package. It currently looks something like:</p>

<pre><code>def longpoll():
    session = requests.Session()
    while True:
        try:
            fetched = session.get(MyURL)
            input = base64.b64decode(fetched.content)
            output = process(data) 
            session.put(MyURL, data=base64.b64encode(response))
        except Exception as e:
            print(e)
            time.sleep(10)
</code></pre>

<p>There is a case where instead of <code>process</code>ing the input and <code>put</code>ing the result, I'd like to raise an http error. Is there a simple way to do this from the high level <code>Session</code> interface? Or do I have to drill down to use the lower level objects?</p>

<p>Since You have control over the server you may want to reverse the 2nd call</p>

<p>Here is an example using bottle to recive the 2nd poll</p>

<pre><code>def longpoll():
    session = requests.Session()
    while True: #I'm guessing that the server does not care that we call him a lot of times ...
        try: 
            session.post(MyURL, {"ip_address": my_ip_address}) # request work or I'm alive
            #input = base64.b64decode(fetched.content)
            #output = process(data) 
            #session.put(MyURL, data=base64.b64encode(response))
        except Exception as e:
            print(e)
            time.sleep(10)

@bottle.post("/process") 
def process_new_work():
    data = bottle.request.json()
    output = process(data) #if an error is thrown an HTTP error will be returned by the framework
    return output
</code></pre>

<p>This way the server will get the output or an bad HTTP status</p>

python python-3.x python-requests http-error