Python Forum
bytes-like object is required, not 'str
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
bytes-like object is required, not 'str
#1
Hi All,

I am fetching all devices from SQL server, checking there reachability, if devices are not in specific IPNetwork, writing to file. again opening file to check model against each device.

netaddr package takes IP are str and easysnmp IP as bytes. required help to resolve error.

import threading
import MySQLdb
import subprocess
import sys
from netaddr import IPNetwork, IPAddress
from easysnmp import Session
dslamsip = open('dslamip.txt','w')
rechable_dslamsip = open('rechable_dslamsip.txt','w')
#dslams = []
#rechable_dslams = []
switches_network = ['10.104.75.0/24','10.17.100.0/24','10.16.100.0/24','10.14.100.0/24']
def dslam_rechability_check(ip):
                 ping_reply = subprocess.call(['ping', '-c', '2', '-w', '2', '-q', '-n', ip],stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
                 if ping_reply == 0:
                     rechable_dslamsip.write(ip)
                     rechable_dslamsip.write('\n')
###################
def dslam_model(ip):
    community ='public'
    #sysDescr OID
    oid ='SNMPv2-MIB::sysDescr.0'
    try:
           session = Session(hostname=ip, community=community, version=2)
           snmp_get = session.get(oid)
           result = snmp_get.value.encode('ascii')
           if 'IES1248-51' in result:
                 model = 'ZyXEL'
           elif 'ECI' in result:
                 model = 'ECI'
           elif 'VES1724-56' in result:
                 model = 'ZyXEL VDSL'
           else:
                 model = 'UT'
           print('{} {}'.format(ip, model))
    except Exception as excp:
                 print(ip,excp)
def db_connection():
    global dslams
    db = MySQLdb.connect("localhost","abc","xyz","xxxx" )
    # prepare a cursor object using cursor() method
    cursor = db.cursor()

    sql = "SELECT * FROM ttml_ttsl_host_details \
       WHERE access_group =  '%s'" % ('TTML')
    try:
       # Execute the SQL command
         cursor.execute(sql)
         # Fetch all the rows in a list of lists.
         results = cursor.fetchall()
         for row in results:
             ip_addr = row[6]
             dslamsip.write(ip_addr)
             dslamsip.write('\n')
    except:
         print("Error: unable to fecth data")
# disconnect from server
    db.close()
###
def rechability_create_threads():
    threads = []
    with open('dslamip.txt') as ipfile:
        for sr_no, line in enumerate(ipfile, start=1):
             host = line.strip()
             th = threading.Thread(target = dslam_rechability_check ,args = (host,))
             th.start()
             threads.append(th)
        for thr in threads:
             thr.join()
def dslam_model_threads():
        model_threads = []
        with open('rechable_dslamsip.txt') as rechable_ipfile:
             for sr_no, line in enumerate(rechable_ipfile, start=1):
                 host = line.strip()
                 #print(host)
                 model_th = threading.Thread(target = dslam_model ,args = (host,))
                 model_th.start()
                 model_threads.append(model_th)
             for model_thr in model_threads:
                 model_thr.join()

if __name__ == "__main__":
     db_connection()
     rechability_create_threads()
     dslam_model_threads()
     rechable_dslamsip.close()
     dslamsip.close()
Error:
10.111.3.254 a bytes-like object is required, not 'str' 10.211.128.53 a bytes-like object is required, not 'str' 10.111.3.172 a bytes-like object is required, not 'str' 10.112.1.171 a bytes-like object is required, not 'str' 10.112.3.76 a bytes-like object is required, not 'str' 10.112.3.56 a bytes-like object is required, not 'str' 10.111.1.113 a bytes-like object is required, not 'str' 10.11.2.17 a bytes-like object is required, not 'str' 10.112.1.130 a bytes-like object is required, not 'str' 10.111.1.162 a bytes-like object is required, not 'str' 10.111.3.174 a bytes-like object is required, not 'str'
Reply
#2
Can't test it and it's just a guess but you could try using:

import io 
bytes_like_object_to_send = io.BytesIO(string_to_send)

Edit: nevermind, my bad, please ignore
Reply
#3
Hi All,


from easysnmp import snmp_get, snmp_set, snmp_walk

snmp_get from easysnmp resolved this issue.

def dslam_model(ip):
    oid ='SNMPv2-MIB::sysDescr.0'
    try:
        result = snmp_get(oid, hostname=ip, community='public', version=2)
        if result.value == 'ECI telecom HiFOCuS broadband access system':
            model = 'ECI'
        elif result.value == 'VES1724-56':
            model = 'ZyXEL_VDSL'
        elif result.value == 'IP DSLAM MASTER':
            model = 'UT'
        else:
            model = 'ZyXEL'
        print(ip,model)
    except Exception as excp:
        pass
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: a bytes-like object is required ZeroX 13 4,154 Jan-07-2023, 07:02 PM
Last Post: deanhystad
  TypeError: a bytes-like object is required, not 'str' - Help Please. IanJ 3 4,819 Aug-29-2022, 05:53 PM
Last Post: deanhystad
  bytes object saved as .mp4 jttolleson 10 5,880 Feb-25-2022, 02:42 PM
Last Post: jttolleson
  python 3: TypeError: a bytes-like object is required, not 'str' wardancer84 3 6,508 Jul-09-2021, 05:55 PM
Last Post: deanhystad
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 5,214 May-02-2021, 03:45 PM
Last Post: Anldra12
  how to solve "a bytes-like object is required, not 'str'" error maiya 2 3,795 Jul-28-2020, 07:03 PM
Last Post: bowlofred
  TypeError: a bytes-like object is required, not 'str' ozzy69 1 2,858 Jul-17-2020, 03:38 PM
Last Post: stullis
  Packet Sniffer - TypeError: a bytes-like object is required, not 'str' cheffa2020 4 5,319 Jun-12-2020, 02:10 PM
Last Post: cheffa2020
  Why, TypeError: expected string or bytes-like object ? JohnnyCoffee 3 18,624 May-08-2020, 04:26 AM
Last Post: bowlofred
  TypeError: a bytes-like object is required, not 'str'. jacklee26 4 5,660 Apr-18-2020, 11:04 PM
Last Post: jacklee26

Forum Jump:

User Panel Messages

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