python开发笔记---requests

开发过程中有对于代理的需求,由于先前有过使用代理的经验,使用的是socks模块将模块进行包装以使用代理请求目标地址,由于python模块儿的import机制,当时导致使用socks模块之后,对socket模块进行了修改,导致所有的http请求都会走代理,结果就是大量模块异常,包括代码中的mysqldb,redis,虽然这个问题当时得到了修正,但是仍然觉得还是不太保险,于是在询问爬虫之后得知,他们使用的是requests模块自带的代理功能,于是找文档果然十分开心的找到了相关的文档,首先介绍了http代理的介绍

import requests

proxies = {
‘http’: http://10.10.1.10:3128',
‘https’: http://10.10.1.10:1080',
}

requests.get(http://example.org', proxies=proxies)
然而开发的时候是使用的ssh+autossh的socks5代理,接下来就是

SOCKS

New in version 2.10.0.


In addition to basic HTTP proxies, Requests also supports proxies using the SOCKS protocol. This is an optional feature that requires that additional third-party libraries be installed before use.

You can get the dependencies for this feature from <span class="pre">pip</span>:

$ pip install requests[socks]



Once you’ve installed those dependencies, using a SOCKS proxy is just as easy as using a HTTP one:

proxies = {
‘http’: ‘socks5://user:pass@host:port’,
‘https’: ‘socks5://user:pass@host:port’
}




看到上面pip安装的依赖的时候我就知道。。。终究还是没有逃过socks模块呀~

转载请注明来源链接 http://just4fun.im/2016/12/20/python-e5-bc-80-e5-8f-91-e7-ac-94-e8-ae-b0-requests/ 尊重知识,谢谢:)