Python Forum

Full Version: TypeError: a bytes-like object is required, not 'str'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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()