# -*- coding: UTF-8 -*- # coding="utf-8" import httplib2 import json from urllib.parse import urlencode #判断执行Get、还是Post,传:方法,接口url,接口参数 def DoHttpRequest(method,addr,param): Method = method Addr = addr Param = param if Method =="get": Parameter = StringMosaic(Param) Url = Addr + Parameter res = TestHttpGet(Url) else: Parameter = eval(str(Param)) res = TestHttpPost(Addr,Param) return res #字符串转Python字典后拼接参数 def StringMosaic(param): Dicts = eval(param) #转python字典 result = '' for key, value in Dicts.items(): if result !='': result +="&" result += key + "=" + value return result def TestHttpGet(url): urlstr = url h = httplib2.Http('.cache') response,content = h.request(urlstr) jo = json.loads(content.decode('utf-8')) #转Jsonr后返回结果 return jo def TestHttpPost(url,data): urlstr = url param=eval(str(data)) # print(param) h = httplib2.Http('.cache') response,content = h.request(urlstr, 'POST', urlencode(param), headers={'Content-Type': 'application/x-www-form-urlencoded'}) jo = json.loads(content.decode('utf-8')) return jo