[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1024
  • Last Modified:

urllib2 info()

Sorry if this is a stupid question, but I am a bit stuck.  I am using info() from urllib2.  It returns the metadata of the url.  I would like to print 'some' of this info to the screen, but I can't seem to get just what I want.  I would like to avoid re, but am prepared to use it if needed.

For example.  Calling info() on cnn.com produces this output:

ate: Sat, 04 Nov 2006 00:01:09 GMT
Server: Apache
Vary: Accept-Encoding,User-Agent
Cache-Control: max-age=60, private
Expires: Sat, 04 Nov 2006 00:02:01 GMT
Content-Type: text/html
Content-Length: 103933
Connection: close

How would I just get parts of it?  For example, just the Server and content-type.  Or the content length, expiration and ate?  I believe that info() returns a dict, but I am not sure.

Thanks,
Brian
0
bnblazer
Asked:
bnblazer
1 Solution
 
efnCommented:
info() actually returns an httplib.HTTPMessage.

http://doc.astro-wise.org/httplib.html#HTTPMessage

You can use the get function of the HTTPMessage to get any header by name.

>>> import urllib2
>>> response = urllib2.urlopen("http://www.experts-exchange.com/Programming/Programming_Languages/Python/Q_22048712.html")
>>> info = response.info()
>>> info.get("Server")
'Apache-Coyote/1.1'
0
 
bnblazerAuthor Commented:
Thank you!

Brian
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now