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