rc and message. __name__ check, so usable as module
This commit is contained in:
parent
f1ba1a6adb
commit
884f17c8db
|
@ -2,10 +2,11 @@
|
||||||
# vim: sw=4 ts=4 expandtab
|
# vim: sw=4 ts=4 expandtab
|
||||||
# (c) 2021 Thomas BERNARD
|
# (c) 2021 Thomas BERNARD
|
||||||
# Python 3 : code sample
|
# Python 3 : code sample
|
||||||
import socket
|
import socket, os
|
||||||
|
|
||||||
|
|
||||||
def codelength(s):
|
def codelength(s):
|
||||||
|
""" returns the given bytearray prepended with the 7-bit-encoded length """
|
||||||
l = len(s)
|
l = len(s)
|
||||||
if l == 0:
|
if l == 0:
|
||||||
return b'\x00'
|
return b'\x00'
|
||||||
|
@ -18,21 +19,32 @@ def codelength(s):
|
||||||
|
|
||||||
|
|
||||||
def SubmitServicesToMiniSSDPD(st, usn, server, url, sockpath="/var/run/minissdpd.sock"):
|
def SubmitServicesToMiniSSDPD(st, usn, server, url, sockpath="/var/run/minissdpd.sock"):
|
||||||
|
""" submits the specified service to MiniSSDPD (if running)"""
|
||||||
|
# First check if sockpath exists i.e. MiniSSDPD is running
|
||||||
|
if not os.path.exists(sockpath):
|
||||||
|
return -1, f"Error: {sockpath} does not exist. Is minissdpd running?"
|
||||||
|
# OK, submit
|
||||||
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||||
try:
|
try:
|
||||||
sock.connect(sockpath)
|
sock.connect(sockpath)
|
||||||
sock.send(b'\x04' + codelength(st) + codelength(usn) + codelength(server) + codelength(url))
|
sock.send(b'\x04' + codelength(st) + codelength(usn) + codelength(server) + codelength(url))
|
||||||
except socket.error as msg:
|
except socket.error as msg:
|
||||||
print(msg)
|
print(msg)
|
||||||
return -1
|
return -1, msg
|
||||||
finally:
|
finally:
|
||||||
sock.close()
|
sock.close()
|
||||||
return 0
|
return 0, "OK"
|
||||||
|
|
||||||
|
|
||||||
SubmitServicesToMiniSSDPD(
|
if __name__ == "__main__":
|
||||||
b'urn:schemas-upnp-org:device:InternetGatewayDevice:1',
|
# Example usage
|
||||||
b'uuid:73616d61-6a6b-7a74-650a-0d24d4a5d636::urn:schemas-upnp-org:device:InternetGatewayDevice:1',
|
rc, message = SubmitServicesToMiniSSDPD(
|
||||||
b'MyServer/0.0',
|
b'urn:schemas-upnp-org:device:InternetGatewayDevice:1',
|
||||||
b'http://192.168.0.1:1234/rootDesc.xml',
|
b'uuid:73616d61-6a6b-7a74-650a-0d24d4a5d636::urn:schemas-upnp-org:device:InternetGatewayDevice:1',
|
||||||
)
|
b'MyServer/0.0',
|
||||||
|
b'http://192.168.0.1:1234/rootDesc.xml',
|
||||||
|
)
|
||||||
|
if rc == 0:
|
||||||
|
print("OK: submitting to MiniSSDPD went well")
|
||||||
|
else:
|
||||||
|
print("Not OK. Error message is:", message)
|
||||||
|
|
Loading…
Reference in New Issue