> For the complete documentation index, see [llms.txt](https://docs.decentraland.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.decentraland.org/contributor/contributor-zh/nei-rong/shi-jian/python-examples.md).

# Python 示例

这个实践展示了如何编写一个简单程序，使用 [快照](https://github.com/decentraland/docs/tree/main/contributor/practice/snapshots/README.md) 内容服务器提供的。

{% hint style="info" %}
你可以在 [完整脚本](https://github.com/decentraland/documentation/blob/main/content/contributor/content/practice/snapshots_mini.py) 的 GitHub 中找到，同时还有一个 [更高级的示例](https://github.com/decentraland/documentation/blob/main/content/contributor/content/practice/snapshots.py).
{% endhint %}

我们将使用 Decentraland 基金会的服务器，地址是 `peer.decentraland.org`，并以 Python 3 作为我们选择的语言。

我们将这样做：

1. 查询内容服务器的状态。
2. 选择并下载一个 [快照](https://github.com/decentraland/docs/tree/main/contributor/practice/snapshots/README.md) ，其中包含实体列表。
3. 打印所有引用实体的类型和 ID。

让我们先做一些准备来开始编写脚本。我们只会使用标准库模块，但在实际代码中，你可能会想用一个更方便的 HTTP 客户端（比如 [requests](https://github.com/psf/requests) 库）。

```py
# 发起一个 HTTP GET 请求，返回一个类似文件的 HTTP 响应。
def fetch(path):
    url = f"https://peer.decentraland.org/{path}"
    headers = { "User-Agent": "urllib" } # 在某些服务器上很重要（如果为空，会返回 403 Forbidden）

    request = urllib.request.Request(url, headers=headers)
    response = urllib.request.urlopen(request)

    return response
```

我们这个简单的辅助函数会发起一个 HTTP `GET` 请求，并返回类似文件的响应对象。没什么花哨的。让我们用它来访问 `/about` 端点并检查服务器状态：

```py
# 检查服务器状态：
about = json.load(fetch('about'))

if not about['healthy']:
    print("Server not healthy!")
    sys.exit(1)
```

如果我们能通过这一点，说明服务器正在运行（我们得到了一个 `200` 响应），并且报告自己处于正常工作状态。我们可以请求当前的快照集合（它以 JSON 数组格式返回）：

```py
# 获取快照列表：
all_snapshots = json.load(fetch('content/snapshots'))
```

快照文件（尤其是时间范围更长的那些）可能非常大。为了快速试验，我们先按 `numberOfEntities`:

```py
# 选择包含实体数量最少的快照：
snapshot = min(all_snapshots, key=lambda s: s['numberOfEntities'])
```

要下载内容，我们需要 `hash` 字段，来自 `快照`。我们通过将它附加到内容根路径来获取文件 URL：

```py
# 从内容 API 请求文件：
response = fetch('content/contents/' + snapshot['hash'])
```

我们选中的文件足够小，可以缓存在内存中，但让我们假装不知道这一点，直接流式读取它。第一行是快照头，后面的每一行都包含一个 JSON 对象。

让我们检查一下头部，这是个好习惯：

```py
# 验证快照头：
header = response.readline().decode('utf-8').strip()

if header != '### Decentraland json snapshot':
    print("Invalid snapshot header: " + header)
    sys.exit(1)
```

现在我们可以逐行读取响应，处理快照中的所有实体了。就我们这个简单目的而言， *处理* 是指打印实体类型和 ID：

```py
# 逐行读取并解码所有项目，每行一个 JSON：
for line in response:
    item = json.loads(line)
    print(item['entityType'], item['entityId'])
```

这个循环将开始流式读取、解析并打印如下内容，直到快照处理完毕：

```
profile bafkreic36qmzyprs6whkpuxbeiif4no6kvdrr2tfpichbx2fzfz5py6eyv
scene bafkreibr5xfujqrp5q3o4s73vm2yljlcp7cucqgugssnarsuclxv4emlmy
profile bafkreid7khr5wnkialba44rsslffi633rh3lvctad5oa5vjoe6wa7s4c5a
wearable bafkreihlqcb7jgubomyidikpwpqhgzbagltk5m4rgbjdvzydxmoka7bg4i
```

干杯！我们已经使用快照系统探索了 Decentraland 中一些可用的内容。

请记住，你可以在 [完整脚本](https://github.com/decentraland/documentation/blob/main/content/contributor/content/practice/snapshots_mini.py) 的 GitHub 中找到，同时还有一个 [更高级的示例](https://github.com/decentraland/documentation/blob/main/content/contributor/content/practice/snapshots.py).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.decentraland.org/contributor/contributor-zh/nei-rong/shi-jian/python-examples.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
