Python代理伺服器

代理伺服器用於通過另一臺伺服器流覽到某些網站,以便流覽保持匿名。它也可以用來繞過特定IP地址的阻止。

我們通過傳遞代理伺服器地址作為參數,使用urllib模組中的urlopen方法訪問網站。

示例
在下面的示例中,使用代理地址訪問網站twitter.com進行匿名訪問。OK的回應狀態證明已成功通過代理伺服器訪問。

import urllib
URL = 'http://www.xntutor.com'
PROXY_ADDRESS = "202.54.11.6:8088"
if __name__ == '__main__':
    resp = urllib.urlopen(URL, proxies = {"http" : PROXY_ADDRESS})
print "Proxy server returns response headers: %s " %resp.headers

當運行上面的程式時,得到類似以下輸出-

Proxy server returns response headers: cache-control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
content-length: 185960
content-type: text/html;charset=utf-8
date: Mon, 02 Jul 2019 02:06:19 GMT
expires: Tue, 31 Mar 1981 05:00:00 GMT
last-modified: Mon, 02 Jul 2019 02:06:19 GMT
pragma: no-cache
server: tsa_n
set-cookie: fm=0; Expires=Mon, 02 Jul 2019 02:06:10 GMT; Path=/; Domain=.twitter.com; Secure; HTTPOnly
set-cookie:
................
x-xss-protection: 1; mode=block; report=http://xntutor.com/i/xss_report

上一篇: Python上傳數據 下一篇: Python列出目錄