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'
#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


Messages In This Thread
RE: TypeError: a bytes-like object is required, not 'str' - by stullis - Jul-17-2020, 03:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I am getting this TypeError: 'TreasureMap' object is not subscriptable. makilakos 2 157 May-25-2024, 07:58 PM
Last Post: deanhystad
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 554 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 624 Dec-30-2023, 04:35 PM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 852 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 1,180 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 1,548 Aug-23-2023, 06:21 PM
Last Post: deanhystad
  TypeError: 'float' object is not callable #1 isdito2001 1 1,147 Jan-21-2023, 12:43 AM
Last Post: Yoriz
  TypeError: a bytes-like object is required ZeroX 13 4,678 Jan-07-2023, 07:02 PM
Last Post: deanhystad
  TypeError: 'float' object is not callable TimofeyKolpakov 3 1,609 Dec-04-2022, 04:58 PM
Last Post: TimofeyKolpakov
  API Post issue "TypeError: 'str' object is not callable" makeeley 2 2,045 Oct-30-2022, 12:53 PM
Last Post: makeeley

Forum Jump:

User Panel Messages

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