Python Forum
TypeError: a bytes-like object is required, not 'str'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: a bytes-like object is required, not 'str'
#1
Hello everyone, i tried to run this code, but appeared this error to me:

Error:
TypeError: a bytes-like object is required, not 'str' Traceback (most recent call last): File "/media/kali/E:\/cap/h37/pull_video.py", line 6, in <module> magicword='495464000000580000009bf89049c926884d4f922b3b33ba7eceacef63f77157ab2f53e3f768ecd9e18547b8c22e21d01bfb6b3de325a27b8fb3acef63f77157ab2f53e3f768ecd9e185eb20be383aab05a8c2a71f2c906d93f72a85e7356effe1b8f5af097f9147f87e'.decode('hex') AttributeError: 'str' object has no attribute 'decode'
The script?

import socket
import sys
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('172.16.10.1', 8888))

magicword='495464000000580000009bf89049c926884d4f922b3b33ba7eceacef63f77157ab2f53e3f768ecd9e18547b8c22e21d01bfb6b3de325a27b8fb3acef63f77157ab2f53e3f768ecd9e185eb20be383aab05a8c2a71f2c906d93f72a85e7356effe1b8f5af097f9147f87e'.decode('hex')
#magicword='000024002f4000a0f170de0c000000000000da0088012c0008a77afc2032336f0300000008004500cdb2ac100afcac10742e00832be65010fb982008000000000000106c7109c000da002032336fc4c92cbec4c980a50000aaaa00280000400040060a01c2ed22b8555effff671b000001f5'.decode('hex')
s.send(magicword)
data = s.recv(106) 
n=0
while n<150000: #write replace by while 1 if you want this to not stop
    data = s.recv(1024)
    sys.stdout.write(data)
    n=n+1
s.close()
Someone can help me
Reply
#2
Line 8, socket.send() requires a byte string. There are two changes you can make to do this:

  1. Change the string to a byte string with b'':
    import socket
    import sys
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('172.16.10.1', 8888))
     
    magicword = b'495464000000580000009bf89049c926884d4f922b3b33ba7eceacef63f77157ab2f53e3f768ecd9e18547b8c22e21d01bfb6b3de325a27b8fb3acef63f77157ab2f53e3f768ecd9e185eb20be383aab05a8c2a71f2c906d93f72a85e7356effe1b8f5af097f9147f87e'.decode('hex')
    #magicword='000024002f4000a0f170de0c000000000000da0088012c0008a77afc2032336f0300000008004500cdb2ac100afcac10742e00832be65010fb982008000000000000106c7109c000da002032336fc4c92cbec4c980a50000aaaa00280000400040060a01c2ed22b8555effff671b000001f5'.decode('hex')
    s.send(magicword)
    data = s.recv(106) 
    n=0
    while n<150000: #write replace by while 1 if you want this to not stop
        data = s.recv(1024)
        sys.stdout.write(data)
        n=n+1
    s.close()
  2. Use bytes() to change the string to a byte string when calling socket.send():
    import socket
    import sys
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('172.16.10.1', 8888))
     
    magicword='495464000000580000009bf89049c926884d4f922b3b33ba7eceacef63f77157ab2f53e3f768ecd9e18547b8c22e21d01bfb6b3de325a27b8fb3acef63f77157ab2f53e3f768ecd9e185eb20be383aab05a8c2a71f2c906d93f72a85e7356effe1b8f5af097f9147f87e'.decode('hex')
    #magicword='000024002f4000a0f170de0c000000000000da0088012c0008a77afc2032336f0300000008004500cdb2ac100afcac10742e00832be65010fb982008000000000000106c7109c000da002032336fc4c92cbec4c980a50000aaaa00280000400040060a01c2ed22b8555effff671b000001f5'.decode('hex')
    s.send(bytes(magicword, "utf-8"))
    data = s.recv(106) 
    n=0
    while n<150000: #write replace by while 1 if you want this to not stop
        data = s.recv(1024)
        sys.stdout.write(data)
        n=n+1
    s.close()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 361 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 492 Dec-30-2023, 04:35 PM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 730 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 977 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 1,323 Aug-23-2023, 06:21 PM
Last Post: deanhystad
  TypeError: 'float' object is not callable #1 isdito2001 1 1,071 Jan-21-2023, 12:43 AM
Last Post: Yoriz
  TypeError: a bytes-like object is required ZeroX 13 4,035 Jan-07-2023, 07:02 PM
Last Post: deanhystad
  TypeError: 'float' object is not callable TimofeyKolpakov 3 1,423 Dec-04-2022, 04:58 PM
Last Post: TimofeyKolpakov
  API Post issue "TypeError: 'str' object is not callable" makeeley 2 1,889 Oct-30-2022, 12:53 PM
Last Post: makeeley
  TypeError: 'NoneType' object is not subscriptable syafiq14 3 5,228 Sep-19-2022, 02:43 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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