Convert a curl POST request to Python only using standard library

前端 未结 4 512
没有蜡笔的小新
没有蜡笔的小新 2020-12-28 16:02

I would like to convert this curl command to something that I can use in Python for an existing script.

curl -u 7898678:X -H \'Content-Type: application/jso         


        
4条回答
  •  长发绾君心
    2020-12-28 16:57

    Thanks every

    this works

    import urllib2
    
    def speak(status):
    
        def basic_authorization(user, password):
            s = user + ":" + password
            return "Basic " + s.encode("base64").rstrip()
    
        req = urllib2.Request("http://example.com/60/speak.json",
                      headers = {
           "Authorization": basic_authorization("2345670", "X"),
         "Content-Type": "application/json",
    
    
            "Accept": "*/*",   
            "User-Agent": "my-python-app/1", 
            },
                          data = '{"message":{"body":'+ status +'}}')
    
        f = urllib2.urlopen(req)
    
    
    speak('Yay')
    

提交回复
热议问题