By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,820 Members | 1,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,820 IT Pros & Developers. It's quick & easy.

Making HEAD/PUT/DELETE requests with urllib2?

P: n/a
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.
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies

P: n/a
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
<ph************@gmail.comwrote:
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
Jun 27 '08 #2

P: n/a
Thanks for the info. That's working like a charm. Looks as though I'll
be able to handle all request types with that object.

I got a little worried then that the python dev's had missed something
truly important!
Jun 27 '08 #3

P: n/a
Phillip B Oldham schrieb:
Thanks for the info. That's working like a charm. Looks as though I'll
be able to handle all request types with that object.

I got a little worried then that the python dev's had missed something
truly important!
I've done that in urrlib2 like this:

class MyRequest(urllib2.Request):
def get_method(self):
if alternate_http_method is not None:
return alternate_http_method
return urllib2.Request.get_method(self)

THe alternate_http_method is part of a class-closure, but of course you
could do that with an instance variable as well.

I then use it like this:

req = MyRequest()
handlers = []
if USE_PROXY:
handlers.append(urllib2.ProxyHandler({'http' : PROXY}))
req = self._create_request(url, connector, urlparams,
queryparams, alternate_http_method)
opener = urllib2.build_opener(*handlers)

Diez
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.