为什么不从该函数生成输出?

问题描述 投票:-1回答:1

我有这个python函数,我希望使用lambda处理函数执行它,因此我已经编写了这段代码。在Pycharm中执行时,在控制台中看不到任何输出。有人可以指导下面的代码有什么问题吗?

import json
from json import loads

import requests
from requests import exceptions
from requests.auth import HTTPBasicAuth


def lambda_handler(event, context):
    test_post_headers_body_json()
    return {"statusCode": 200, "body": json.dumps("Hello from Lambda!")}


def test_post_headers_body_json():
    client_id = "WJRYDHNGROIZHL8B"
    client_secret = "V5VXK6FLG1YI0GD2XY3H"
    user = "[email protected]"


    password = "c0Ba5PBdvVl2"

    access_point = "https://api.platform.abc.com/auth/oauth/token"
    grant_type = "password"

    headers = {"Content-Type": "application/x-www-form-urlencoded"}

    # auth = auth.HTTPBasicAuth(client_id, client_secret)

    data = {"grant_type": grant_type, "username": user, "password": password}

    resp = None
    try:
        resp = requests.post(
            access_point,
            auth=HTTPBasicAuth(client_id, client_secret),
            data=data,
            headers=headers,
        )
    except exceptions.ConnectionError:
        exit(1)

    if resp.status_code == 200:
        resp = loads(resp.text)
        if "access_token" in resp:
            print(resp["access_token"])
            exit(0)

    exit(1)

python pycharm
1个回答
0
投票

这是正常的,因为在运行代码时,Python仅声明该函数未使用它。您应该在文件末尾添加__main__ entry point

__main__
© www.soinside.com 2019 - 2024. All rights reserved.