代理服务器用于通过另一台服务器浏览到某些网站,以便浏览保持匿名。它也可以用来绕过特定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列出目录