-
Notifications
You must be signed in to change notification settings - Fork 0
/
msearch.py
62 lines (48 loc) · 1.51 KB
/
msearch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding:utf-8 -*-
# PythonでUPnPによるポートマップ(なぜなに Torrent)
# https://symfoware.blog.fc2.com/blog-entry-1990.html
import socket
import urllib.request
import urllib.parse as urlparse
import json
import xmlParser
def urlLiveview():
host = '239.255.255.250'
port = 1900
messages = [
b'M-SEARCH * HTTP/1.1',
b'HOST: 239.255.255.250:1900',
b'MAN: "ssdp:discover"',
b'MX:1',
b'ST: urn:schemas-sony-com:service:ScalarWebAPI:1',
]
# messages = [
# b'M-SEARCH * HTTP/1.1',
# b'MX: 3',
# b'HOST: 239.255.255.250:1900',
# b'MAN: "ssdp:discover"',
# b'ST: urn:schemas-upnp-org:service:WANIPConnection:1',
# ]
message = b'\r\n'.join(messages)
message += b'\r\n\r\n' # 末尾に改行コードを2つ付与
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(message, (host, port))
res = sock.recv(4096)
# print(res)
sock.close()
for line in str(res).split('\\r\\n'):
print(line)
info = line.strip()
if not info.startswith('LOCATION:'):
continue
locaction = info[len('LOCATION:'):].strip()
print(f'locaction: {locaction}')
break
# --- XMLの解析
f = urllib.request.urlopen(locaction)
xml_string = f.read()
f.close()
uri, host, url, cameraHost, cameraUrl = xmlParser.urlFromXml(xml_string, 1)
return uri, host, url, cameraHost, cameraUrl
if __name__ == '__main__':
urlLiveview()