让requests模块支持scoks代理
首先,我们要安装requesocks这个模块
pip install requesocks
如果你那里不支持pip命令,可以看这里
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2016/3/2 10:24
# @Author : Aries (i@iw3c.com)
# @Site : http://iw3c.com
# @File : zhihu.py
# @Software: PyCharm
USE_SOCKS_PROXY = True
if USE_SOCKS_PROXY:
import requesocks as requests
else:
import requests
session = requests.session()
session.proxies = {'http': 'socks5://127.0.0.1:9050',
'https': 'socks5://127.0.0.1:9050'}
resp = session.get('https://api.github.com', auth=('user', 'pass'))
print(resp.status_code)
print(resp.headers['content-type'])
print(resp.text)