Solved

urllib2 info()

Posted on 2006-11-03
2
983 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
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 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 …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now