python3 fixes

This commit is contained in:
j 2017-06-15 08:58:07 +02:00
parent 49a30d0c43
commit d222dff214
2 changed files with 22 additions and 18 deletions

View File

@ -30,16 +30,16 @@ class DistributedClient:
def status(self, oshash, status):
url = '%s/status/%s' % (self.url, oshash)
requests.post(url, {'error': status})
def upload(self, oshash, path):
url = '%s/upload/%s' % (self.url, oshash)
with open(path) as f:
requests.put(url, f)
def next(self):
url = '%s/next' % self.url
r = requests.get(url)
data = json.loads(r.content)
data = r.json()
if 'oshash' in data:
self.encode(data['oshash'], data['cmd'], data['output'])
return True
@ -59,7 +59,7 @@ class DistributedClient:
n = 0
while True:
r = p.poll()
if r == None:
if r is None:
if n % 60 == 0:
self.ping(oshash)
n = 0
@ -95,6 +95,7 @@ class DistributedClient:
else:
new = True
if __name__ == '__main__':
url = 'http://127.0.0.1:8789'
if len(sys.args) == 0:

View File

@ -6,7 +6,10 @@ import os
import json
import shutil
import time
import thread
try:
import _thread as thread
except:
import thread
from Queue import Queue
from threading import Thread
@ -96,12 +99,12 @@ class Server(Resource):
def render_json(self, request, response):
request.setHeader('Content-Type', 'application/json')
return json.dumps(response, indent=2)
return json.dumps(response, indent=2).encode()
def getChild(self, name, request):
# make source media available via oshash
if request.path.startswith('/get/'):
oshash = request.path.split('/')[-1]
if request.path.startswith(b'/get/'):
oshash = request.path.decode().split('/')[-1]
for path in self.client.path(oshash):
if os.path.exists(path):
f = File(path, 'application/octet-stream')
@ -110,8 +113,8 @@ class Server(Resource):
return self
def render_PUT(self, request):
if request.path.startswith('/upload'):
parts = request.path.split('/')
if request.path.startswith(b'/upload'):
parts = request.path.decode().split('/')
oshash = parts[-1]
if len(oshash) == 16:
path = self.media_path(oshash)
@ -129,8 +132,8 @@ class Server(Resource):
return '404 unkown location'
def render_POST(self, request):
if request.path.startswith('/status'):
oshash = request.path.split('/')[-1]
if request.path.startswith(b'/status'):
oshash = request.path.decode().split('/')[-1]
error = request.args['error']
self.update_status(oshash, 'failed')
return self.render_json(request, {})
@ -138,7 +141,7 @@ class Server(Resource):
return '404 unkown location'
def render_GET(self, request):
if request.path.startswith('/next'):
if request.path.startswith(b'/next'):
response = {}
files = self.queued_encodes()
for oshash in files:
@ -162,17 +165,17 @@ class Server(Resource):
print(oshash, f)
return self.render_json(request, response)
return self.render_json(request, response)
elif request.path.startswith('/ping/'):
parts = request.path.split('/')
elif request.path.startswith(b'/ping/'):
parts = request.path.decode().split('/')
# FIXME: store client id somewhere
client = parts[-1]
oshash = parts[-2]
self.update_status(oshash, 'active')
return self.render_json(request, {})
elif request.path.startswith('/update'):
elif request.path.startswith(b'/update'):
thread.start_new_thread(self.update, ())
return self.render_json(request, {'status': True})
elif request.path.startswith('/status'):
elif request.path.startswith(b'/status'):
queued, offline = self.queued()
return self.render_json(request, {
'active': self.active_encodes(),
@ -180,7 +183,7 @@ class Server(Resource):
'offline': offline
})
request.setHeader('Content-Type', 'text/html')
data = 'pandora_client distributed encoding server'
data = b'pandora_client distributed encoding server'
return data
def update(self):