more py3 porting

This commit is contained in:
j 2014-09-03 01:09:42 +02:00
parent 8e27b9f76e
commit de68f4c4c4
7 changed files with 21 additions and 15 deletions

View file

@ -39,7 +39,7 @@ def find(data):
if 'group' in q: if 'group' in q:
names = {} names = {}
groups = {} groups = {}
key = 'group:' + hashlib.sha1(json.dumps(data)).hexdigest() key = 'group:' + hashlib.sha1(json.dumps(data).encode('utf-8')).hexdigest()
g = state.cache.get(key) g = state.cache.get(key)
if g is None: if g is None:
items = [i.id for i in q['qs'].options(load_only('id'))] items = [i.id for i in q['qs'].options(load_only('id'))]

View file

@ -9,7 +9,7 @@ import os
import shutil import shutil
from glob import glob from glob import glob
#from pyPdf import PdfFileReader from pyPdf import PdfFileReader
import stdnum.isbn import stdnum.isbn
import settings import settings

View file

@ -2,17 +2,15 @@
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
import os
from tornado.web import Application from tornado.web import Application
from tornado.httpserver import HTTPServer from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop from tornado.ioloop import IOLoop
from . import oxtornado import oxtornado
from .oxtornado import actions from oxtornado import actions
from . import meta import meta
from . import utils import utils
import logging import logging
logger = logging.getLogger('metaoml') logger = logging.getLogger('metaoml')

View file

@ -67,7 +67,7 @@ class NodeHandler(tornado.web.RequestHandler):
content = {'status': 'not peered'} content = {'status': 'not peered'}
logger.debug('PEER %s IS UNKNOWN SEND 403', key) logger.debug('PEER %s IS UNKNOWN SEND 403', key)
self.set_status(403) self.set_status(403)
content = json.dumps(content) content = json.dumps(content).encode('utf-8')
sig = settings.sk.sign(content, encoding='base64') sig = settings.sk.sign(content, encoding='base64')
self.set_header('X-Ed25519-Signature', sig) self.set_header('X-Ed25519-Signature', sig)
self.set_header('X-Node-Protocol', settings.NODE_PROTOCOL) self.set_header('X-Node-Protocol', settings.NODE_PROTOCOL)

View file

@ -36,6 +36,7 @@ ENCODING='base64'
class Node(Thread): class Node(Thread):
_running = True _running = True
_cert = None _cert = None
host = None
online = False online = False
download_speed = 0 download_speed = 0
TIMEOUT = 5 TIMEOUT = 5
@ -124,7 +125,7 @@ class Node(Thread):
logger.debug('unable to find host %s', self.user_id) logger.debug('unable to find host %s', self.user_id)
self.online = False self.online = False
return None return None
content = json.dumps([action, args]) content = json.dumps([action, args]).encode('utf-8')
sig = settings.sk.sign(content, encoding=ENCODING) sig = settings.sk.sign(content, encoding=ENCODING)
headers = { headers = {
'User-Agent': settings.USER_AGENT, 'User-Agent': settings.USER_AGENT,
@ -137,7 +138,8 @@ class Node(Thread):
} }
self._opener.addheaders = list(zip(list(headers.keys()), list(headers.values()))) self._opener.addheaders = list(zip(list(headers.keys()), list(headers.values())))
try: try:
r = self._opener.open(url, data=content, timeout=self.TIMEOUT) self._opener.timeout = self.TIMEOUT
r = self._opener.open(url, data=content)
except urllib.error.HTTPError as e: except urllib.error.HTTPError as e:
if e.code == 403: if e.code == 403:
logger.debug('REMOTE ENDED PEERING') logger.debug('REMOTE ENDED PEERING')
@ -172,7 +174,7 @@ class Node(Thread):
sig = r.headers.get('X-Ed25519-Signature') sig = r.headers.get('X-Ed25519-Signature')
if sig and self._valid(data, sig): if sig and self._valid(data, sig):
response = json.loads(data) response = json.loads(data.decode('utf-8'))
else: else:
logger.debug('invalid signature %s', data) logger.debug('invalid signature %s', data)
response = None response = None
@ -202,7 +204,8 @@ class Node(Thread):
'Accept-Encoding': 'gzip', 'Accept-Encoding': 'gzip',
} }
self._opener.addheaders = list(zip(list(headers.keys()), list(headers.values()))) self._opener.addheaders = list(zip(list(headers.keys()), list(headers.values())))
r = self._opener.open(url, timeout=1) self._opener.timeout = 1
r = self._opener.open(url)
version = r.headers.get('X-Node-Protocol', None) version = r.headers.get('X-Node-Protocol', None)
if version != settings.NODE_PROTOCOL: if version != settings.NODE_PROTOCOL:
logger.debug('version does not match local: %s remote %s', settings.NODE_PROTOCOL, version) logger.debug('version does not match local: %s remote %s', settings.NODE_PROTOCOL, version)
@ -298,7 +301,8 @@ class Node(Thread):
t1 = datetime.utcnow() t1 = datetime.utcnow()
logger.debug('download %s', url) logger.debug('download %s', url)
self._opener.addheaders = list(zip(list(headers.keys()), list(headers.values()))) self._opener.addheaders = list(zip(list(headers.keys()), list(headers.values())))
r = self._opener.open(url, timeout=self.TIMEOUT*2) self._opener.timeout = self.TIMEOUT*2
r = self._opener.open(url)
if r.getcode() == 200: if r.getcode() == 200:
if r.headers.get('content-encoding', None) == 'gzip': if r.headers.get('content-encoding', None) == 'gzip':
content = gzip.GzipFile(fileobj=r).read() content = gzip.GzipFile(fileobj=r).read()

View file

@ -53,6 +53,10 @@ class VerifiedHTTPSHandler(urllib.request.HTTPSHandler):
def http_class_wrapper(host, **kwargs): def http_class_wrapper(host, **kwargs):
full_kwargs = dict(self._connection_args) full_kwargs = dict(self._connection_args)
full_kwargs.update(kwargs) full_kwargs.update(kwargs)
print(self._connection_args)
print(kwargs)
if 'timeout' in full_kwargs:
del full_kwargs['timeout']
return CertValidatingHTTPSConnection(host, **full_kwargs) return CertValidatingHTTPSConnection(host, **full_kwargs)
try: try:

View file

@ -215,7 +215,7 @@ def remove_empty_tree(leaf):
else: else:
break break
utc_0 = int(time.mktime(datetime(1970, 0o1, 0o1).timetuple())) utc_0 = int(time.mktime(datetime(1970, 1, 1).timetuple()))
def datetime2ts(dt): def datetime2ts(dt):
return int(time.mktime(dt.utctimetuple())) - utc_0 return int(time.mktime(dt.utctimetuple())) - utc_0