Skip to content
Snippets Groups Projects
Commit 94d1b721 authored by Pavel Kácha's avatar Pavel Kácha
Browse files

Python 3 compatibility - imports, str decodes, iterators (thx to Radko Krkos)

parent 808cdaec
No related branches found
No related tags found
No related merge requests found
......@@ -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",
......
......@@ -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))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment