python 模拟get和post请求:
get请求:
import httplib,json
url = "http://192.168.1.162:34343/metrics"
conn = httplib.HTTPConnection("192.168.1.162","34343")
conn.request("GET",url)
response = conn.getresponse()
res= response.read()
obj=json.loads(res)
应用:计算flumechannel中:EventTakeSuccessCount/EventPutSuccessCount比例。
post请求:
import urllib,httplib
test_data = {'ServiceCode':'aaaa','b':'bbbbb'}
test_data_urlencode = urllib.urlencode(test_data)
requrl = "http://192.168.81.16/cgi-bin/python_test/test.py"
headerdata = {"Host":"192.168.81.16"}
conn = httplib.HTTPConnection("192.168.81.16")
conn.request(method="POST",url=requrl,body=test_data_urlencode,headers = headerdata)
response = conn.getresponse()
res= response.read()
print res
关于json
import json。
json.loads:把Json格式字符串解码转换成Python对象。
json.dumps:把一个Python对象编码转换成Json字符串。