#!/usr/bin/env python
import urllib2
#代理服务器相关
user='test'
password='123456'
proxyserver='proxy.test.com:'
#要访问的URL为百度首页
url='http://www.baidu.com'
proxy='http://%s:%s@%s' %(user,password,proxyserver)
proxy_handler=urllib2.ProxyHandler({'http':proxy})
#创建opener
opener=urllib2.build_opener(proxy_handler,urllib2.HTTPHandler)
try:
#使用创建的opener访问URL
response=opener.open(url,timeout=3)
#输出页面 status code
print response.code
#输出页面内容
print response.read().decode('gb2312')
#异常处理
except urllib2.HTTPError,e:
print 'The server couldn\'t fulfill the request.'
print 'Error code:',e.code
except urllib2.URLError,e:
print 'We failed to open the URL:%s' %(url)
print 'Reason:',e.reason
转载于:https://blog.51cto.com/mayulin/543559