8

I have a python Flask listener waiting on port 8080. I expect another process to make a series of POST's to this port.The code for listener is as follows.

#!/usr/bin/env python2
from __future__ import print_function
from flask import Flask, request
from werkzeug import secure_filename
from datetime import datetime
import os, traceback, sys 
import zlib
import ssl 
import json
import os
import base64

app = Flask('__name__')

@app.route('/',methods=['GET','POST','OPTIONS'])                                                                                                                                         
def recive_fe_events():
    try:
        data = request.get_data()

        if request.content_length < 20000 and request.content_length != 0:
            filename = 'out/{0}.json'.format(str(datetime.now()))
            with open(filename, 'w') as f:
                 f.write(data)

            print('Wrote', filename)
        else:
            print("Request too long", request.content_length)
            content = '{{"status": 413, "content_length": {0}, "content": "{1}"}}'.format(request.content_length, data)
            return content, 413 
    except:
        traceback.print_exc()
        return None, status.HTTP_500_INTERNAL_SERVER_ERROR

    return '{"status": 200}\n'

if __name__ == '__main__':
    app.run(host='0.0.0.0',debug=False,port=8080)

However whenever I try to trigger an event to be pushed to the above listener.It seems that I am getting OPTIONS instead of POST.

192.168.129.75 - - [20/May/2015 14:33:45] "OPTIONS / HTTP/1.1" 200 -
192.168.129.75 - - [20/May/2015 14:33:45] "OPTIONS / HTTP/1.1" 200 -
192.168.129.75 - - [20/May/2015 14:33:51] "OPTIONS / HTTP/1.1" 200 -
192.168.129.75 - - [20/May/2015 14:33:51] "OPTIONS / HTTP/1.1" 200 -

The investigation of my client revealed that it expects the following flags in its response to OPTIONS.

Access-Control-Allow-Origin          value_1
Access-Control-Allow-Methods         value_2      
Access-Control-Allow-Headers         value_3

How do I format the above response to OPTIONS so that my server can start receiving POST messages from the client.

liv2hak
  • 14,472
  • 53
  • 157
  • 270
  • possible duplicate of [Python Flask application getting OPTIONS instead of POST](http://stackoverflow.com/questions/30339629/python-flask-application-getting-options-instead-of-post) – Celeo May 20 '15 at 22:35
  • @Celeo - That is my question as well. I have clearly tried and looked at the answer and this is a different problem. – liv2hak May 20 '15 at 22:37
  • HTTP clients send an OPTIONS request first to handle CORS. You don't have to specifically handle the OPTIONS method. Google "CORS preflight request" – nathancahill May 20 '15 at 22:58
  • @nathancahill - That is not working in my case. So I have to explictly send OPTIONS response with the mentioned values. – liv2hak May 20 '15 at 23:00
  • 2
    What do you mean not working? Is Flask set up for CORS? Are you using [Flask-CORS](http://flask-cors.readthedocs.org/en/latest/)? – nathancahill May 20 '15 at 23:01
  • @nathancahill - You are a lifesaver :) Can you please post this as an answer so that I can accept it. Thanks :) – liv2hak May 20 '15 at 23:11
  • Ok, you should probably delete your other question too :) – nathancahill May 20 '15 at 23:14
  • well I did try.I didn't let me :( – liv2hak May 20 '15 at 23:29
  • @nathancahill - could you please look at http://stackoverflow.com/questions/30362121/scalable-server-to-listen-to-post-messages please? – liv2hak May 20 '15 at 23:40

2 Answers2

4

You need to set up your application for CORS. The easiest was is to use Flask-CORS.

nathancahill
  • 10,452
  • 9
  • 51
  • 91
0

This is old, but i'd like to actually answer the question...

In preflight a browser, and probably toolkit, sends an OPTIONS-request to figure out if it's safe to send a POST or PUT. Those are the typical cases...

Note that the headers should be on an OPTIONS-request only, they are redundant in the actual POST/PUT... cases.

I return the following in response-headers for an OPTIONS:

'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Max-Age': '300'

You might want to read the spect for more details.

I find the Flask-CORS somewhat redundant, but thats a matter of taste perhaps.

OriginalHacker
  • 671
  • 6
  • 8