librelist archives

« back to archive

Why web service API in Flask is not work

Why web service API in Flask is not work

From:
Jerry OELoo
Date:
2014-12-01 @ 08:55
Hi All:
I am using Nginx+uWSGI+Flask to build up a web service API.

I follow http://flask.pocoo.org/docs/0.10/deploying/uwsgi/ as below

In Nginx, I add below configuration to let flask handle
http://www.xxx.com/appapi/query?name=123

location = /appapi { rewrite ^ /appapi /; }
location /appapi { try_files $uri @appapi ; }
location @appapi {
  include uwsgi_params;
  uwsgi_param SCRIPT_NAME /appapi;
  uwsgi_modifier1 30;
  uwsgi_pass 127.0.0.1:3301;
}

In Flask app code. I have defined route for appapi

@app.route('/appapi/query')
def query():
    print 'query()'


But I find query function is not called, and in log. it return 404, not found.
Thanks in advance!


-- 
Rejoice,I Desire!

Re: [flask] Why web service API in Flask is not work

From:
Raimon Esteve
Date:
2014-12-01 @ 12:36
2014-12-01 9:55 GMT+01:00 Jerry OELoo <oyljerry@gmail.com>:
>
> Hi All:
> I am using Nginx+uWSGI+Flask to build up a web service API.
>
> I follow http://flask.pocoo.org/docs/0.10/deploying/uwsgi/ as below
>
> In Nginx, I add below configuration to let flask handle
> http://www.xxx.com/appapi/query?name=123
>
> location = /appapi { rewrite ^ /appapi /; }
> location /appapi { try_files $uri @appapi ; }
> location @appapi {
>   include uwsgi_params;
>   uwsgi_param SCRIPT_NAME /appapi;
>   uwsgi_modifier1 30;
>   uwsgi_pass 127.0.0.1:3301;
> }
>
I like more configure Nginx + Supervisor + Flask

>
> In Flask app code. I have defined route for appapi
>
> @app.route('/appapi/query')
> def query():
>     print 'query()'

In your example url is http://localhost:5000/appapi/query

an other example:

from flask import Flask
from werkzeug.contrib.fixers import ProxyFix

app = Flask(__name__)

@app.route('/appapi/query')
def query():
    print 'Hello world'

app.wsgi_app = ProxyFix(app.wsgi_app)

if __name__ == "__main__":
    app.run()


I think in query view you would like:
http://localhost:5000/appapi/?q=SEARCHSTRING

Or pass a param in appapi view. An example:

@app.route("/appapi/<slug>")
def query(query):
    print "query is %s" % query

Re: [flask] Why web service API in Flask is not work

From:
Jerry OELoo
Date:
2014-12-03 @ 03:01
Hi Raimon:
Thanks for your information.
Now I find that my query() api will not be called when nginx receive
http reqeust, but I do not know the root cause.

On Mon, Dec 1, 2014 at 8:36 PM, Raimon Esteve <raimonesteve@gmail.com> wrote:
> 2014-12-01 9:55 GMT+01:00 Jerry OELoo <oyljerry@gmail.com>:
>>
>> Hi All:
>> I am using Nginx+uWSGI+Flask to build up a web service API.
>>
>> I follow http://flask.pocoo.org/docs/0.10/deploying/uwsgi/ as below
>>
>> In Nginx, I add below configuration to let flask handle
>> http://www.xxx.com/appapi/query?name=123
>>
>> location = /appapi { rewrite ^ /appapi /; }
>> location /appapi { try_files $uri @appapi ; }
>> location @appapi {
>>   include uwsgi_params;
>>   uwsgi_param SCRIPT_NAME /appapi;
>>   uwsgi_modifier1 30;
>>   uwsgi_pass 127.0.0.1:3301;
>> }
>>
> I like more configure Nginx + Supervisor + Flask
>
>>
>> In Flask app code. I have defined route for appapi
>>
>> @app.route('/appapi/query')
>> def query():
>>     print 'query()'
>
> In your example url is http://localhost:5000/appapi/query
>
> an other example:
>
> from flask import Flask
> from werkzeug.contrib.fixers import ProxyFix
>
> app = Flask(__name__)
>
> @app.route('/appapi/query')
> def query():
>     print 'Hello world'
>
> app.wsgi_app = ProxyFix(app.wsgi_app)
>
> if __name__ == "__main__":
>     app.run()
>
>
> I think in query view you would like:
> http://localhost:5000/appapi/?q=SEARCHSTRING
>
> Or pass a param in appapi view. An example:
>
> @app.route("/appapi/<slug>")
> def query(query):
>     print "query is %s" % query



-- 
Rejoice,I Desire!