# 入门

## 指导

借助 Tencent Cloud SDK for Python，您可以以面向对象编程的方式使用 Tencent Cloud 产品。

目前，Tencent Cloud SDK for Python 支持以同步或异步的编程方式集成到应用程序中使用，异步编程方式可以编写性能更好的应用程序。下面我们将通过多个连续的代码片段逐步向您演示如何在 Python 应用程序中集成并使用 Tencent Cloud SDK for Python。

### 无服务器云函数

我们以 Tencent Cloud 产品 无服务器云函数 为例。

假定我们在数据中心 `ap-shanghai` 拥有一个名为 `default` 的命名空间，该命名空间中拥有一个名为 `hello` 的函数。现在我们尝试使用 Tencent Cloud SDK for Python 调用该函数并获得返回值。

#### 导入模块

首先我们需要导入 Tencent Cloud SDK for Python 的相关软件包和模块：

```python
from tencent.cloud.core import errors
from tencent.cloud.auth import credentials
from tencent.cloud.serverless import functions
```

#### 访问凭据

接着我们需要实例化一个访问凭据，以便于 Tencent Cloud 可以确信您有权访问相关资源并执行操作：

```python
access_credentials = credentials.Credentials(
    secret_id = 'AKIDF3sMOAU1pOgkmrKHchX6TZQ1Mo1C5qa7',
    secret_key = 'b4JL8fwxkIgsKMXGi39yYt0ECxZw4wZf'
)
```

上文示例代码中给定的参数 `secret_id` 和 `secret_key` 为演示凭据，您需要转到 [Tencent Cloud 控制台](https://console.cloud.tencent.com/cam/capi) 创建并获取适用于您账户的凭据。

{% content-ref url="/pages/-M14z8RF-wJEWixbZtaA" %}
[选择最佳的访问凭据类型](/tencent-cloud-sdk/python-best-practices/other-products/select-credential-type.md)
{% endcontent-ref %}

#### 产品客户端

然后我们需要实例化一个 无服务器云函数 产品的抽象产品客户端，以便于我们通过面向对象编程的方式与 无服务云函数 产品交互：

```python
function_client: functions.Client = functions.Client(
    access_credentials = access_credentials
)
```

参数 `credentials_context` 要求您提供一个有效的访问凭据，它是可选的。

如果您在 无服务器云函数 产品所创建的函数中使用 Tencent Cloud SDK for Python 且该参数被忽略或设置为 `None`，Tencent Cloud SDK for Python 将尝试查找并使用环境凭据。

{% hint style="info" %}
环境凭据：是指绑定 [运行角色](https://cloud.tencent.com/document/product/583/32389) 的无服务器云函数被运行时，在运行环境中所临时生成的非持久性访问凭据。
{% endhint %}

#### 调用函数

最后我们将尝试调用无服务器云函数并获取返回值：

```python
return_value: str = function_client.easy_invoke(
    region_id = 'ap-shanghai',  # Unique identifier of the data center
    namespace_name = 'default', # Name of the namespace
    function_name = 'hello'     # Name of the Cloud Function
)
```

在以上示例代码中，局部变量 `return_value` 将拥有无服务器云函数 `hello` 的实际返回值。

{% hint style="info" %}
方法 **easy\_invoke** 将尝试推断给定无服务器云函数调用结果中的函数返回数据类型，并将其解析封装为 Python 原生数据类型返回。
{% endhint %}

请注意，如果给定的无服务器云函数运行时出错，方法 `easy_invoke` 将引发 `InvokeError` 异常，该异常类型在 `tencent.cloud.serverless.functions.errors` 模块中定义。

如果您正在寻找在无服务器云函数间相互调用的方法，使用 Tencent Cloud SDK for Python 提供的托管式产品客户端或许是一个不错的选择：

```python
return_value: str = functions.invoke('hello')
```

此时 Tencent Cloud SDK for Python 将代您管理访问凭据和产品客户端，函数 `functions.invoke` 在内部将调用托管产品客户端实例的 [`easy_invoke`](https://smallso.gitbook.io/tencent-cloud-sdk/python-docs/serverless-functions/class-and-method/client/easy-invoke-method) 方法。

{% hint style="warning" %}
请注意，函数 functions.invoke 仅支持已配置 [运行角色](https://cloud.tencent.com/document/product/583/32389) 的无服务器云函数发起调用。
{% endhint %}

参阅 [Quick-Start](https://github.com/nobody-night/tencent-cloud-sdk-python/blob/master/quickstart.py) 源代码以获取完整的无服务器云函数产品 Python 示例代码。

### 其他 Tencent Cloud 产品

对于 Tencent Cloud SDK for Python 未提供抽象产品客户端的 Tencent Cloud 产品，依然可以使用通用客户端直接访问 Tencent Cloud API 并获得结果。

我们以 Tencent Cloud 产品 云服务器 为例。假定我们需要检索数据中心 `ap-shanghai` 目前正在运营的所有可用区 ID 列表：

#### 导入模块

首先我们需要导入 Tencent Cloud SDK for Python 的相关软件包和模块：

```python
from tencent.cloud.core import errors
from tencent.cloud.core import client
from tencent.cloud.auth import credentials
```

#### 访问凭据

接着我们需要实例化一个访问凭据，以便于 Tencent Cloud 可以确信您有权访问相关资源并执行操作：

```python
access_credentials = credentials.Credentials(
    secret_id = 'AKIDF3sMOAU1pOgkmrKHchX6TZQ1Mo1C5qa7',
    secret_key = 'b4JL8fwxkIgsKMXGi39yYt0ECxZw4wZf'
)
```

上文示例代码中给定的参数 `secret_id` 和 `secret_key` 为演示凭据，您需要转到 [Tencent Cloud 控制台](https://console.cloud.tencent.com/cam/capi) 创建并获取适用于您账户的凭据。

#### 通用客户端

然后我们需要实例化一个通用客户端，以便于我们使用面向对象编程的方式与 云服务器 产品所提供的 Tencent Cloud API 进行交互：

```python
virtual_machine_client: client.UniversalClient = client.UniversalClient(
    product_id = 'cvm',     # Unique identifier of the product
    access_credentials = access_credentials    # Access credentials
)
```

#### 使用 Tencent Cloud API

最后我们尝试使用 云服务器 产品提供的 Tencent Cloud API -  [DescribeZones](https://cloud.tencent.com/document/api/213/15707) 检索数据中心园区正在运营的所有可用区 ID 列表：

```python
action_result: dict = virtual_machine_client.action(
    region_id = 'ap-shanghai',      # Unique identifier of the data center
    action_id = 'DescribeZones',    # Unique identifier of the Tencent Cloud API
    action_version = '2017-03-12'   # Version name of the Tencent Cloud API
)
```

请注意，如果给定 Tencent Cloud API 发生错误或对应操作执行失败，方法 `action` 将引发 `ActionError` 异常；如果给定 Tencent Cloud API 返回结果不符合预期协定，方法 `action` 将引发 `ActionResultError` 异常。这些异常类型在模块 `tencent.cloud.core.errors` 中定义。

打印数据中心正在运营的所有可用区 ID：

```python
for zone_info in action_result['ZoneSet']:
    print(zone_info['Zone'])
```

## API 文档

在上文中我们通过多个连续的代码片段逐步向您演示 Tencent Cloud SDK for Python 的基本用法。如果您需要获取相关产品客户端或通用客户端的其他用法，请参阅我们的技术文档。

{% content-ref url="/pages/-M-U\_GOq6lFUAS4WY7vz" %}
[API 文档](/tencent-cloud-sdk/python-docs.md)
{% endcontent-ref %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://smallso.gitbook.io/tencent-cloud-sdk/python-getting-started.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
