2015-12-12 11 views
0

Я пытаюсь написать клиентский веб-сокет python, который может получать созданные пользователем скрипты & выполнить их.Перезагрузка модуля Python на удаленном получателе WebSocket

Мой код выглядит следующим образом до сих пор:

import config 
import websocket 
import time, ssl, json, importlib, sys 
#when putting "import script" here def main is found, but not reloaded later on 

script_loaded = False 

def import_script(ws): #import/reload script module 
    global script_loaded 
    global script 
    try: 
     if script_loaded: 
      importlib.reload(script) 
     else: 
      import script 
      script_loaded = True 
    except Exception as e: 
     iot.report_error(ws, 'Loading Script', str(sys.exc_info()[0]), str(e)) 
     print('Error: ' + str(sys.exc_info()[0]) + ': ' + str(e)) 

class iot: 
    def report_error(ws, place, etype, error): 
     msg = json.dumps({"context": 3, "place": place, "type": etype, "error": error}, sort_keys=True) 
     ws.send(msg) 
    def write(ws, socket, data): 
     msg = json.dumps({"context": 2, "socket": socket, "data": data}, sort_keys=True) 
     ws.send(msg) 
    def display(data): 
     print(data) 
    def start(ws): #start script module (def main) 
     try: 
      script.main(ws) 
     except Exception as e: 
      iot.report_error(ws, 'Executing Script (main())', str(sys.exc_info()[0]), str(e)) 
      print('Error: ' + str(sys.exc_info()[0]) + ': ' + str(e)) 


connectionEstablished = False 
def on_message(ws, message): 
    global connectionEstablished 
    global script_loaded 
    global script 
    try: 
     decoded_data = json.loads(message) 
     if decoded_data['context'] == 0: #Log on 
      connectionEstablished = True 
     elif decoded_data['context'] == 1: #Receive Script 
      if connectionEstablished: 
       file = open('script.py','w') 
       file.write("from iot_daemon import iot\n\n") 
       file.write(decoded_data['script']) #write received script to file (works) 
       import_script(ws) #import/reload script 
       if script_loaded: 
        iot.start(ws) #start script module (def main) 
    except Exception as e: 
     print('Failed to process string: ' + str(e)) 

def on_error(ws, error): 
    print('WebSocket Error: ' + error) 

def on_close(ws): 
    print("Closed.") 

def on_open(ws): 
    print("Connected.") 
    msg = json.dumps({"context": 0, "type": 0, "key": config.key}, sort_keys=True) 
    ws.send(msg) 

if __name__ == "__main__": 
    while True: 
     if config.debugMode: 
      websocket.enableTrace(True) 
     ws = websocket.WebSocketApp(config.serverURL, 
            on_message = on_message, 
            on_error = on_error, 
            on_close = on_close) 
     ws.on_open = on_open 
     ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE}) 
     time.sleep(5) 
     print("Reconnecting...") 

Получая сценарий, кажется, работает (script.py обновляется, когда, взглянув на с текстовым редактором), как-то перезагрузки и запуска новой версии (он всегда запускает версию скрипта, существующую при повторном запуске клиента или вообще не запускает скрипт, если оператор импорта в верхней части скрипта не учитывается).

Любая помощь будет высоко оценена. Заранее спасибо.

ответ

0

Проблема на самом деле была довольно простой: Python не может перезагрузить модуль, который в данный момент открывается как FileObject. Поэтому просто добавление «file.close()» решило его.