Apr-18-2020, 03:18 PM
Do anyone know how to convert the byte to str. I used below code work on python2 but if I used python3 it will occur Error like this:
TypeError: a bytes-like object is required, not 'str'.
So I try to use stdout = stdout.decode("UTF-8"), but it will also pop error
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa8 in position 16: invalid start byte
TypeError: a bytes-like object is required, not 'str'.
So I try to use stdout = stdout.decode("UTF-8"), but it will also pop error
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa8 in position 16: invalid start byte
# -*- coding: utf-8 -*- import telnetlib import subprocess import time def Telnet_Check_reachability(ip): ping_count=3 process = subprocess.Popen(['ping', ip, '-n', str(ping_count)],stdout=subprocess.PIPE,stderr=subprocess.STDOUT) process.wait() stdout = process.stdout.read() print (type(stdout)) print (stdout) if "TTL=" in stdout: #print "Server reachable" #successful = 1 print("1 Server reachable ") else: #print "Server unreachable" successful = 0 print("0 Server unreachable") #return successful ip ="8.8.8.8" new_IPv6 = Telnet_Check_reachability(ip) #print(new_IPv6)