- Python - 网络编程
- Python - 网络介绍
- Python - 网络环境
- Python - Internet协议
- Python - IP地址
- Python - DNS查找
- Python - 路由
- Python - HTTP请求
- Python - HTTP响应
- Python - HTTP标头
- Python - 自定义HTTP请求
- Python - 请求状态代码
- Python - HTTP身份验证
- Python - HTTP数据下载
- Python - 连接重用
- Python - 网络接口
- Python - 套接字编程
- Python - HTTP客户端
- Python - HTTP服务器
- Python - 生成URL
- Python - Web窗体提交
- Python - 数据库和SQL
- Python - Telnet
- Python - 电子邮件消息
- Python - SMTP
- Python - POP3
- Python - IMAP
- Python - SSH
- Python - FTP
- Python - SFTP
- Python - Web服务器
- Python - 上传数据
- Python - 代理服务器
- Python - 目录列表
- Python - 远程过程调用
- Python - RPC JSON服务器
- Python - Google地图
- Python - RSS提要
Python - 代理服务器
代理服务器用于通过另一台服务器浏览某些网站,以保持匿名的浏览状态。也可用于绕过对特定IP地址的封锁。
我们可以使用urllib模块中的urlopen方法来访问网站,方法是将代理服务器地址作为参数传递。
示例
在以下示例中,我们使用代理地址访问网站twitter.com以实现匿名访问。OK响应状态证明通过代理服务器成功访问了该网站。
import urllib URL = 'https://www.twitter.com' PROXY_ADDRESS = "265.24.11.6:8080" 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: 145960 content-type: text/html;charset=utf-8 date: Mon, 02 Jul 2018 02:06:19 GMT expires: Tue, 31 Mar 1981 05:00:00 GMT last-modified: Mon, 02 Jul 2018 02:06:19 GMT pragma: no-cache server: tsa_n set-cookie: fm=0; Expires=Mon, 02 Jul 2018 02:06:10 GMT; Path=/; Domain=.twitter.com; Secure; HTTPOnly set-cookie: _twitter_sess=BAh7CSIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%250ASGFzaHsABjoKQHVzZWR7ADoPY3JlYXRlZF9hdGwrCAifvVhkAToMY3NyZl9p%250AZCIlNzlhY2ZhMzdmNGFkYTU0ZTRmMDkxODRhNWNiYzI0MDI6B2lkIiUyZTgy%250AOTAyYjY4NTBkMzE3YzNjYTQwNzZjZDhhYjZhMQ%253D%253D--6807256d74a01129a7b0dcf383f56f169fb8a66c; Path=/; Domain=.twitter.com; Secure; HTTPOnly set-cookie: personalization_id="v1_iDacJdPWUx3e81f8UErWTA=="; Expires=Wed, 01 Jul 2020 02:06:19 GMT; Path=/; Domain=.twitter.com set-cookie: guest_id=v1%3A153049717939764273; Expires=Wed, 01 Jul 2020 02:06:19 GMT; Path=/; Domain=.twitter.com set-cookie: ct0=50c8b59b09322825cd750ea082e43bdc; Expires=Mon, 02 Jul 2018 08:06:19 GMT; Path=/; Domain=.twitter.com; Secure status: 200 OK strict-transport-security: max-age=631138519 x-connection-hash: 89dfbab81abc35dd8f618509c6171bd3 x-content-type-options: nosniff x-frame-options: SAMEORIGIN x-response-time: 312 x-transaction: 007b998000f86eaf x-twitter-response-tags: BouncerCompliant x-ua-compatible: IE=edge,chrome=1 x-xss-protection: 1; mode=block; report=https://twitter.com/i/xss_report
广告