# 12.3 radapy

The radapy is the python implementation for the radare remote protocol. This module is distributed in the scripts/ directory of radare. For better understanding, here's an example:

    $ cat scripts/radapy-example.py
    import radapy
    from string import *

    PORT = 9999

    def fun_system(str):
            print "CURRENT SEEK IS %d"%radapy.offset
            return str

    def fun_open(file,flags):
            return str

    def fun_seek(off,type):
            return str

    def fun_write(buf):
            print "WRITING %d bytes (%s)"%(len(buf),buf)
            return 6

    def fun_read(len):
            print "READ %d bytes from %d\n"% (len, radapy.offset)
            str = "patata"
            str = str[radapy.offset:]
            return str

    #radapy.handle_cmd_close = fun_close
    radapy.handle_cmd_system = fun_system
    radapy.handle_cmd_read = fun_read
    radapy.handle_cmd_write = fun_write
    radapy.size = 10

    radapy.listen_tcp (PORT)

As you see, you just need to implement the 'read' command and all the rest will mostly work. Here's a shorter implementation for immunity debugger:

    import immlib
    import radapy

    def fun_read(len):
        return immlib.Debugger().readMemory(radapy.offset, len)

    radapy.handle_cmd_read = fun_read
    radapy.listen_tcp ( 9999 )

For the other side you just have to connect a radare to this port to get the fun:

    $ radare connect://127.0.0.1:9999/dbg://immunity