Making HEAD/PUT/DELETE requests with urllib2?

P

Phillip B Oldham

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.
 
J

Jeff McNeil

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.
I don't honestly know if there's a way to do it via urllib(2), though.
 
P

Phillip B Oldham

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!
 
D

Diez B. Roggisch

Phillip said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top