Python 如何使用 urllib2 制作 HTTP DELETE 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4511598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:04:33  来源:igfitidea点击:

How to make HTTP DELETE method using urllib2?

pythonurllib2

提问by Pol

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

是否urllib2支持 DELETE 或 PUT 方法?如果是,请提供任何示例。我需要使用活塞 API。

采纳答案by Corey Goldberg

you can do it with httplib:

你可以用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:

另外,看看这个问题。接受的答案显示了一种向 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)

回答by Raj

You can subclass the urllib2.Request object and override the method when you instantiate the class.

您可以子类化 urllib2.Request 对象并在实例化类时覆盖该方法。

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

Benjamin Smedberg 提供

回答by Dave

Correction for Raj's answer:

更正 Raj 的回答:

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()

回答by Wilfred Hughes

You can define a subclass of the Requestobject, and call it as follows:

您可以定义Request对象的子类,并按如下方式调用它:

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

(这类似于上述答案,但显示了用法。)

回答by Ali Sadik Kumlali

Found following code from https://gist.github.com/kehr/0c282b14bfa35155deff34d3d27f8307and it worked for me (Python 2.7.5):

https://gist.github.com/kehr/0c282b14bfa35155deff34d3d27f8307找到以下代码,它对我有用(Python 2.7.5):

import urllib2

request = urllib2.Request(uri, data=data)
request.get_method = lambda: 'DELETE'
response = urllib2.urlopen(request)