首页 > 解决方案 > 将类从一个模块导入另一个模块 - 烧瓶 - 数据未在服务器/docker中传输

问题描述

应用程序.py

class ClientInformation:
    _id = None
    _secret = None

ClientInformation = ClientInformation() ## this is not needed, just added it made it work on the container

@api.route("/test_me")
class TestMe(Resource):
    def post(self):
        parser = reqparse.RequestParser()

        ### define arguments
        parser.add_argument("client_id", type=str, required=True, )
        parser.add_argument("client_secret", type=str, required=True)

        ClientInformation._id = args["client_id"]
        ClientInformation._secret = args["client_secret"]
        
        ## Start test with threading to avoid timeout error
        x = threading.Thread(target=run_tests.run_test, kwargs=dict(pytest_pattern="test_oauth.py"))
        x.start()

        return response(f"OAuth Testing started: {datetime.now().strftime('%H:%M:%S')}", status=202)

test_oauth.py

import oauth_requests.py
oauth_requests.print_clientInformation.py

ouath_requests.py

from app import ClientInformation
def print_clientInformation():
    client_id = ClientInformation._id
    client_secret = ClientInformation._secret
    payload = {"client_id": client_id, "client_secret": client_secret}
    print(payload)

收到请求后的日志输出

{'client_id': None, 'client_secret': None} ## should not be None

笔记:

它是ClientInformation._id并且ClientInformation._secret在导入oauth_requests.py时为空,如有效负载所示。

工作流程是:

发送 POST 请求 --> @api.route('/test_OAuth') 接收它并解析请求中的数据 --> 将数据存储到 ClientInformation --> 启动测试文件“test_OAuth.py”,该文件调用一个函数oauth_requests.py 获取令牌。

为了获取令牌,它从应用程序导入 ClientInformation。但如您所见,有效载荷中充满了无。

这在使用烧瓶上的本地服务器启动应用程序时非常有效。

这里有什么问题?

标签: pythondockerflaskkubernetes

解决方案


推荐阅读