Skip to content

Instantly share code, notes, and snippets.

@antic183
Last active April 12, 2024 10:55
Show Gist options
  • Save antic183/fe536de268d1aee0aefdc773200a203e to your computer and use it in GitHub Desktop.
Save antic183/fe536de268d1aee0aefdc773200a203e to your computer and use it in GitHub Desktop.
run a simple Python FTP Server on windows as a system service

pip install pywin32

pip install pyftpdlib

replace the path "C:\..wathever...\ftp-public" on ftp.py

python service.py install

python service.py start

python service.py debug

python service.py stop

python service.py remove

from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import ThreadedFTPServer
from pyftpdlib.authorizers import DummyAuthorizer
class FtpServer:
def start(self):
authorizer = DummyAuthorizer()
authorizer.add_user('user', '12345', 'C:\\..wathever...\\ftp-public', perm='elradfmwMT')
handler = FTPHandler
handler.authorizer = authorizer
self.server = ThreadedFTPServer(('', 2121), handler)
self.server.serve_forever()
def stop(self):
self.server.close_all()
import socket
import win32serviceutil
import servicemanager
import win32event
import win32service
import ftp
class SMWinservice(win32serviceutil.ServiceFramework):
_svc_name_ = '_pythonFTPService_'
_svc_display_name_ = '_pythonFTPService_'
_svc_description_ = '_pythonFTPService_ description'
@classmethod
def parse_command_line(cls):
win32serviceutil.HandleCommandLine(cls)
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)
def SvcStop(self):
self.stop()
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
self.start()
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
self.main()
def start(self):
self.myFtpServer = ftp.FtpServer()
pass
def stop(self):
self.myFtpServer.stop()
pass
def main(self):
# BLL
self.myFtpServer.start()
pass
if __name__ == '__main__':
SMWinservice.parse_command_line()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment