LoginSignup
0
0

Tor経由でPythonを使う

Last updated at Posted at 2023-11-30

眠いので素のLinuxでやる

インストール

  • Ubuntuであることを前提とする
sudo apt install tor -y
pip install PySocks

起動

  • localhost:9050でバックグラウンド起動させる
tor&

確認

# まじIP
curl -sL ipinfo.io

# Tor経由
curl -sL --socks5 127.0.0.1:9050 ipinfo.io

Python経由で使う

import requests

def get_ip_info(proxy=None):
    url = 'https://ipinfo.io'
    proxies = {'http': proxy, 'https': proxy} if proxy else None
    
    try:
        response = requests.get(url, proxies=proxies)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        print(f"Error: {e}")
        return None

result_without_proxy = get_ip_info()
print(result_without_proxy)

proxy_address = 'socks5://127.0.0.1:9050'
result_with_proxy = get_ip_info(proxy=proxy_address)
print(result_with_proxy)

コンテナ化

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0