How to implement SSDP / UPnP? Trying to use the Sony camera API

I am a complete newbie for HTTP requests, but I would like to write a Python application that uses the Sony API to control its Wi-Fi. So far I'm just trying to talk to the camera at all, but my request does not work. I have all the documents (UPnP documentation, SSDP document, user manual, etc.), but I think that I am missing something really fundamental. According to Sony doc, I need:

  • Connect to the camera as an access point (i.e. log in like any other Wi-Fi router).
  • Sending a request to a specific URL and port

Can anyone understand what could be wrong here? Any good resources to get started with UPnP / SSDP? I got DISCOVERY_MSG line formatting from here .

#!/usr/bin/python

def main():
    import requests

    DISCOVERY_MSG = ('M-SEARCH * HTTP/1.1\r\n' +
                 'HOST: 239.255.255.250:1900\r\n' +
                 'MAN: "ssdp:discover"\r\n' +
                 'MX: 3\r\n' +
                 'ST: urn:schemas-sony-com:service:ScalarWebAPI:1\r\n' +
                 'USER-AGENT: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/536.30.1 (KHTML, like Gecko) Version/6.0.5 Safari/536.30.1\r\n\r\n')

    try:
        r = requests.get(DISCOVERY_MSG)
    except:
        print('Didn\'t work')


if __name__ == '__main__':
  main()
+4
source share
2 answers

I think this has little to do with UPnP: Sony just uses SSDP for discovery, and the defacto SSDP specification is in the UPnP architecture document.

As for the problem: it requests.get()does the usual HTTP GET (or if you provided the correct arguments) when you have to send UDP multicast messages and process the responses instead.

, SSDP (. UPNP UDA 1 ). , SSDP - (, sony).

+3
import sys
import socket

SSDP_ADDR = "239.255.255.250";
SSDP_PORT = 1900;
SSDP_MX = 1;
SSDP_ST = "urn:schemas-sony-com:service:ScalarWebAPI:1";

ssdpRequest = "M-SEARCH * HTTP/1.1\r\n" + \
                "HOST: %s:%d\r\n" % (SSDP_ADDR, SSDP_PORT) + \
                "MAN: \"ssdp:discover\"\r\n" + \
                "MX: %d\r\n" % (SSDP_MX, ) + \
                "ST: %s\r\n" % (SSDP_ST, ) + "\r\n";

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(ssdpRequest, (SSDP_ADDR, SSDP_PORT))
print sock.recv(1000)

https://github.com/crquan/work-on-sony-apis/blob/master/search-nex.py

+13

All Articles