> 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-ko/content/practice/python-examples.md).

# Python 예제

이 실습에서는 다음을 사용하여 일부 콘텐츠를 다운로드하고 분석하는 간단한 프로그램을 작성하는 방법을 보여줍니다. [스냅샷](https://github.com/decentraland/docs/blob/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 Foundation의 서버에서 `peer.decentraland.org`, 그리고 Python 3를 기본 언어로 사용할 것입니다.

우리가 할 일은 다음과 같습니다:

1. 콘텐츠 서버의 상태를 조회합니다.
2. 선택하고 다운로드할 [스냅샷](https://github.com/decentraland/docs/blob/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("서버가 정상적이지 않습니다!")
    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("잘못된 스냅샷 헤더: " + 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 dynamically by asking a question.

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

```
GET https://docs.decentraland.org/contributor/contributor-ko/content/practice/python-examples.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
