
正文
python 代理
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
1.参考
http://docs.python-requests.org/en/master/user/advanced/
Using Python’s urllib2 or Requests with a SOCKS5 proxy
Python中Request 使用socks5代理的两种方法(个人推荐方法二)
How to make python Requests work via socks proxy
Connecting to a SOCKS Proxy within Python
Should you want to use the SOCKS proxy only with urllib2 then the wrapmodule can be used. This replaces a module's socket library with a SOCKS socket[2].
v3.2.0版本中新增的socks5代理设置选项是做什么的?
这个前置代理,应该是给 shadowsocks.exe 本身的代理设置,使得 它 自己走某个代理。因为有些公司,内网环境下需要代理才可以访问外网。
http,socks4,socks5代理的区别
HTTP代理:能够代理客户机的HTTP访问,主要是代理浏览器访问网页,它的端口一般为80、8080、3128等;
SOCKS代理:SOCKS代理与其他类型的代理不同,它只是简单地传递数据包,而并不关心是何种应用协议,既可以是HTTP请求,所以SOCKS代理服务器比其他类型的代理服务器速度要快得多。SOCKS代理又分为SOCKS4和SOCKS5,二者不同的是SOCKS4代理只支持TCP协议(即传输控制协议),而SOCKS5代理则既支持TCP协议又支持UDP协议(即用户数据包协议),还支持各种身份验证机制、服务器端域名解析等。SOCK4能做到的SOCKS5都可得到,但SOCKS5能够做到的SOCK4则不一定能做到,比如我们常用的聊天工具QQ在使用代理时就要求用SOCKS5代理,因为它需要使用UDP协议来传输数据
极客学院 Requests 库的使用
10-穿墙代理的设置
1.5.socket代理
参见《python中的socket代理》可知,更底层的socket代理如下所示:
import socks, socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "proxy_host", proxy_port)
socket.socket = socks.socksocket
需要 socks 库。
Python爬虫进阶七之设置ADSL拨号服务器代理
tor?
2.urllib2import urllib2
req = urllib2.Request('http://httpbin.org/ip')
req_https = urllib2.Request('https://httpbin.org/ip')
proxy_http = urllib2.ProxyHandler({'http':'http://127.0.0.1:1080'})
proxy_https = urllib2.ProxyHandler({'https':'https://127.0.0.1:1080'})
opener = urllib2.build_opener(proxy_http, proxy_https)
# urllib2.install_opener(opener)
print urllib2.urlopen(req).read()
print urllib2.urlopen(req_https).read()
print opener.open(req, timeout=10).read()
print opener.open(req_https, timeout=10).read()
3.requestsimport requests
# proxies={'http': 'http://127.0.0.1:1080', 'https': 'http://127.0.0.1:1080'}
proxies={'http': 'socks5://127.0.0.1:1080', 'https': 'socks5://127.0.0.1:1080'}
# s.proxies = proxies
print requests.get('http://httpbin.org/ip').content
print requests.get('https://httpbin.org/ip').content
print requests.get('http://httpbin.org/ip', proxies=proxies, timeout=10).content
print requests.get('https://httpbin.org/ip', proxies=proxies, timeout=10).content
4.更加底层 socket.socket# pip install requests[socks]
import socket
import socks
import requestsdefault_socket = socket.socketdef get():
print urllib2.urlopen('http://httpbin.org/ip', timeout=10).read()
print urllib2.urlopen('https://httpbin.org/ip', timeout=10).read()
print(requests.get('http://httpbin.org/ip', timeout=10).text)
print(requests.get('https://httpbin.org/ip', timeout=10).text)print 'no proxy:'
get()socks.set_default_proxy(socks.SOCKS5, '127.0.0.1', 1080)
socket.socket = socks.socksocket
print 'proxy:'
get()socket.socket = default_socket
print 'no proxy:'
get()
import urllib2
req = urllib2.Request('http://httpbin.org/ip')
req_https = urllib2.Request('https://httpbin.org/ip')
proxy_http = urllib2.ProxyHandler({'http':'http://127.0.0.1:1080'})
proxy_https = urllib2.ProxyHandler({'https':'https://127.0.0.1:1080'})
opener = urllib2.build_opener(proxy_http, proxy_https)
# urllib2.install_opener(opener)
print urllib2.urlopen(req).read()
print urllib2.urlopen(req_https).read()
print opener.open(req, timeout=10).read()
print opener.open(req_https, timeout=10).read()
import requests
# proxies={'http': 'http://127.0.0.1:1080', 'https': 'http://127.0.0.1:1080'}
proxies={'http': 'socks5://127.0.0.1:1080', 'https': 'socks5://127.0.0.1:1080'}
# s.proxies = proxies
print requests.get('http://httpbin.org/ip').content
print requests.get('https://httpbin.org/ip').content
print requests.get('http://httpbin.org/ip', proxies=proxies, timeout=10).content
print requests.get('https://httpbin.org/ip', proxies=proxies, timeout=10).content
4.更加底层 socket.socket# pip install requests[socks]
import socket
import socks
import requestsdefault_socket = socket.socketdef get():
print urllib2.urlopen('http://httpbin.org/ip', timeout=10).read()
print urllib2.urlopen('https://httpbin.org/ip', timeout=10).read()
print(requests.get('http://httpbin.org/ip', timeout=10).text)
print(requests.get('https://httpbin.org/ip', timeout=10).text)print 'no proxy:'
get()socks.set_default_proxy(socks.SOCKS5, '127.0.0.1', 1080)
socket.socket = socks.socksocket
print 'proxy:'
get()socket.socket = default_socket
print 'no proxy:'
get()
# pip install requests[socks]
import socket
import socks
import requestsdefault_socket = socket.socketdef get():
print urllib2.urlopen('http://httpbin.org/ip', timeout=10).read()
print urllib2.urlopen('https://httpbin.org/ip', timeout=10).read()
print(requests.get('http://httpbin.org/ip', timeout=10).text)
print(requests.get('https://httpbin.org/ip', timeout=10).text)print 'no proxy:'
get()socks.set_default_proxy(socks.SOCKS5, '127.0.0.1', 1080)
socket.socket = socks.socksocket
print 'proxy:'
get()socket.socket = default_socket
print 'no proxy:'
get()






