How to respond with a status = 200 to a HTTPS POST in python

1

I am creating an application in Flask that receives an HTTPS POST message and processes the data it contains.

Once I have received the HTTPS message, I have to answer the server with a code = 200 so that it knows that I have received the message and it does not resend it to me within a few seconds. The problem I have is that more code is executed before arriving at the return and it gives time to receive 2 or 3 messages from the server before arriving at the return.

How can I send the code 200 in the middle of the code and actually receive only one message? Do I have to do a HTTPS GET even though I do not ask for anything?

 app = Flask(__name__)
@app.route('/webhook', methods=['POST','GET'])''
def webhook():
req = request.form
xml = req['data']
#HOW DO I SEND A CODE 200 NOW???
info = ET.fromstring(xml)
print info[0].text
print info[1].text
print info[2].text
print info[3].text
sender = email()
return None

Thank you!

    
asked by Alex 04.06.2017 в 17:09
source

1 answer

0

In these cases what is usually done is to move the heavy part to a thread / process so that the request itself can issue the response in an 'immediate' manner. Of course, this is only possible if the answer will not contain the result of what we want to process, as in this case. The simplest is something like:

from threading import Thread
from flask import Flask, Response

app = Flask(__name__)

def procesar(xml):
    info = ET.fromstring(xml)
    print info[0].text
    print info[1].text
    print info[2].text
    print info[3].text
    sender = email()


@app.route('/webhook', methods=['POST','GET'])''
def webhook():
    req = request.form
    xml = req['data']
    t = Thread(target=procesar, args=(xml,))
    t.start()
    return None

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8082, debug=debug, threaded=True)

This is only by way of example, make sure that what you handle in the threads is thread-safe.

    
answered by 04.06.2017 / 21:35
source