Mercurial > hg > AuthRPC
diff wibble/client/ServerProxy.py @ 8:685479d1f0a7
Tests available for both py2 and py3
author | Ben Croston <ben@croston.org> |
---|---|
date | Sun, 04 Sep 2011 23:37:41 +0100 |
parents | 3a6f3193cc7d |
children |
line wrap: on
line diff
--- a/wibble/client/ServerProxy.py Thu Sep 01 19:19:03 2011 +0100 +++ b/wibble/client/ServerProxy.py Sun Sep 04 23:37:41 2011 +0100 @@ -27,6 +27,12 @@ import copy import socket import hashlib +import platform + +if platform.python_version().startswith('3'): + IS_PY3 = True +else: + IS_PY3 = False class _Method(object): def __init__(self, call, name, username=None, password=None): @@ -55,7 +61,10 @@ if self._username is not None: request['username'] = self._username if self._password is not None: - request['password'] = hashlib.md5(self._password).hexdigest() + if IS_PY3: + request['password'] = hashlib.md5(self._password.encode()).hexdigest() + else: + request['password'] = hashlib.md5(self._password).hexdigest() resp = self.call(json.dumps(request)) if resp is not None and resp['error'] is None and resp['id'] == request['id']: @@ -144,7 +153,10 @@ except socket.error: raise NetworkSocketException if response.status == 200: - return json.loads(response.read()) + if IS_PY3: + return json.loads(response.read().decode()) + else: + return json.loads(response.read()) elif response.status == 400: raise BadRequestException elif response.status == 401: @@ -154,7 +166,10 @@ elif response.status == 404: raise NotFoundException elif response.status == 500: - msg = json.loads(response.read()) + if IS_PY3: + msg = json.loads(response.read().decode()) + else: + msg = json.loads(response.read()) raise Exception('JSONRPCError\n%s'%msg['error']['error']) elif response.status == 502: raise BadGatewayException @@ -173,20 +188,3 @@ # magic method dispatcher return _Method(self.__request, name, self._username, self._password) -if __name__ == '__main__': - ##### btc fixme - jsonrpc_client = ServerProxy('http://localhost:1337/', username='testuser', password='', user_agent='Py2NotInternetExploiter') - #jsonrpc_client = ServerProxy('https://www.croston.org/test/index.py', - # username='testuser', - # password='', - # user_agent='Py2NotInternetExploiter') - assert jsonrpc_client.api.mymethod() == jsonrpc_client.mymethod() - try: - print jsonrpc_client.wibble('this should fail') - except BadRequestException: - pass # test passed - else: - raise Exception('Test failed (calling unknown method)') - - print 'All tests passed' -