diff --git a/warden3/warden_client/warden_client.py b/warden3/warden_client/warden_client.py index c4d93c6efb522d9cc5ff3302231bc33562083930..da08778d5a74b86f769fc6f8dbac24986d42ba7f 100644 --- a/warden3/warden_client/warden_client.py +++ b/warden3/warden_client/warden_client.py @@ -5,17 +5,25 @@ # Use of this source is governed by a 3-clause BSD-style license, see LICENSE file. import hashlib # Some Python/ssl versions incorrectly initialize hashes, this helps -import json, httplib, ssl, socket, logging, logging.handlers, time -from urlparse import urlparse -from urllib import urlencode -from sys import stderr, exc_info +from sys import stderr, exc_info, version_info +import json, ssl, socket, logging, logging.handlers, time from traceback import format_tb from os import path from operator import itemgetter -from sys import version_info fix_logging_filename = str if version_info<(2, 7) else lambda x: x +if version_info > (3, 0): + import http.client as httplib + from urllib.parse import urlparse + from urllib.parse import urlencode + basestring = str +else: + import httplib + from urlparse import urlparse + from urllib import urlencode + + VERSION = "3.0-beta2" @@ -142,6 +150,8 @@ class Error(Exception): """ In list or iterable context we're empty """ raise StopIteration + __next__ = next + def __bool__(self): """ In boolean context we're never True """ @@ -434,13 +444,13 @@ class Client(object): if res.status==httplib.OK: try: - data = json.loads(response_data) + data = json.loads(response_data.decode("utf-8")) except: data = Error(method=func, message="JSON message parsing failed", exc=exc_info(), response=response_data) else: try: - data = json.loads(response_data) + data = json.loads(response_data.decode("utf-8")) data["errors"] # trigger exception if not dict or no error key except: data = Error(method=func, message="Generic server HTTP error", diff --git a/warden3/warden_client/warden_client_examples.py b/warden3/warden_client/warden_client_examples.py index 840509e165a40fbc3f5fc0cb7789dca160f021f2..4f8a4b7d87ad4a1eae6f5ab0f962569eba05dfc1 100755 --- a/warden3/warden_client/warden_client_examples.py +++ b/warden3/warden_client/warden_client_examples.py @@ -106,7 +106,7 @@ def gen_random_idea(client_name="cz.example.warden.test"): "Size": 46, "Ref": ["cve:CVE-%s-%s" % (randstr(string.digits, 4), randstr())], "ContentEncoding": "base64", - "Content": b64encode(randstr(maxlen=128*1024)) + "Content": b64encode(randstr().encode('ascii')).decode("ascii") } ], "Node": [ @@ -181,7 +181,6 @@ def main(): group = [] nogroup = [] - ret = wclient.getEvents(count=0, id=0, cat=cat, nocat=nocat, tag=tag, notag=notag, group=group, nogroup=nogroup) ret = wclient.getEvents(count=10, cat=cat, nocat=nocat, tag=tag, notag=notag, group=group, nogroup=nogroup) print("Time: %f" % (time()-start)) print("Got %i events" % len(ret))