# Python

> Proxyman Automatic Setup으로 Python 스크립트와 서버의 HTTP/HTTPS 트래픽을 캡처하세요. requests, http.client, urllib3, httpx, aiohttp와 함께 사용할 수 있습니다.

## 1. 새로운 자동 설정 방식 (v4.7.0 이상) ✅ {#1-new-automatic-solution-v470-or-later-}

Proxyman v4.7.0 이상에서는 클릭 한 번으로 Python의 HTTP/HTTPS 트래픽을 캡처할 수 있습니다.

* 클릭 한 번으로 설정: HTTP 프록시 설정을 직접 하거나 자체 서명 인증서를 수동으로 신뢰할 필요가 없습니다.
* 여러 Python 라이브러리를 지원합니다: requests, http.client, urllib3, httpx, aiohttp

### 사용 방법: {#how-to-use}

1. Proxyman -> Setup Menu -> Automatic Setup 순서로 엽니다.
2. "Open New Terminal"을 클릭합니다.
3. 필요한 경우 AppleScript 권한 요청을 허용합니다.
4. 새 Terminal 창이 열리면 그곳에서 Python 백엔드 서버를 시작하거나 스크립트를 실행합니다. => Proxyman이 모든 트래픽을 자동으로 캡처합니다.
5. 완료 ✅

![aiohttp와 httplib 같은 Python 라이브러리를 캡처하는 Open New Terminal 버튼이 있는 Proxyman Automatic Setup 창](https://proxyman.com/docs/assets/proxyman-automatic-setup-window.jpg)

_Proxyman Automatic Setup으로 Python 트래픽 캡처_

Automatic Setup 페이지를 확인하세요:

- [Automatic Setup](https://proxyman.com/docs/ko/automatic-setup/automatic-setup)

## 2. 이전 방식 (권장하지 않음) ❌ {#2-old-solution-not-recommended-}

### 1. 스크립트 방식 {#1-script-approach}

1. Python용 인증서를 자동으로 설치하거나 제거하려면 [GitHub 이슈 #1220의 이 스크립트](https://github.com/ProxymanApp/Proxyman/issues/1220#issuecomment-1249090359)를 사용하세요.
2. 스크립트를 바탕화면 폴더에 `script.py`라는 이름으로 저장합니다. 

* 인증서 추가: 

```bash
$ python3 script.py add
```

* 인증서 제거

```
$ python3 script.py remove
```

> **노트:** macOS 12.2 이상에서는 반드시 `python3`를 사용하세요.
>
> [@novitae](https://github.com/novitae)님께 감사드립니다.

### 2. 수동 방식 {#2-manual-approach}

#### Python 환경에 Proxyman 인증서 설치 {#install-the-proxyman-certificate-in-the-python-environment}

macOS의 Python은 기본적으로 Proxyman의 자체 서명 인증서를 신뢰하지 않습니다. 그래서 HTTPS 트래픽을 가로챌 때 SSL 오류가 발생할 수 있습니다. 

Python 스크립트의 HTTPS 트래픽을 가로채려면 `~/.proxyman/proxyman-ca.pem`에 있는 **Proxyman 루트 인증서를 사용하도록 Python에 명시적으로 지정해야 합니다**.

> **노트:** 최신 Proxyman 버전은 인증서를 `~/Library/Application Support/com.proxyman.NSProxy/app-data/proxyman-ca.pem`에 저장합니다. Mac에 `~/.proxyman/proxyman-ca.pem` 파일이 없다면 아래 명령어에서 이 경로를 사용하거나, Certificate 메뉴 -> Export -> Root Certificate as PEM에서 인증서를 내보내세요.

다음 순서를 따르세요:

1. Mac에 Proxyman 인증서를 설치합니다. 이미 설치했다면 이 단계는 건너뛰세요. 아직이라면 [macOS 가이드](https://proxyman.com/docs/ko/debug-devices/macos)를 확인하세요.
2. Terminal 앱에서 다음 명령어를 실행합니다

```bash
$ export SSL_CERT_FILE=~/.proxyman/proxyman-ca.pem
$ export REQUESTS_CA_BUNDLE=~/.proxyman/proxyman-ca.pem
$ echo "export REQUESTS_CA_BUNDLE=~/.proxyman/proxyman-ca.pem" >> ~/.bash_profile ; source ~/.bash_profile
```

3\. 완료.

#### 변경 사항 되돌리기 {#revert-the-change}

Proxyman을 사용하지 않는다면 다음 줄을 주석 처리해서 변경 사항을 되돌리세요:

```bash
# export REQUESTS_CA_BUNDLE=~/.proxyman/proxyman-ca.pem
```

위 줄은 \~/.bash\_profile에 있습니다.

## 3. 문제 해결 {#3-troubleshooting}

### 3.1 Python 코드에서 보내는 HTTP 트래픽을 Proxyman이 캡처하지 못함 {#31-proxyman-could-not-capture-http-traffic-from-my-python-code}

**해결 방법**: [Automatic Setup](https://proxyman.com/docs/ko/automatic-setup/automatic-setup)을 사용하세요.

### 참고 자료 {#reference}

* [https://github.com/ProxymanApp/Proxyman/issues/948#issuecomment-890520435](https://github.com/ProxymanApp/Proxyman/issues/948#issuecomment-890520435)
* [https://github.com/ProxymanApp/Proxyman/issues/1220](https://github.com/ProxymanApp/Proxyman/issues/1220)

## 더 알아보기 {#learn-more}

* [Python requests 또는 httpx 라이브러리의 HTTPS 트래픽을 캡처하는 방법](https://proxyman.com/posts/2025-01-29-How-To-Capture-HTTPS-Traffic-From-Python-Requests-httpx)
* [Proxyman으로 Python의 HTTP/HTTPS 트래픽 캡처하기](https://proxyman.com/python) (Proxyman 웹사이트)
