Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 TypeError: a bytes-like object is required, not 'str'.
#1
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

# -*- 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)

Quote
#2
Please, post full traceback you get, not just the last line, in error tags
Quote
#3
Error:
Traceback (most recent call last): File "ip_check.py", line 28, in <module> new_IPv6 = Telnet_Check_reachability(ip) File "ip_check.py", line 13, in Telnet_Check_reachability stdout = stdout.encode("UTF-8") AttributeError: 'bytes' object has no attribute 'encode'
adding decode
Error:
Traceback (most recent call last): File "ip_check.py", line 28, in <module> new_IPv6 = Telnet_Check_reachability(ip) File "ip_check.py", line 13, in Telnet_Check_reachability stdout = stdout.decode("UTF-8") UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa8 in position 16: invalid start byte
buran wrote Apr-18-2020, 03:42 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Quote
#4
Adding "stdout = stdout.decode("UTF-8")" seems to be a bad choice. I would remove it.
Perhaps better to concentrate on the original message: "TypeError: a bytes-like object is required, not 'str'".
Please show us the complete message and the code where that error occurs.
Quote
#5
I change to stdout = stdout.decode("big5") and can work correct.

so python2 uses str, but python3 uses byte.
Python3 has to decode to convert byte to str. Why do we have to do that?
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  (TypeError: 'module' object is not callable) for getopt Module nnevarez 2 251 Jul-03-2020, 01:45 AM
Last Post: nnevarez
  TypeError: forward() missing 1 required positional argument: 'x' sveto4ka 4 323 Jun-17-2020, 07:25 PM
Last Post: sveto4ka
  Packet Sniffer - TypeError: a bytes-like object is required, not 'str' cheffa2020 4 281 Jun-12-2020, 02:10 PM
Last Post: cheffa2020
  TypeError: _linspace_dispatcher() missing 1 required positional argument: 'stop' Ae_Lovecraft 3 300 May-28-2020, 03:33 PM
Last Post: Larz60+
  TypeError: add() missing 2 required positional arguments NectDz 5 314 May-28-2020, 02:54 PM
Last Post: BitPythoner
  TypeError: 're.Match' object is not iterable charlesauspicks 1 485 May-25-2020, 06:14 AM
Last Post: bowlofred
  TypeError: a bytes-like object is required, not 'str' - Help Please. IanJ 1 371 May-19-2020, 07:15 PM
Last Post: Larz60+
  Why, TypeError: expected string or bytes-like object ? JohnnyCoffee 3 639 May-08-2020, 04:26 AM
Last Post: bowlofred
  Getting error "TypeError: 'int' object is not callable" while using ylim maxprime_bhisham 2 343 Apr-26-2020, 10:55 AM
Last Post: DataAnalyticsIreland
  TypeError: 'tuple' object is not callable Chuck_Norwich 2 463 Feb-29-2020, 08:23 PM
Last Post: Chuck_Norwich

Forum Jump:


Users browsing this thread: 1 Guest(s)