region
and SecretId
).default
will be used by default.FuncInvoked
in Guangzhou region with the following content:# -*- coding: utf8 -*-def main_handler(event, context):if 'key1' in event.keys():print("value1 = " + event['key1'])if 'key2' in event.keys():print("value2 = " + event['key2'])return "Hello World from the function being invoked" #return
PythonInvokeTest
in Chengdu region. You can edit it as needed in the following two methods.from tencentserverless import scffrom tencentserverless.scf import Clientfrom tencentserverless.exception import TencentServerlessSDKExceptionfrom tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKExceptiondef main_handler(event, context):print("prepare to invoke a function!")try:data = scf.invoke('FuncInvoked',region="ap-guangzhou",data={"a": "b"})print (data)except TencentServerlessSDKException as e:print (e)except TencentCloudSDKException as e:print (e)except Exception as e:print (e)return "Already invoked a function!" # return
"Already invoked a function!"
Client
by using the following sample code:# -*- coding: utf8 -*-from tencentserverless import scffrom tencentserverless.scf import Clientfrom tencentserverless.exception import TencentServerlessSDKExceptionfrom tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKExceptiondef main_handler(event, context):#scf = Client(region="ap-guangzhou") # To use this method to establish a `Client` connection, enable the "execution role" feature in the function configuration and select an execution role with the function invocation permission.scf = Client(secret_id="AKIxxxxxxxxxxxxxxxxxxxxxxggB4Sa",secret_key="3vZzxxxxxxxxxxxaeTC",region="ap-guangzhou",token=" ")# To use this method to establish a `Client` connection, replace `secret_id` and `secret_key` in the sample code with your actual `secret_id` and `secret_key`. This key pair needs to contain the function invocation permission.print("prepare to invoke a function!")try:data = scf.invoke('FuncInvoked',data={"a": "b"})# data = scf.FuncInvoked(data={"a": "b"}) # To use Python's native invocation method, perform initialization through `Client` first.print (data)except TencentServerlessSDKException as e:print (e)except TencentCloudSDKException as e:print (e)except Exception as e:print (e)return "Already invoked a function!" # return
"Already invoked a function!"
secret_id
and secret_key
: TencentCloud API key ID and key, which can be obtained or created in TencentCloud API Key > API Key Management in the CAM console.pip install tencentserverless
cd tencent-serverless-python-masterpython setup.py install
pip install tencentserverless -U
pip show tencentserverless
FuncInvoked
in Guangzhou region with the following content:# -*- coding: utf8 -*-def main_handler(event, context):if 'key1' in event.keys():print("value1 = " + event['key1'])if 'key2' in event.keys():print("value2 = " + event['key2'])return "Hello World from the function being invoked" #return
PythonInvokeTest.py
with the following content:# -*- coding: utf8 -*-from tencentserverless import scffrom tencentserverless.scf import Clientfrom tencentserverless.exception import TencentServerlessSDKExceptionfrom tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKExceptiondef main_handler(event, context):print("prepare to invoke a function!")scf = Client(secret_id="AKIxxxxxxxxxxxxxxxxxxxxxxggB4Sa",secret_key="3vZzxxxxxxxxxxxaeTC",region="ap-guangzhou",token=" ")# Replace with your own `secret_id` and `secret_key`try:data = scf.invoke('FuncInvoked',data={"a":"b"})# data = scf.FuncInvoked(data={"a":"b"})print (data)except TencentServerlessSDKException as e:print (e)except TencentCloudSDKException as e:print (e)except Exception as e:print (e)return "Already invoked a function!" # returnmain_handler("","")
PythonInvokeTest.py
file is located and run the following command to view the result.python PythonInvokeTest.py
prepare to invoke a function!"Hello World form the function being invoked"
Parameter Name | Required | Type | Description |
region | No | String | Region, which is the same as the region of the function invoking the API and is Guangzhou for local invocations by default. |
secret_id | No | String | User `SecretId`, which is obtained from the function's environment variable by default and is required for local debugging. |
secret_key | No | String | User `SecretKey`, which is obtained from the function's environment variable by default and is required for local debugging. |
token | No | String | User `token`, which is obtained from the function's environment variable by default. |
Parameter Name | Required | Type | Description |
function_name | Yes | String | Function name. |
qualifier | No | String | Function version. Default value: $LATEST. |
data | No | Object | Input parameter for function execution, which must be an object that can be processed by `json.dumps`. |
namespace | No | String | Namespace. Default value: default. |
Parameter | Required | Type | Description |
region | No | String | Region, which is the same as the region of the function invoking the API and is Guangzhou for local invocations by default. |
secret_id | No | String | User SecretId , which is obtained from the function's environment variable by default and is required for local debugging. |
secret_key | No | String | User SecretKey , which is obtained from the function's environment variable by default and is required for local debugging. |
token | No | String | User token , which is obtained from the function's environment variable by default. |
function_name | Yes | String | Function name. |
qualifier | No | String | Function version. Default value: $LATEST. |
data | No | String | Input parameter for function execution, which must be an object that can be processed by json.dumps . |
namespace | No | String | Namespace. Default value: default. |
Method Name | Description |
get_code | Returns error code |
get_message | Returns error message |
get_request_id | Returns RequestId |
get_response | Returns response |
get_stack_trace | Returns stack_trace |
Was this page helpful?