Making HEAD/PUT/DELETE requests with urllib2?

Jeff McNeil jeff at jmcneil.net
Fri Jun 13 20:00:08 EDT 2008


The only time I've ever pulled a HEAD request I've used the httplib
module directly. Ought to be able to do it like so:

Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import httplib
>>> c = httplib.HTTPConnection('www.google.com')
>>> c.request('HEAD', '/')
>>> r = c.getresponse()
>>> r.getheader('server')
'gws'
>>> r.status
200
>>>

I don't honestly know if there's a way to do it via urllib(2), though.





On Fri, Jun 13, 2008 at 7:40 PM, Phillip B Oldham
<phillip.oldham at gmail.com> wrote:
> In my attempt to learn python in a weekend, I've fallen foul at line
> 10 of my second scripting attempt. Basically I'm writing a simple
> spider, but currently I'm unable to find any documentation on making
> HEAD requests using the urllib2 library to test whether a file exists
> on a remote webserver.
>
> I've checked the docs on urllib2 from docs.python.org, and unless I'm
> missing something there doesn't seem to be a way to do *any* request
> other than a GET and POST.
>
> Surely this can't be correct? If so, we're all going to have a hell of
> a time creating RESTful web apps.
>
> Any help on the matter would be greatly appreciated.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list