在python中的socket模組使用代理執行個體_python

來源:互聯網
上載者:User

說socket代理之前,先來說說http代理,python的urllib2是內建http代理功能的,可以用如下代碼實現:

複製代碼 代碼如下:

proxy_handler = urllib2.ProxyHandler({'http' : 'http://地址:連接埠'})
opener = urllib2.build_opener(proxy_handler, urllib2.HTTPHandler)
urllib2.install_opener(opener)

如此,接下來用urllib2來urlopen的時候,就會自動使用這個代理了。

但是,在我們瘋狂的天朝,有時候,http代理並不能滿足要求,也許你還想使用更底層的socket代理,於是python裡似乎就沒有直接的方法了(python3不知道有沒有內建,未研究)。這時候,你可能需要這個包:SocksiPy – A Python SOCKS module,下載解壓以後,除了文檔也就一個 socks.py 檔案,很方便使用,給出範例程式碼:

複製代碼 代碼如下:

import socks, socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "地址", 連接埠)
socket.socket = socks.socksocket

接下來,你就可以照常使用socket了,一切都會悄悄地通過代理出去的~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.