From 8719b84a95805d109e21c20f05a0164315e1b38a Mon Sep 17 00:00:00 2001 From: Vasilii Chernov Date: Wed, 2 Mar 2016 10:26:13 +0100 Subject: Move scripts handing code from py.c to Python wrap --- pyserver/api_server.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'pyserver') diff --git a/pyserver/api_server.py b/pyserver/api_server.py index 18ee1f8..c170a19 100644 --- a/pyserver/api_server.py +++ b/pyserver/api_server.py @@ -1,7 +1,7 @@ import os import sys -import pcipywrap +import pcilib import time import json @@ -471,7 +471,7 @@ class PcilibServerHandler(BaseHTTPRequestHandler): ' command: run_script - Run specified script\n' ' required fields\n' - ' script_name: - script name (with extension)\n' + ' script_name: - script name (without extension)\n' ' value: - input value in json format\n' '\n' @@ -503,9 +503,9 @@ class PcilibServerHandler(BaseHTTPRequestHandler): class ApiServer(MultiThreadedHTTPServer): def __init__(self, device='/dev/fpga0', model=None, adress=('0.0.0.0', 9000)): #redirect logs to exeption - pcipywrap.redirect_logs_to_exeption() + pcilib.redirect_logs_to_exeption() #pass Pcipywrap to to server handler - self.lib = pcipywrap.Pcipywrap(device, model) + self.lib = pcilib.Pcilib(device, model) def handler(*args): PcilibServerHandler(self.lib, *args) MultiThreadedHTTPServer.__init__(self, adress, handler) -- cgit v1.2.3 From 02d0026e2df2ba5c68c0c1a67aec4437c9e8e8f3 Mon Sep 17 00:00:00 2001 From: Vasilii Chernov Date: Wed, 2 Mar 2016 14:38:59 +0100 Subject: Add Python3 support --- pyserver/api_server.py | 53 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 21 deletions(-) (limited to 'pyserver') diff --git a/pyserver/api_server.py b/pyserver/api_server.py index c170a19..736d521 100644 --- a/pyserver/api_server.py +++ b/pyserver/api_server.py @@ -6,10 +6,14 @@ import pcilib import time import json from optparse import OptionParser - from multiprocessing import Process -from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler -from SocketServer import ThreadingMixIn + +if sys.version_info >= (3,0): + from http.server import HTTPServer, BaseHTTPRequestHandler + from socketserver import ThreadingMixIn +else: + from SocketServer import ThreadingMixIn + from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler class MultiThreadedHTTPServer(ThreadingMixIn, HTTPServer): pass @@ -146,8 +150,8 @@ class PcilibServerHandler(BaseHTTPRequestHandler): #parse command arguments and convert them to string reg = str(data.get('reg', None)) bank = data.get('bank', None) - if not bank is None: - bank = str(bank) + if(not bank is None): + bank = str(bank) value = 0 try: @@ -178,10 +182,10 @@ class PcilibServerHandler(BaseHTTPRequestHandler): #parse command arguments and convert them to string reg = str(data.get('reg', None)) - value = str(data.get('value', None)) + value = data.get('value', None) bank = data.get('bank', None) - if not bank is None: - bank = str(bank) + if(not bank is None): + bank = str(bank) try: s.pcilib.write_register(value, reg, bank) @@ -230,10 +234,10 @@ class PcilibServerHandler(BaseHTTPRequestHandler): s.error('message doesnt contains "value" field, ' 'which is required for "set_property" command', data) return - + #parse command arguments and convert them to string prop = str(data.get('prop', None)) - value = str(data.get('value', None)) + value = data.get('value', None) try: s.pcilib.set_property(value, prop) @@ -340,7 +344,7 @@ class PcilibServerHandler(BaseHTTPRequestHandler): return #Success! Create and send reply - if(type(out) == bytearray): + if(type(out) == bytearray or type(out) == bytes): s.send_response(200) s.send_header('content-disposition', 'inline; filename=value') s.send_header('content-type', 'application/octet-stream') @@ -374,13 +378,13 @@ class PcilibServerHandler(BaseHTTPRequestHandler): # #Success! Create and send reply # s.wrapMessageAndSend({'status': 'ok'}, data) - + else: - s.error('command "' + command + '" undefined', data) - return + s.error('command "' + command + '" undefined', data) + return else: - s.error('message doesnt contains "command" field, which is required', data) - return + s.error('message doesnt contains "command" field, which is required', data) + return @@ -481,7 +485,10 @@ class PcilibServerHandler(BaseHTTPRequestHandler): s.send_response(200) s.send_header('content-type', 'text/plain') s.end_headers() - s.wfile.write(usage) + if sys.version_info >= (3,0): + s.wfile.write(bytes(usage, 'UTF-8')) + else: + s.wfile.write(usage) #Send error message with text description def error(s, info, received_message = None): @@ -498,8 +505,12 @@ class PcilibServerHandler(BaseHTTPRequestHandler): s.end_headers() if not received_message is None: message['received_message'] = received_message - s.wfile.write(json.dumps(message)) - + if sys.version_info >= (3,0): + s.wfile.write(bytes(json.dumps(message), 'UTF-8')) + else: + s.wfile.write(json.dumps(message)) + + class ApiServer(MultiThreadedHTTPServer): def __init__(self, device='/dev/fpga0', model=None, adress=('0.0.0.0', 9000)): #redirect logs to exeption @@ -534,7 +545,7 @@ if __name__ == '__main__': #start server httpd = ApiServer(DEVICE, MODEL, (HOST_NAME, PORT_NUMBER)) - print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER) + print(time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)) try: httpd.serve_forever() @@ -543,4 +554,4 @@ if __name__ == '__main__': httpd.server_close() - print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER) + print(time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)) -- cgit v1.2.3