1. 程式人生 > >python之代_理

python之代_理

conn ogr with ur 外網 rar read 代理服 arch tin

1.參考 tor?

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撥號服務器代理

2.urllib2

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()

3.requests

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 requests

default_socket = socket.socket

def 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()

python之代_理