Python equivalent of Curl HTTP post

后端 未结 2 835
长发绾君心
长发绾君心 2020-12-31 02:26

I am posting to Hudson server using curl from the command line using the following--

curl -X POST -d \'4142430A         


        
相关标签:
2条回答
  • 2020-12-31 02:59

    The modern day solution to this is much simpler with the requests module (tagline: HTTP for humans! :)

    import requests
    
    r = requests.post('http://httpbin.org/post', data = {'key':'value'}, auth=('user', 'passwd'))
    r.text      # response as a string
    r.content   # response as a byte string
                #     gzip and deflate transfer-encodings automatically decoded 
    r.json()    # return python object from json! this is what you probably want!
    
    0 讨论(0)
  • 2020-12-31 03:10
    import urllib2
    
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req)
    result = response.read()
    

    where data is the encoded data you want to POST.

    You can encode a dict using urllib like this:

    import urllib
    
    values = { 'foo': 'bar' }
    data = urllib.urlencode(values)
    
    0 讨论(0)
提交回复
热议问题