Annotation of embedaddon/miniupnpd/minissdpd/submit_to_minissdpd.py, revision 1.1

1.1     ! misho       1: #!/usr/bin/env python3
        !             2: # vim: sw=4 ts=4 expandtab
        !             3: # (c) 2021 Thomas BERNARD
        !             4: # Python3 module to submit service to running MiniSSDPd
        !             5: # MiniSSDPd: See http://miniupnp.free.fr/minissdpd.html
        !             6: import socket, os
        !             7: 
        !             8: 
        !             9: def codelength(s):
        !            10:     """ returns the given string/bytes as bytes, prepended with the 7-bit-encoded length """
        !            11:     # We want bytes
        !            12:     if not isinstance(s, bytes):
        !            13:         # Not bytes. Let's try to convert to bytes, but only plain ASCII
        !            14:         try:
        !            15:             s = str.encode(s, "ascii")
        !            16:         except:
        !            17:             s = b''
        !            18:     l = len(s)
        !            19:     if l == 0:
        !            20:         return b'\x00'
        !            21:     encodedlen = (l & 0x7F).to_bytes(1, 'little')
        !            22:     while l > 0x7F:
        !            23:         l = l >> 7
        !            24:         c = (l & 0x7F) | 0x80
        !            25:         encodedlen = c.to_bytes(1, 'little') + encodedlen
        !            26:     return encodedlen + s
        !            27: 
        !            28: 
        !            29: def submit_to_minissdpd(st, usn, server, url, sockpath="/var/run/minissdpd.sock"):
        !            30:     """ submits the specified service to MiniSSDPD (if running)"""
        !            31:     # First check if sockpath exists i.e. MiniSSDPD is running
        !            32:     if not os.path.exists(sockpath):
        !            33:         return -1, f"Error: {sockpath} does not exist. Is minissdpd running?"
        !            34:     # OK, submit
        !            35:     sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        !            36:     try:
        !            37:         sock.connect(sockpath)
        !            38:         sock.send(b'\x04' + codelength(st) + codelength(usn) + codelength(server) + codelength(url))
        !            39:     except socket.error as msg:
        !            40:         print(msg)
        !            41:         return -1, msg
        !            42:     finally:
        !            43:         sock.close()
        !            44:     return 0, "OK"
        !            45: 
        !            46: 
        !            47: if __name__ == "__main__":
        !            48:     # Example usage
        !            49:     rc, message = submit_to_minissdpd(
        !            50:         b'urn:schemas-upnp-org:device:InternetGatewayDevice:1',
        !            51:         b'uuid:73616d61-6a6b-7a74-650a-0d24d4a5d636::urn:schemas-upnp-org:device:InternetGatewayDevice:1',
        !            52:         b'MyServer/0.0',
        !            53:         b'http://192.168.0.1:1234/rootDesc.xml',
        !            54:     )
        !            55:     if rc == 0:
        !            56:         print("OK: submitting to MiniSSDPD went well")
        !            57:     else:
        !            58:         print("Not OK. Error message is:", message)

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>