English 中文(简体)
Is there any way to read the header codes without downloading the file at all?
原标题:
 import httplib
 conn = httplib.HTTPConnection(head)
 conn.request("HEAD",tail)
 res = conn.getresponse()
 print res.status

I am currently using this to get the HTTP header code of a file. However, it seems like this code DOWNLOADS the file, and then gets the code.

However, some files are actually video files...and it would be inefficient for my program to download them.

Is there any way to read the header codes without downloading the file at all?

最佳回答

Unfortunately the HEAD HTTP Method like all other HTTP method is just a directive to the server. The HTTP spec says that the server must not return the body in case, but if the server is not implemented or configured correctly then it may return the entire contents of the URL.

There are other factors that may be at play here a proxy server either on your end or on the server end could be caching the content (especially if since it is video) and returning it from a cache. Since the data is coming from a cache full respect of the HTTP specification may be missing.

问题回答

The purpose of the HTTP HEAD command is to return the header information, identical to what you would receive with a GET command, but without the response body (e.g. the video itself). If you are issuing the HEAD command and receiving the full body of the response anyway, that sounds like a problem with the server you re connecting to.

As the others have said, if you re getting more than the headers back with the HEAD command, the target server is misconfigured.

However, a pragmatic solution to your problem is to simply ask for the first N bytes of the file, and then parse those as a header. Alternatively, stream the connection, periodically parsing for a complete header, and then cancel the download once you ve got the header information you need.

Use urllib2.open() and get the headers already parsed for you, along with a file-handle ready to read the rest of the data stream. At that point you close the file and don t fetch anything more.

>>> import urllib2
>>> f = urllib2.urlopen("http://stackoverflow.com/")
>>> for k,v in f.headers.items():
...     print repr(k), "=", repr(v)
... 
 content-length  =  113782 
 expires  =  Tue, 17 Nov 2009 22:12:33 GMT 
 server  =  Microsoft-IIS/7.0 
 connection  =  close 
 cache-control  =  private 
 date  =  Tue, 17 Nov 2009 22:12:33 GMT 
 content-type  =  text/html; charset=utf-8 
>>> f.read(20)
 

<!DOCTYPE HTML P 
>>> 

There should be no reason to generate a HEAD request here.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签