Python urllib2 implementing a Get POST request for HTTPpython can generally use the module URLLIB2 to implement the Get post HTTP requestGET Request
Import Os,sys
Import Os,sys
Import Urllib2
Http_str = ' http://127.0.0.1:12345/apps/' + serviceline + '/clusters/' + clustername
url = http_str + '/machine_info '
req = Urllib2. Request (URL) # URL converted to URL to initiate GET request
result = Urllib2.urlopen (req) # initiates get HTTP service
res = Result.read () #把结果通过. Read () function reads
Slave_info = json.loads (res) #把返回结果 (string) is converted to JSON for ease of processing,
Print slave_info[' content ' [' slave '] # printing dict information for JSON strings
post request "POST and get to go to request the difference is that some of the parameters are written inside the program, not in the HTTP request"
Import Os,sys
Import Os,sys
Import Urllib2
Http_str = ' http://127.0.0.1:12345/apps/' + serviceline + '/clusters/' + clustername
URL1 = http_str + '/ops/rpm_update ' # URL part
Params1 = {"app_rpm_id": "1", "rpm_xxx": rpmxxxxx} #参数部分
The params = Json.dumps (params1) # HTTP server requires the incoming params to be a JSON object, so you need to convert it here, and if the HTTP server is not required, you can not convert
Response = Urllib.urlopen (url1,params) #发送POST request Print Response # because the HTTP server has already specified that the result of the return is a JSON object, so there is no need to convert to JSON;
Print Response.read () # reads the result data through read (), and if HTTP is not returning a JSON object, it needs to be converted to a JSON object.
Python urllib2 http GET, POST request differences