info()返回页面信息
import urllib2 from urllib2 import Request, urlopen, URLError req = urllib2.Request(‘http://www.python.org‘) try: response = urlopen(req) except URLError,e: if hasattr(e,‘reason‘): print ‘We failed to reach a server.‘ print ‘Reason‘, e.reason elif hasattr(e, ‘code‘): print ‘The server couldn\‘t fulfill the request.‘ print ‘Error code: ‘, e.code else: print ‘dragonball!‘ print response.info() print response.geturl()
打印结果:
dragonball!
Date: Tue, 08 Jul 2014 13:29:38 GMT
Server: nginx
Content-Type: text/html; charset=utf-8
X-Frame-Options: SAMEORIGIN
Content-Length: 45646
Accept-Ranges: bytes
Via: 1.1 varnish
Age: 3111
X-Served-By: cache-ty66-TYO
X-Cache: HIT
X-Cache-Hits: 70
Vary: Cookie
Strict-Transport-Security: max-age=63072000; includeSubDomains
Connection: close
https://www.python.org/
时间: 2024-10-13 00:37:04