我正在尝试将 jwt 合并到我的代码中,但出现模块错误

时间:2021-05-26 07:43:48

标签: python apache-spark pyspark jwt

我正在尝试向以下代码添加令牌机制。我尝试了很多方法,但下面一种似乎是实现 jwt 的更简单方法。但是当我运行代码时,在 from security import authenticate, identity

处出现模块错误

我也尝试通过创建单独的 requirements.txt 但它不起作用。

这是我的代码

from flask import request
from flask import Flask
#from jsonify.convert import jsonify
from flask import jsonify
from pyspark.shell import spark
from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StructField, StringType, IntegerType
from pyspark.sql.types import ArrayType, DoubleType, BooleanType
from pyspark.sql.functions import col, array_contains
import json
#from flask_jwt_extended import create_access_token
#from flask_jwt_extended import get_jwt_identity
#from flask_jwt_extended import jwt_required
#from flask_jwt_extended import JWTManager, jwt_required, create_access_token
import base64
from flask_jwt_extended import jwt_required
from flask import Flask
from flask_jwt import JWT
from security import authenticate, identity

app = Flask(__name__)
app.secret_key = "righdata"  # Make this long, random, and secret in a real app!
jwt = JWT(app, authenticate, identity)
app.config['JWT_ALGORITHM'] = 'HS512'
#jwt = JWTManager(app)
spark = SparkSession.builder.master("local").appName("DX_STATS").enableHiveSupport().getOrCreate()
spark.sql("SHOW TABLES").show()

class User(object):
    def __init__(self):
        self.id = id
        self.username = "abc"
        self.password = "password"

    def __str__(self):
        return "User(id='%s')" % self.id

users = [
    User(1, 'abc', 'password'),
]

username_table = {u.self.username: u for u in users}
userid_table = {u.id: u for u in users}

def authenticate():
    username = "abc"
    password = "password"
    user = username_table.get(username, None)
    if user and safe_str_cmp(user.password.encode('utf-8'), password.encode('utf-8')):
        return user

def identity(payload):
    user_id = payload['identity']
    return userid_table.get(user_id, None)

app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'secret_key'

jwt = JWT(app, authenticate, identity)

# if __name__ == '__main__':
#     app.run()
@app.route('/analyze', methods=["POST"])
@jwt_required()
def analyze():
    print(request.is_json)
    content = request.get_json()
    print(content)
    dir = content["preview"]
    print(dir)
    sql = "select * from " + tablename + " limit " + str(content["previewCount"])
    df = spark.sql(sql)
    data = df.toPandas()
    stats = data.describe(include=['object'])
    df2 = data.isnull().sum()
    notnull = data.notnull().sum()
    zero = (data == 0).sum(axis=0)
    nonzero = (data != 0).sum(axis=0)
    null_result = json.loads(df2.to_json(orient="index"))
    not_null = json.loads(notnull.to_json(orient="index"))
    stats_result = json.loads(stats.to_json(orient='index'))
    zero_result = json.loads(zero.to_json(orient="index"))
    non_zero = json.loads(nonzero.to_json(orient='index'))
    
    return json.dumps(stats_result, indent=4)

if __name__ == '__main__':
    app.run(debug=True)  # host = "0.0.0.0",port = specific port0

我收到此错误:

Traceback (most recent call last):
  File "new.py", line 19, in <module>
    from .security import authenticate, identity
ModuleNotFoundError: No module named '__main__.security'; '__main__' is not a package
^C
[1]+  Exit 1                  python new.py
$ python3 -m security
/bin/python3: No module named security
$~/myproject$

实际错误: 回溯(最近一次调用最后一次): 文件“new.py”,第 19 行,在 从安全进口认证,身份 ModuleNotFoundError: 没有名为“安全”的模块

而且我也遇到了以下错误:

错误 1:

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
flask-jwt 0.3.2 requires PyJWT<1.5.0,>=1.4.0, but you have pyjwt 2.1.0 which is incompatible.
flask-appbuilder 3.3.0 requires Flask-JWT-Extended<4,>=3.18, but you have flask-jwt-extended 4.2.1 which is incompatible.
flask-appbuilder 3.3.0 requires PyJWT<2.0.0,>=1.7.1, but you have pyjwt 2.1.0 which is incompatible.

错误 2:

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
flask-jwt 0.3.2 requires PyJWT<1.5.0,>=1.4.0, but you have pyjwt 1.7.1 which is incompatible.

这些总是安装最新版本。如何安装特定版本。

0 个答案:

没有答案
相关问题