Python 代理服务器

Python 代理服务器

代理服务器用于通过另一台服务器浏览到某些网站,以便浏览保持匿名。它也可以用来绕过特定IP地址的阻止。

我们通过传递代理服务器地址作为参数,使用urllib模块中的urlopen方法访问网站。

示例在下面的示例中,使用代理地址访问网站twitter.com进行匿名访问。OK的响应状态证明已成功通过代理服务器访问。

 
# Filename : example.py
# Copyright : 2020 By Codebaoku
# Author by : www.yapf.com
# Date : 2020-08-25
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
  
 

当运行上面的程序时,得到类似以下输出:

 
# Filename : example.py
# Copyright : 2020 By Codebaoku
# Author by : www.yapf.com
# Date : 2020-08-25
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 网络编程

相关文章