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://h4ck.org.cn/
* 本文标题: 《Python requests socks代理》
* 本文链接:https://h4ck.org.cn/2020/10/python-requests-socks%e4%bb%a3%e7%90%86/
* 转载文章请标明文章来源,原文标题以及原文链接。请遵从 《署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN) 》许可协议。


分享文章:

猜你喜欢:

一条评论

  1. Google Chrome 91 Google Chrome 91 Mac OS X 10.15 Mac OS X 10.15 China山东省 移动 ip address 223.99.*.*

    如果提示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)

发表回复

您的电子邮箱地址不会被公开。