http://www.web2py.com/books/default/chapter/29/09/access-control#Access-Control-and-Basic-Authentication
суббота, 16 января 2021 г. в 00:54:58 UTC+3, 黄祥: 

> *controllers/api.py*
> @auth.requires_login()
> def rows_as_json():
>     if not request.env.request_method == 'GET': raise HTTP(403)
>     table_name = request.args(0)
>     id = request.args(1)
>     if id.isdigit() and int(id) > 0:
>         query = (db[table_name]['id'] == id)
>     else:
>         query = (db[table_name]['id'] > 0)
>     rows = db(query).select().as_json()
>     return rows
>
> *terminal*
> curl -X GET --user admin:password -i 
> http://localhost:8000/music/api/rows_as_json/instrument/1
> *result*
> You are being redirected <a 
> href="/music/default/user/login?_next=/music/api/rows_as_json/instrument/1">here</a>
>
> *python*
> import requests
> from requests.auth import HTTPBasicAuth 
> auth = HTTPBasicAuth('admin', 'password')
> r = requests.get("
> http://localhost:8000/music/api/rows_as_json.json/instrument/1";, auth = 
> auth).json()
> print(r)
> *result*
> {'form': '<form action="#" enctype="multipart/form-data" 
> method="post"><div class="form-group row" 
> id="auth_user_username__row"><label class="form-control-label col-sm-3" 
> for="auth_user_username" 
> id="auth_user_username__label">Username</label><div class="col-sm-9"><input 
> class="form-control string" id="auth_user_username" name="username" 
> type="text" value="" /><span class="help-block"></span></div></div><div 
> class="form-group row" id="auth_user_password__row"><label 
> class="form-control-label col-sm-3" for="auth_user_password" 
> id="auth_user_password__label">Password</label><div class="col-sm-9"><input 
> class="password form-control" id="auth_user_password" name="password" 
> type="password" value="" /><span class="help-block"></span></div></div><div 
> class="form-group row" id="auth_user_remember_me__row"><div 
> class="sm-hidden col-sm-3"></div><div class="col-sm-9"><div 
> class="form-check"><label class="form-check-label" 
> for="auth_user_remember_me" id="auth_user_remember_me__label"><input 
> class="boolean form-check-input" id="auth_user_remember_me" 
> name="remember_me" type="checkbox" value="on" />Remember me (for 30 
> days)</label><span class="help-block"></span></div></div></div><div 
> class="form-group row" id="submit_record__row"><div class="col-sm-9 
> col-sm-offset-3"><input class="btn btn-primary" type="submit" value="Log 
> In" /></div></div><div style="display:none;"><input name="_next" 
> type="hidden" value="/music/api/rows_as_json.json/instrument/1" /><input 
> name="_formkey" type="hidden" value="bda90a78-ce8a-404f-b6fc-b805c78276c7" 
> /><input name="_formname" type="hidden" value="login" /></div></form>'}
>
> r = requests.get("
> http://localhost:8000/music/api/rows_as_json.json/instrument/1";, auth = 
> ('admin', 'password') ).json()
> print(r)
> *result*
> {'form': '<form action="#" enctype="multipart/form-data" 
> method="post"><div class="form-group row" 
> id="auth_user_username__row"><label class="form-control-label col-sm-3" 
> for="auth_user_username" 
> id="auth_user_username__label">Username</label><div class="col-sm-9"><input 
> class="form-control string" id="auth_user_username" name="username" 
> type="text" value="" /><span class="help-block"></span></div></div><div 
> class="form-group row" id="auth_user_password__row"><label 
> class="form-control-label col-sm-3" for="auth_user_password" 
> id="auth_user_password__label">Password</label><div class="col-sm-9"><input 
> class="password form-control" id="auth_user_password" name="password" 
> type="password" value="" /><span class="help-block"></span></div></div><div 
> class="form-group row" id="auth_user_remember_me__row"><div 
> class="sm-hidden col-sm-3"></div><div class="col-sm-9"><div 
> class="form-check"><label class="form-check-label" 
> for="auth_user_remember_me" id="auth_user_remember_me__label"><input 
> class="boolean form-check-input" id="auth_user_remember_me" 
> name="remember_me" type="checkbox" value="on" />Remember me (for 30 
> days)</label><span class="help-block"></span></div></div></div><div 
> class="form-group row" id="submit_record__row"><div class="col-sm-9 
> col-sm-offset-3"><input class="btn btn-primary" type="submit" value="Log 
> In" /></div></div><div style="display:none;"><input name="_next" 
> type="hidden" value="/music/api/rows_as_json.json/instrument/1" /><input 
> name="_formkey" type="hidden" value="bb23c02c-174b-47c9-aa91-bd9761bd99d0" 
> /><input name="_formname" type="hidden" value="login" /></div></form>'}
>
> *description*
> above code work in the past
> currently use web2py version 2.20.4-stable
> right now it only work on browser
>
> *question*
> is there something wrong with current @auth.requires_login() to work in 
> terminal or python requests ?
> how to make it work with curl or python requests in web2py current version 
> ?
>
> thanks and best regards,
> stifan
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/4fdfe5f0-7eb8-4f25-8e2b-d367c33de8e7n%40googlegroups.com.

Reply via email to