Python requests socks代理

更新requests 为支持socks的版本:

pip install -U requests[socks]

测试代码:

import requests

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
}

proxies = {
    #'socks5': '127.0.0.1:8889'
    'http':'socks5://127.0.0.1:8889',
    'https':'socks5://127.0.0.1:8889'
}
url = 'http://nkm3s8.xyz/'
try:
    print('Proxy:')
    res = requests.get(url, headers=headers, proxies=proxies, timeout=10)
    print(res)
except Exception as e:
    print(e)

try:
    print('No Proxy:')
    res = requests.get(url, headers=headers, timeout=10)
    print(res)
except Exception as e:
    print(e)

☆版权☆

* 网站名称:obaby@mars
* 网址:https://oba.by/
* 个性:https://oba.by/
* 本文标题: 《Python requests socks代理》
* 本文链接:https://www.zhongxiaojie.cn/2020/10/7681
* 短链接:https://oba.by/?p=7681
* 转载文章请标明文章来源,原文标题以及原文链接。请遵从 《署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN) 》许可协议。


You may also like

1 comment

  1. 公主 Queen 
    Google Chrome 91 Google Chrome 91 Mac OS X 10.15 Mac OS X 10.15 cn中国–山东–济南 移动

    如果提示zsh: no matches found: requests[socks],运行pip install -U “requests[socks]” 即可。

    (venv) (base) zhongming@ZhongMingdeMacBook-Pro meitulu % pip install -U requests[socks]
    zsh: no matches found: requests[socks]
    (venv) (base) zhongming@ZhongMingdeMacBook-Pro meitulu % pip install -U “requests[socks]”
    Requirement already satisfied: requests[socks] in ./venv/lib/python3.8/site-packages (2.25.1)
    Requirement already satisfied: urllib3<1.27,>=1.21.1 in ./venv/lib/python3.8/site-packages (from requests[socks]) (1.26.5)
    Requirement already satisfied: certifi>=2017.4.17 in ./venv/lib/python3.8/site-packages (from requests[socks]) (2021.5.30)
    Requirement already satisfied: chardet<5,>=3.0.2 in ./venv/lib/python3.8/site-packages (from requests[socks]) (4.0.0)
    Requirement already satisfied: idna<3,>=2.5 in ./venv/lib/python3.8/site-packages (from requests[socks]) (2.10)
    Requirement already satisfied: PySocks!=1.5.7,>=1.5.6 in ./venv/lib/python3.8/site-packages (from requests[socks]) (1.7.1)

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注