Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the server multi-threaded #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions src/sftpserver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import argparse
import sys
import textwrap
import threading

import paramiko

Expand All @@ -38,6 +39,20 @@
BACKLOG = 10


def socket_thread_loop(conn, keyfile):
host_key = paramiko.RSAKey.from_private_key_file(keyfile)
transport = paramiko.Transport(conn)
transport.add_server_key(host_key)
transport.set_subsystem_handler(
'sftp', paramiko.SFTPServer, StubSFTPServer)

server = StubServer()
transport.start_server(server=server)

channel = transport.accept()
while transport.is_active():
time.sleep(1)

def start_server(host, port, keyfile, level):
paramiko_level = getattr(paramiko.common, level)
paramiko.common.logging.basicConfig(level=paramiko_level)
Expand All @@ -48,20 +63,18 @@ def start_server(host, port, keyfile, level):
server_socket.listen(BACKLOG)

while True:
conn, addr = server_socket.accept()

host_key = paramiko.RSAKey.from_private_key_file(keyfile)
transport = paramiko.Transport(conn)
transport.add_server_key(host_key)
transport.set_subsystem_handler(
'sftp', paramiko.SFTPServer, StubSFTPServer)

server = StubServer()
transport.start_server(server=server)

channel = transport.accept()
while transport.is_active():
time.sleep(1)
try:
conn, addr = server_socket.accept()
except OSError:
pass

else:
new_thread = threading.Thread(
target=socket_thread_loop,
args=(conn, keyfile),
daemon=True,
)
new_thread.start()


def main():
Expand Down