MCPcopy
hub / github.com/iBaa/PlexConnect / PlexGDM

Function PlexGDM

PlexAPI.py:144–214  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

142Msg_PlexGDM = 'M-SEARCH * HTTP/1.0'
143
144def PlexGDM():
145 dprint(__name__, 0, "***")
146 dprint(__name__, 0, "PlexGDM - looking up Plex Media Server")
147 dprint(__name__, 0, "***")
148
149 # setup socket for discovery -> multicast message
150 GDM = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
151 GDM.settimeout(1.0)
152
153 # Set the time-to-live for messages to 1 for local network
154 ttl = struct.pack('b', 1)
155 GDM.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
156
157 returnData = []
158 try:
159 # Send data to the multicast group
160 dprint(__name__, 1, "Sending discovery message: {0}", Msg_PlexGDM)
161 GDM.sendto(Msg_PlexGDM, (IP_PlexGDM, Port_PlexGDM))
162
163 # Look for responses from all recipients
164 while True:
165 try:
166 data, server = GDM.recvfrom(1024)
167 dprint(__name__, 1, "Received data from {0}", server)
168 dprint(__name__, 1, "Data received:\n {0}", data)
169 returnData.append( { 'from' : server,
170 'data' : data } )
171 except socket.timeout:
172 break
173 finally:
174 GDM.close()
175
176 discovery_complete = True
177
178 PMS_list = {}
179 if returnData:
180 for response in returnData:
181 update = { 'ip' : response.get('from')[0] }
182
183 # Check if we had a positive HTTP response
184 if "200 OK" in response.get('data'):
185 for each in response.get('data').split('\n'):
186 # decode response data
187 update['discovery'] = "auto"
188 #update['owned']='1'
189 #update['master']= 1
190 #update['role']='master'
191
192 if "Content-Type:" in each:
193 update['content-type'] = each.split(':')[1].strip()
194 elif "Resource-Identifier:" in each:
195 update['uuid'] = each.split(':')[1].strip()
196 elif "Name:" in each:
197 update['serverName'] = each.split(':')[1].strip().decode('utf-8', 'replace') # store in utf-8
198 elif "Port:" in each:
199 update['port'] = each.split(':')[1].strip()
200 elif "Updated-At:" in each:
201 update['updated'] = each.split(':')[1].strip()

Callers 2

discoverPMSFunction · 0.85
PlexAPI.pyFile · 0.85

Calls 1

dprintFunction · 0.85

Tested by

no test coverage detected