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
  code isnt working. nezercat33 1 613 Mar-14-2025, 03:45 AM
Last Post: deanhystad
  Simple code not working properly tmv 2 484 Feb-28-2025, 09:27 PM
Last Post: deanhystad
  I'm new to Python - can someone help with this code as it is not working? lminc123 1 504 Feb-13-2025, 06:13 PM
Last Post: lminc123
  multithreading xlwings error Equivocal 0 360 Jan-25-2025, 05:10 PM
Last Post: Equivocal
  How can I make this code more efficient and process faster? steven_ximen 0 368 Dec-17-2024, 04:27 PM
Last Post: steven_ximen
  my code is not working erTurko 1 646 Nov-11-2024, 08:43 AM
Last Post: buran
  Coding help required in Python using VS Code KakashiSenpai 3 1,097 Jul-04-2024, 12:32 PM
Last Post: jefsummers
  Different code execution times Wirbelwind94 4 1,691 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 2,307 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 3,140 Jul-18-2023, 07:16 AM
Last Post: ThomasDC

Forum Jump:

User Panel Messages

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