Python Forum
Another working code, help required for faster multithreading execution
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Another working code, help required for faster multithreading execution
#1
Hi All,

this code is taking, device ips from file and threads to print output in file. I have replaced multiple spaces with single space. This code is taking around 5min and devices are 450. Is there any tweak in this for faster execution. thanks forum to help me for initial code.

import os
import unidecode
import telnetlib
import time
import re
from time import sleep
start = time.time()
outfile = open('zyxelmac.txt','w')
def open_telnet(ip):
        user = 'admin'
        password = '1234'
        sr_no = 0
        try:
            telnet = telnetlib.Telnet(ip, 23, 2)
            telnet.read_until('User name:', 3)
            telnet.write(user.encode('ascii') + '\r')
            telnet.read_until('Password:', 3)
            telnet.write(password.encode('ascii') + '\r')
            telnet.write('statistics mac 1~48' + '\r\r\r\r')
            telnet.read_until('>')
            telnet.write(' exit''\r')
            output = telnet.read_all()
            sleep(2)
            data = iter(output.split('\n'))

            try:
                        for line in data:
                                if "Port:" in line:
                                        port1 = line.split(':')
                                        port = str(port1[:5])
                                        port2 = port[10:13].strip()
                                        port3 = port2.replace('\\','')
                                        next(data)
                                        next(data)
                                        port_mac = next(data)[1:]
                                        formatted_line = (ip+' '+port3+' '+port_mac)
                                        line = re.sub('\s{2,}', ' ', formatted_line)
                                        outfile.write(line+'\n')
            except StopIteration:
                   pass
        except Exception as excp:
            print('time out \n')
def create_threads():
    threads = []
    with open('zyxel.txt','r') as ipfile:
        for sr_no, line in enumerate(ipfile, start=1):
            ip = line.strip()
            th = threading.Thread(target = open_telnet ,args = (ip,))
            th.start()
            threads.append(th)
        for thr in threads:
            thr.join()

if __name__ == "__main__":
        create_threads()
        print "Exiting the program"
        outfile.close()
        print('It took', time.time()-start, 'seconds.')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Different code execution times Wirbelwind94 4 789 Oct-06-2023, 12:30 PM
Last Post: snippsat
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 984 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 920 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 1,035 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,103 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  faster code for my code kucingkembar 19 3,370 Aug-09-2022, 09:48 AM
Last Post: DPaul
  multithreading Hanyx 4 1,349 Jul-29-2022, 07:28 AM
Last Post: Larz60+
  In consistency in code execution Led_Zeppelin 1 1,135 Jun-27-2022, 03:00 AM
Last Post: deanhystad
  My Code isn't working... End3r 4 1,955 Mar-21-2022, 10:12 AM
Last Post: End3r
Question Problems with variables in multithreading Wombaz 2 1,355 Mar-08-2022, 03:32 PM
Last Post: Wombaz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020