更新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)
1 comment
如果提示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)