app.py
from flask import Flask,request
from flask_restful import Api , Resource
from flask_jwt import JWT ,jwt_required , current_identity
from security import auntheticate , identity
app = Flask(__name__)
app.secret_key = "sangam"
api = Api(app)
jwt = JWT(app,auntheticate,identity)
items = []
class Item(Resource):
@jwt_required
def get(self,nam):
item = next(filter(lambda x:x['name'] == nam, items), None)
return {'item':item} ,200 if item else 404
def post(self,nam):
if next(filter(lambda x:x['name'] == nam,items),None):
return{'message':"an item with name '{}' already exist." .format(nam)}
data = request.get_json()
item = {'name':nam,
'price':data['price']}
items.append(item)
return item,201
def delete(self,nam):
global items
items = list(filter(lambda x:x['name'] != nam,items))
return{'message':'item deleted'}
def put(self,nam):
data = request.get_json()
item = next(filter(lambda x:x['name'] == nam,items),None)
if item is None:
item = {'name':nam,'price':data['price']}
items.append(item)
else:
item.update(data)
return item
class item_list(Resource):
def get(self):
return {'itmes':items}
api.add_resource(Item,'/item/<string:nam>')
api.add_resource(item_list,'/items')
app.run(port = 5000 , debug = True)
securtiy.py
from werkzeug.security import safe_str_cmp
from user import User
users = [User(1,'bob','asdf')]
username_mapping = {u.username: u for u in users}
userid_mapping = {u.id: u for u in users}
def auntheticate(username,password):
user = username_mapping.get(username , None)
if user and safe_str_cmp(user.password , password):
return user
def identity(payload):
user_id = payload['identity']
return userid_mapping.get(user_id,None)
user.py
class User:
def __init__(self,_id,username,password):
self.id = _id
self.username = username
self.password = password
(错误消息(
Traceback (most recent call last):
File "C:UsersSANGAM PRASAD YADAV.virtualenvsvenv-6bMM9kJAlibsite-packagesflaskapp.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "C:UsersSANGAM PRASAD YADAV.virtualenvsvenv-6bMM9kJAlibsite-packagesflaskapp.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "C:UsersSANGAM PRASAD YADAV.virtualenvsvenv-6bMM9kJAlibsite-packagesflask_restful__init__.py", line 272, in error_router
return original_handler(e)
File "C:UsersSANGAM PRASAD YADAV.virtualenvsvenv-6bMM9kJAlibsite-packagesflaskapp.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:UsersSANGAM PRASAD YADAV.virtualenvsvenv-6bMM9kJAlibsite-packagesflask_compat.py", line 38, in reraise
raise value.with_traceback(tb)
File "C:UsersSANGAM PRASAD YADAV.virtualenvsvenv-6bMM9kJAlibsite-packagesflaskapp.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:UsersSANGAM PRASAD YADAV.virtualenvsvenv-6bMM9kJAlibsite-packagesflaskapp.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:UsersSANGAM PRASAD YADAV.virtualenvsvenv-6bMM9kJAlibsite-packagesflask_restful__init__.py", line 272, in error_router
return original_handler(e)
File "C:UsersSANGAM PRASAD YADAV.virtualenvsvenv-6bMM9kJAlibsite-packagesflaskapp.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:UsersSANGAM PRASAD YADAV.virtualenvsvenv-6bMM9kJAlibsite-packagesflask_compat.py", line 38, in reraise
raise value.with_traceback(tb)
File "C:UsersSANGAM PRASAD YADAV.virtualenvsvenv-6bMM9kJAlibsite-packagesflaskapp.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "C:UsersSANGAM PRASAD YADAV.virtualenvsvenv-6bMM9kJAlibsite-packagesflaskapp.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:UsersSANGAM PRASAD YADAV.virtualenvsvenv-6bMM9kJAlibsite-packagesflask_restful__init__.py", line 468, in wrapper
resp = resource(*args, **kwargs)
File "C:UsersSANGAM PRASAD YADAV.virtualenvsvenv-6bMM9kJAlibsite-packagesflaskviews.py", line 89, in view
return self.dispatch_request(*args, **kwargs)
File "C:UsersSANGAM PRASAD YADAV.virtualenvsvenv-6bMM9kJAlibsite-packagesflask_restful__init__.py", line 583, in dispatch_request
resp = meth(*args, **kwargs)
TypeError: wrapper() got an unexpected keyword argument 'nam'
问题似乎出在 Item 类的 get 方法的@jwt_required装饰器中。 这是一个修复:在@jwt_required后添加参数,就像@jwt_required()
一样。它在我的情况下有效。