python之代_理

來源:互聯網
上載者:User

標籤:tar   opener   issues   pip   socket   import   解析   tde   href   

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請求,所以SOCKSProxy 伺服器比其他類型的Proxy 伺服器速度要快得多。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 urllib2req = 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 = proxiesprint requests.get(‘http://httpbin.org/ip‘).contentprint requests.get(‘https://httpbin.org/ip‘).contentprint requests.get(‘http://httpbin.org/ip‘, proxies=proxies, timeout=10).contentprint requests.get(‘https://httpbin.org/ip‘, proxies=proxies, timeout=10).content

 

4.更加底層 socket.socket
# pip install requests[socks]import socketimport socksimport 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.socksocketprint ‘proxy:‘get()socket.socket = default_socketprint ‘no proxy:‘get()

 

python之代_理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.