пишу простенький sftp сервер на пайтон, появляется ошибка

import paramiko
import os

kast ='>>'
client={}
sftp_client=''
        

file1 = open(f"{os.getcwd()}\\server\\setings.conf", "r")
while True:
    # считываем строку
    line = file1.readline()
    # прерываем цикл, если строка пустая
    if not line:
        break
    # выводим строку
    pers, parametre = line.strip().split('-')
    client[pers] =parametre
# закрываем файл
file1.close
#except:
#    print('eror file 1') 
#    print('возможно в папке сервер оцуцтвует файл setings.conf')  
print(client)
#os.chdir('. . / ')
os.chdir('file')



def config():
    print(f'host ip: {client['host']}')
    print(f'port: {client['port']}')
    print(f'user name: {client['username']}')
    print(f'password: {client['password']}')
config()

 
def create_sftp_client(host, port, username, password):
    host=str(host)
    transport = paramiko.Transport((host, port))
    transport.connect(username=username, password=password)
 
    sftp_client = paramiko.SFTPClient.from_transport(transport)
 
    return sftp_client

def upload_file_to_server(sftp_client, local_file, remote_file):
    sftp_client.put(local_file, remote_file)
    
def download_file_from_server(sftp_client, remote_file, local_file):
    sftp_client.get(remote_file, local_file)

#sftp_client=create_sftp_client(client['host'],client['port'],client['username'],client['password'])
#print(sftp_client)

#ftp_client = create_sftp_client(client['host'],client['port'],client['username'],client['password'])
#upload_file_to_server(sftp_client, file_upload , "remote_file")

#ftp_client = create_sftp_client(client['host'],client['port'],client['username'],client['password'])
#download_file_from_server(sftp_client, "remote_file", file_download)

while True:
    server_command=input(f'sftp server{kast}')
    if server_command == 'exit':
        break
        exit()
    elif server_command =='config':
        config()
    elif server_command.startswith("upload"):
        file_upload = server_command.split("-f")[1]
        if sftp_client == 'NaN':
            print("\033[1;31;40m  error not client \n")
        ftp_client = create_sftp_client(client['host'],client['port'],client['username'],client['password'])
        upload_file_to_server(sftp_client, file_upload , "remote_file")
    elif server_command.startswith("clien"):
        key = server_command.split("-")[1]
        if key =="run":
            sftp_client=create_sftp_client(str(client['host']),str(client['port']),str(client['username']),str(client['password']))
            print(sftp_client)
        elif key == 'close':
            try:
                sftp_client.close()
            except:
                print('error')    
    elif server_command.startswith("download"):
        file_download = server_command.split("-f")[1]
        if sftp_client == 'NaN':
            print("\033[1;31;40m  error not client \n")
        ftp_client = create_sftp_client(client['host'],client['port'],client['username'],client['password'])
        download_file_from_server(sftp_client, "remote_file", file_download)       

пробовал исправить, не получилось:

Traceback (most recent call last): File "c:\Users\User\Desktop\tsp server\server\server.py", line 78, in sftp_client=create_sftp_client(str(client['host']),str(client['port']),str(client['username']),str(client['password'])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\User\Desktop\tsp server\server\server.py", line 40, in create_sftp_client transport = paramiko.Transport((host, port)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\paramiko\transport.py", line 458, in init sock.connect((hostname, port)) TypeError: 'str' object cannot be interpreted as an integer **


Ответы (1 шт):

Автор решения: Валентин Димитерко

Ошибка указывает на то, что порт должен быть целым числом, а не строкой. Ваш код пытается использовать строку в качестве порта.

Ниже приложу исправленный код на всякий:

import paramiko
import os

kast = '>>'
client = {}
sftp_client = None

# Открытие и чтение файла настроек
file_path = os.path.join(os.getcwd(), "server", "setings.conf")
try:
    with open(file_path, "r") as file1:
        while True:
            line = file1.readline()
            if not line:
                break
            pers, parametre = line.strip().split('-')
            client[pers] = parametre
except FileNotFoundError:
    print('Error: settings.conf file not found')
    exit(1)

print(client)

os.chdir('file')

def config():
    print(f'host ip: {client["host"]}')
    print(f'port: {client["port"]}')
    print(f'user name: {client["username"]}')
    print(f'password: {client["password"]}')

config()

def create_sftp_client(host, port, username, password):
    port = int(port)  # Приведение порта к целому числу
    transport = paramiko.Transport((host, port))
    transport.connect(username=username, password=password)
    sftp_client = paramiko.SFTPClient.from_transport(transport)
    return sftp_client

def upload_file_to_server(sftp_client, local_file, remote_file):
    sftp_client.put(local_file, remote_file)

def download_file_from_server(sftp_client, remote_file, local_file):
    sftp_client.get(remote_file, local_file)

while True:
    server_command = input(f'sftp server{kast}')
    if server_command == 'exit':
        break
    elif server_command == 'config':
        config()
    elif server_command.startswith("upload"):
        file_upload = server_command.split("-f")[1]
        if not sftp_client:
            print("\033[1;31;40m  error: no client \n")
        else:
            upload_file_to_server(sftp_client, file_upload, "remote_file")
    elif server_command.startswith("client"):
        key = server_command.split("-")[1]
        if key == "run":
            sftp_client = create_sftp_client(client['host'], client['port'], client['username'], client['password'])
            print(sftp_client)
        elif key == 'close':
            if sftp_client:
                sftp_client.close()
            else:
                print('error: no client to close')
    elif server_command.startswith("download"):
        file_download = server_command.split("-f")[1]
        if not sftp_client:
            print("\033[1;31;40m  error: no client \n")
        else:
            download_file_from_server(sftp_client, "remote_file", file_download)
→ Ссылка