'Expected Type Bytes and got str instead udp client error


    import socket
    
    target_host = "127.0.0.1"
    target_port = 80
    
    # create a socket object
    client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    
    # send some data
    client.sendto("AAABBBCCC", (target_host, target_port))
    
    # receive some data
    data, addr = client.recvfrom(4096)
    print(data)

I receive this error when running this copy and pasted code from a pentesting book:


    Traceback (most recent call last):
      File "C:\Users\cLappy\PycharmProjects\Coffee Machine\.idea\VirtualEnvironment\lib\site-packages\IPython\core\interactiveshell.py", line 3441, in run_code
        exec(code_obj, self.user_global_ns, self.user_ns)
      File "<ipython-input-2-576063807716>", line 1, in <module>
        runfile('E:/Pycharm Projects/Pentesting/UDP Client.py', wdir='E:/Pycharm Projects/Pentesting')
      File "C:\Program Files\JetBrains\PyCharm 2020.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
        pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
      File "C:\Program Files\JetBrains\PyCharm 2020.3\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
        exec(compile(contents+"\n", file, 'exec'), glob, loc)
      File "E:/Pycharm Projects/Pentesting/UDP Client.py", line 10, in <module>
        client.sendto("AAABBBCCC", (target_host, target_port))
    TypeError: a bytes-like object is required, not 'str'

I would like to know what I can replace "AAABBBCCC" with so I can begin my free lancing pentesting journey.

Thank you for your time.



Solution 1:[1]

Edit the sendto func:

client.sendto(("AAABBBCCC").encode()(target_host.encode(),target_port))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 klutt