Making HEAD/PUT/DELETE requests with urllib2?

Diez B. Roggisch deets at
Sat Jun 14 13:15:00 EDT 2008

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)


More information about the Python-list mailing list