[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

urllib2 info()

Posted on 2006-11-03
2
Medium Priority
?
1,015 Views
Last Modified: 2011-09-20
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
Comment
Question by:bnblazer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Accepted Solution

by:
efn earned 1000 total points
ID: 17871695
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
 
LVL 1

Author Comment

by:bnblazer
ID: 17871854
Thank you!

Brian
0

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question