LoginSignup
0
0

More than 1 year has passed since last update.

【requests】PythonでSocksProxyを使う備忘録

Posted at

SocksProxyを使う備忘録

こちらの記事でsshを介したSocksProxyの実装を紹介しているので合わせてご覧ください。

本記事ではProxyサーバーのアドレスをlocalhost:55557としています。適宜読み替えてください。

import time
import requests

def request(proxies={}):
    time.sleep(1)
    url = 'https://ifconfig.me' # グローバルIPを返してくれるwebサイト
    response = requests.get(url, timeout=5.5, proxies=proxies)
    print(response.text) # => ProxyサーバーのIPアドレス


proxies = {
    'https': 'socks5h://localhost:55557'
}

request(proxies)

requestsへのProxyの渡し方はHTTP Proxyと同様ですが重要なのは辞書のキーとバリュー
'https': socks5h://xxxx.xxx.xxxとすることです。

proxies = {
    'https': 'socks5h://localhost:55557'
}

いつも以下のようにしがちなため、備忘録として残しました。

# だめな例
proxies = {
    'socks5h': 'socks5h://localhost:55557'
}
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