Question

i have a ftp server that uses a serve_forever command. this ftp service is called in a thread ,what i want to do is that i want to abruptly close the thread from the main thread ,when i hit the stop button on my GUI.

import os
import sqlite3
from pyftpdlib import ftpserver
def main():
    authorizer = ftpserver.DummyAuthorizer()
    #does something
    address = ('127.0.0.1', 10221)
    ftpd = ftpserver.FTPServer(address, handler)

    # start ftp server
    ftpd.serve_forever()
if __name__ == '__main__':
    main()

this is my main thread which calls the ftp service

def start_ftp(self):
    self.ftp_status.setText("Running")
    self.ftp_status.setStyleSheet("Background : light green")
    #thread.start_new_thread( FtpService )

def stop_ftp(self):
    self.ftp_status.setText("Stopped")
    self.ftp_status.setStyleSheet("Background : red")
    #what should i put here for the desired result

please help guys

Was it helpful?

Solution

According to the documentation, close_all should "Stop serving ...", so serve_forever should return.

Store your ftpd object in a global variable (currently it is local to main), you can call close or close_all from the main thread.


Per your comment, the steps are:

  1. make ftpd = ftpserver.FTPServer(address, handler) refer to a global variable
  2. call ftpd.close_all() when you want to stop serving ftp connections
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top