PhalApiClient.py 600 B

12345678910111213
  1. #-*- coding:utf-8 -*-
  2. #gaoyiping (iam@gaoyiping.com) 2017-02-18
  3. import json, urllib, urllib2
  4. def PhalApiClient(host, service = None, params = None, timeout = None):
  5. url = host + ('' if service is None else ('?service=' + service))
  6. if params is not None:
  7. assert type(params) is dict, 'params type must be dict'
  8. assert params, 'params must is valid values'
  9. params = urllib.urlencode(params)
  10. request = urllib2.Request(url)
  11. response = urllib2.urlopen(request, data = params, timeout = timeout)
  12. return {'info': response.info(), 'state': response.getcode(), 'data': json.loads(response.read())}