代码之家  ›  专栏  ›  技术社区  ›  Amandasaurus

有什么办法可以把HTTP放到python中吗

  •  206
  • Amandasaurus  · 技术社区  · 17 年前

    我需要使用HTTP将一些数据上载到服务器 PUT 在蟒蛇中。从我对URLLIB2文档的简要阅读来看,它只做HTTP POST . 有什么方法可以做HTTP吗 在蟒蛇?

    11 回复  |  直到 8 年前
        1
  •  289
  •   John Carter    14 年前

    我以前使用过各种各样的python-http-libs,现在我已经解决了 Requests “作为我的最爱。现有的libs有相当有用的接口,但代码可能会因为太长的几行而无法进行简单的操作。基本的输入请求如下:

    payload = {'username': 'bob', 'email': 'bob@bob.com'}
    >>> r = requests.put("http://somedomain.org/endpoint", data=payload)
    

    然后,您可以使用以下选项检查响应状态代码:

    r.status_code
    

    或者回答:

    r.content
    

    请求有很多合成糖和捷径,可以让你的生活更轻松。

        2
  •  238
  •   Florian Bösch    17 年前
    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)
    
        3
  •  45
  •   Spooles    15 年前

    httplib似乎是一个更干净的选择。

    import httplib
    connection =  httplib.HTTPConnection('1.2.3.4:1234')
    body_content = 'BODY CONTENT GOES HERE'
    connection.request('PUT', '/url/path/to/put/to', body_content)
    result = connection.getresponse()
    # Now result.status and result.reason contains interesting stuff
    
        4
  •  8
  •   John Montgomery    17 年前

    你应该看看 httplib module . 它应该允许您进行任何类型的HTTP请求。

        5
  •  8
  •   Mike    17 年前

    我还需要解决这个问题,以便我可以充当RESTfulAPI的客户机。我决定使用httplib2,因为它允许我发送put和delete以及get和post。httplib2不是标准库的一部分,但您可以很容易地从奶酪店买到。

        6
  •  8
  •   radtek    9 年前

    您可以使用请求库,与使用urllib2方法相比,它简化了很多事情。首先从PIP安装:

    pip install requests
    

    更多关于 installing requests .

    然后设置Put请求:

    import requests
    import json
    url = 'https://api.github.com/some/endpoint'
    payload = {'some': 'data'}
    
    # Create your header as required
    headers = {"content-type": "application/json", "Authorization": "<auth-key>" }
    
    r = requests.put(url, data=json.dumps(payload), headers=headers)
    

    quickstart for requests library . 我认为这比URLLIB2简单得多,但确实需要安装和导入这个额外的包。

        7
  •  6
  •   jbochi    14 年前

    我也推荐 httplib2 乔格雷戈里奥。我经常用这个来代替标准库中的httplib。

        8
  •  6
  •   anthony sottile    8 年前

    这在python3中做得更好,并记录在 the stdlib documentation

    这个 urllib.request.Request 获得一班 method=... python3中的参数。

    一些示例用法:

    req = urllib.request.Request('https://example.com/', data=b'DATA!', method='PUT')
    urllib.request.urlopen(req)
    
        9
  •  3
  •   William Keller    17 年前

    你看了吗 put.py ?我以前用过。你也可以用urllib修改你自己的请求。

        10
  •  2
  •   wnoise    17 年前

    当然,您可以在任何级别使用现有的标准库,从套接字到调整urllib。

    http://pycurl.sourceforge.net/

    “pycurl是libcurl的python接口。”

    libcurl是一个免费且易于使用的客户端URL传输库…支持…HTTP PUT

    “pycurl的主要缺点是它是libcurl之上的一个相对较薄的层,没有任何一个漂亮的python类层次结构。这意味着它的学习曲线有点陡峭,除非您已经熟悉libcurl的C API。”

        11
  •  2
  •   Wilfred Hughes AntuanSoft    8 年前

    如果您想保留在标准库中,可以子类 urllib2.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)