Created
May 17, 2012 02:26
-
-
Save logic/2715756 to your computer and use it in GitHub Desktop.
Adding an HTTP method to urllib2.Request.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2 | |
class MethodRequest(urllib2.Request): | |
def __init__(self, *args, **kwargs): | |
if 'method' in kwargs: | |
self._method = kwargs['method'] | |
del kwargs['method'] | |
else: | |
self._method = None | |
return urllib2.Request.__init__(self, *args, **kwargs) | |
def get_method(self, *args, **kwargs): | |
if self._method is not None: | |
return self._method | |
return urllib2.Request.get_method(self, *args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This addresses a small deficiency in
urllib2
: you can't specify an HTTP method to use, you can only say whether the request is aGET
(by not specifying any data to send) or aPOST
(by specifying data). You can use this just likeurllib2.Request
, but you can also instantiate it with:Now, when the actual request is made, the
PUT
HTTP method will be used, regardless of whether any data is supplied or not.