V
V
Viktor2020-01-27 20:08:24
Python
Viktor, 2020-01-27 20:08:24

Working with puthon 3.6 features?

Good day, experts!

I’m just starting to learn python, I’m writing a telegram bot for my needs, and a question arose about functions. I have code:

def solR(status):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    result = sock.connect_ex(('192.168.11.12',3389))
    if result == 0:
        status = 'Удаленка №1 - ОК'
    else:
        status = 'Удаленка №1 - Ошибка'
    sock.close()
    return status
status = ''


calling the result like this:
@bot.message_handler(commands=['Status'])
def welcome_status(message):
    if autor(message.chat.id):
        bot.send_message(message.chat.id, 'Статсус: \n' + solR(status))


Everything is working.
Question:
Is it possible to check for several remote sites (let's say I have 5 of them) in one function, and not in 5, and so that if one of them does not work, I know which one is human - not by ip address?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Elvis, 2020-01-27
@azbukait

def solR(status):
    listip = ['192.168.11.12', '192.168.11.13', '192.168.11.14', '192.168.11.15']
    liststatus = []
    for x, ip in enumerate(listip, 1):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result = sock.connect_ex((ip,3389))
        if result == 0:
            liststatus.append(f'Удаленка №{x} - ОК')
        else:
            liststatus.append(f'Удаленка №{x} - Ошибка')
        sock.close()
    return '\n'.join(liststatus)

then welcome_status will return something like the following line:
Удаленка №1 - Ошибка
Удаленка №2 - Ошибка
Удаленка №3 - ОК
Удаленка №4 - Ошибка

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question