How to make HTTP DELETE method using urllib2?

前端 未结 5 1069
忘掉有多难
忘掉有多难 2020-12-05 03:49

Does urllib2 support DELETE or PUT method? If yes provide with any example please. I need to use piston API.

5条回答
  •  离开以前
    2020-12-05 04:44

    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)
    

提交回复
热议问题