views:

331

answers:

2

I know how to do a HEAD request with httplib, but I have to use mechanize for this site.

Essentially, what I need to do is grab a value from the header (filename) without actually downloading the file.

Any suggestions how I could accomplish this?

+5  A: 

Mechanize itself only sends GETs and POSTs, but you can easily extend the Request class to send HEAD. Example:

import mechanize

class HeadRequest(mechanize.Request):
    def get_method(self):
        return "HEAD"

request = HeadRequest("http://www.example.com/")
response = mechanize.urlopen(request)

print response.info()
Michał Kwiatkowski
how would i use this from an instance of mechanize.Browser?
Jeremy Cantrell
mechanize.Browser().open(request)
Vinko Vrsalovic
ahhh, the docs are a bit misleading. it shows open() as taking a url. i wasn't aware that it would take a request object.
Jeremy Cantrell
A: 

to Michał Kwiatkowski : no need to rewrite init function ?

This is not an answer to the original question; it should be a comment to Michal's question. Please delete this “answer”.
ΤΖΩΤΖΙΟΥ