Does urllib2 support DELETE or PUT method? If yes provide with any example please. I need to use piston API.
            Asked
            
        
        
            Active
            
        
            Viewed 4.3k times
        
    51
            
            
         
    
    
        Akshay Patil
        
- 954
- 1
- 12
- 28
 
    
    
        Pol
        
- 24,517
- 28
- 74
- 95
5 Answers
77
            you can do it with httplib:
import httplib 
conn = httplib.HTTPConnection('www.foo.com')
conn.request('PUT', '/myurl', body) 
resp = conn.getresponse()
content = resp.read()
also, check out this question. the accepted answer shows a way to add other methods to urllib2:
import urllib2
opener = urllib2.build_opener(urllib2.HTTPHandler)
request = urllib2.Request('http://example.org', data='your_put_data')
request.add_header('Content-Type', 'your/contenttype')
request.get_method = lambda: 'PUT'
url = opener.open(request)
 
    
    
        Community
        
- 1
- 1
 
    
    
        Corey Goldberg
        
- 59,062
- 28
- 129
- 143
- 
                    It's you! Thanks for the examples using windows monitoring via wmi in Python, those were a great help for me :) – Anders Dec 22 '10 at 17:26
- 
                    1Nice.. httplib does not support authentication? – Pol Dec 22 '10 at 17:38
- 
                    1authentication with urllib2 in case of: `request.add_header( 'Authorization', b'Basic ' + 'user:password'.encode('base64').replace('\n','') )` – NeronLeVelu Jun 02 '17 at 06:50
14
            
            
        Correction for Raj's answer:
import urllib2
class RequestWithMethod(urllib2.Request):
  def __init__(self, *args, **kwargs):
    self._method = kwargs.pop('method', None)
    urllib2.Request.__init__(self, *args, **kwargs)
  def get_method(self):
    return self._method if self._method else super(RequestWithMethod, self).get_method()
 
    
    
        Dave
        
- 3,171
- 1
- 25
- 23
- 
                    2it would be shorter to use `self._method = kwargs.pop('method', None)` – Charles Duffy Oct 31 '11 at 22:24
- 
                    2The use of super gives me a _TypeError_. Instead I used `urllib2.Request.get_method(self)` – Bran Handley Aug 21 '12 at 16:04
11
            
            
        Found following code from https://gist.github.com/kehr/0c282b14bfa35155deff34d3d27f8307 and it worked for me (Python 2.7.5):
import urllib2
request = urllib2.Request(uri, data=data)
request.get_method = lambda: 'DELETE'
response = urllib2.urlopen(request)
 
    
    
        Ali Sadik Kumlali
        
- 631
- 1
- 6
- 14
7
            
            
        You can subclass the urllib2.Request object and override the method when you instantiate the class.
import urllib2
class RequestWithMethod(urllib2.Request):
  def __init__(self, method, *args, **kwargs):
    self._method = method
    urllib2.Request.__init__(*args, **kwargs)
  def get_method(self):
    return self._method
Courtesy of Benjamin Smedberg
 
    
    
        Raj
        
- 3,791
- 5
- 43
- 56
1
            
            
        You can define a subclass of the Request object, and call it as follows:
import urllib2
class RequestWithMethod(urllib2.Request):
    def __init__(self, *args, **kwargs):
        self._method = kwargs.pop('method', None)
        urllib2.Request.__init__(self, *args, **kwargs)
    def get_method(self):
        return self._method if self._method else super(RequestWithMethod, self).get_method()
def put_request(url, data):
    opener = urllib2.build_opener(urllib2.HTTPHandler)
    request = RequestWithMethod(url, method='PUT', data=data)
    return opener.open(request)
def delete_request(url):
    opener = urllib2.build_opener(urllib2.HTTPHandler)
    request = RequestWithMethod(url, method='DELETE')
    return opener.open(request)
(This is similar to the above answers, but shows usage.)
 
    
    
        Wilfred Hughes
        
- 29,846
- 15
- 139
- 192