PhalApiClient.py 618 B

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