Solved

urllib2 info()

Posted on 2006-11-03
2
991 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
2 Comments
 
LVL 15

Accepted Solution

by:
efn earned 250 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Updating urls.py doesn't work 4 86
Macports Import Problem 4 65
linux date/time setting via python 6 68
python - user input - set 4 48
Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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 …

726 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