Home - vmvz/PySocks GitHub Wiki
http://zhengheng.me/2016/08/25/python-requests-socks5/
写这个文章只是记录下如何在requests里使用socks5代理。主要是为了使用shadowsocks,其实还有proxychain4可以在命令行下面使用,不过写python脚本的话,还是用requests比较方便。
首先是需要升级(或安装)支持SOCKS协议代理的requests版本:
pip install -U 'requests[socks]'
python脚本里添加proxies参数:
proxies = {'http':'socks5://127.0.0.1:1080', 'https':'socks5://127.0.0.1:1080'}
就可以了:
import requests
requests.get('http://www.google.com', proxies=proxies)
另外提一下如何使用requests下载文件:
req = requests.get(download_url, headers=headers, proxies=proxies, stream=True)
with open(file_path, 'wb') as save_file:
for chunk in req.iter_content(1024):
save_file.write(chunk)
As of requests version 2.10.0, released on 2016-04-29, requests supports SOCKS.
It requires PySocks, which can be installed with pip install pysocks.
Example usage:
import requests proxies = {'http': "socks5://myproxy:9191"} requests.get('http://example.org', proxies=proxies)
http://stackoverflow.com/questions/12601316/how-to-make-python-requests-work-via-socks-proxy