Python startup code at thread completion

I am currently writing a simple file server using Python and the Socket library. I have finished most of all, and now I am writing a way to do an update on the server. Here is what I expect from this:

  • The client must send a new Python file to the server
  • Server deletes file Previous Python file
  • Downloading a Python File Server
  • Server launches new code
  • Server with old code outputs

As of now, my program can do 1-3, but it still cannot run the new code and exit the program. Since the server program has a length of about 100 lines, here is a link to pastebin with code . If you also want to use the bootloader, here .

In my code, the problem is my update:

class updServer(Thread):
    def __init__(self,s):
        Thread.__init__(self)
        self.s = s
    def run(self):
        os.remove(os.path.realpath(__file__))
        fn = self.s.recv(2048)
        with open(fn, 'wb') as f:
            still_data = True
            while still_data:
                data = self.s.recv(2048)
                if not data:
                    still_data = False
                if data == '':
                    still_data = False
                f.write(data)
                f.flush()
                os.fsync(f.fileno())
        f.close()
        global name_of_file
        name_of_file = fn
        return None

, :

elif file_name_sent == b'to update server':
        new_t = updServer(conn)
        threads.append(new_t)
        new_t.start()
        global name_of_file
        execfile(name_of_file)
        os._exit(0)

. os.command subprocess.call, , python PATH. , .

+4

Source: https://habr.com/ru/post/1691147/


All Articles