PyDLNA 0.1.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pydlna/__init__.py +25 -0
- pydlna/_version.py +24 -0
- pydlna/client.py +290 -0
- pydlna/discovery.py +195 -0
- pydlna/errors.py +15 -0
- pydlna/file.py +101 -0
- pydlna/formatters.py +325 -0
- pydlna/handlers.py +174 -0
- pydlna/server.py +168 -0
- pydlna/services.py +126 -0
- pydlna-0.1.4.dist-info/METADATA +92 -0
- pydlna-0.1.4.dist-info/RECORD +15 -0
- pydlna-0.1.4.dist-info/WHEEL +5 -0
- pydlna-0.1.4.dist-info/licenses/LICENSE.md +21 -0
- pydlna-0.1.4.dist-info/top_level.txt +1 -0
pydlna/services.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"""UPnP service implementations exposed by the media server.
|
|
2
|
+
|
|
3
|
+
Each service owns a set of SOAP actions. The HTTP layer (handlers.py) parses
|
|
4
|
+
the envelope and calls ``handle()``; the service does the actual work and
|
|
5
|
+
returns the response fields. This keeps protocol parsing and business logic
|
|
6
|
+
in separate, testable places.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import logging
|
|
12
|
+
from typing import TYPE_CHECKING, Callable, Mapping
|
|
13
|
+
|
|
14
|
+
from pydlna.errors import UPnPError
|
|
15
|
+
from pydlna.formatters import XMLFormatter
|
|
16
|
+
|
|
17
|
+
if TYPE_CHECKING:
|
|
18
|
+
from pydlna.server import MediaLibrary
|
|
19
|
+
|
|
20
|
+
logger = logging.getLogger(__name__)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class UPnPService:
|
|
24
|
+
"""Base class: dispatches SOAP action names to Python callables."""
|
|
25
|
+
|
|
26
|
+
service_type = ""
|
|
27
|
+
|
|
28
|
+
def __init__(self) -> None:
|
|
29
|
+
self._actions: dict[str, Callable] = {}
|
|
30
|
+
|
|
31
|
+
def handle(self, action: str, args: Mapping[str, str], base_url: str) -> dict[str, str]:
|
|
32
|
+
method = self._actions.get(action)
|
|
33
|
+
if method is None:
|
|
34
|
+
raise UPnPError(401, f"Invalid action: {action}")
|
|
35
|
+
logger.debug("%s.%s(%s)", type(self).__name__, action, dict(args))
|
|
36
|
+
return method(args, base_url)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class ContentDirectoryService(UPnPService):
|
|
40
|
+
"""Implements ``ContentDirectory:1`` over a flat media library."""
|
|
41
|
+
|
|
42
|
+
service_type = "urn:schemas-upnp-org:service:ContentDirectory:1"
|
|
43
|
+
|
|
44
|
+
def __init__(self, library: "MediaLibrary", root_title: str = "PyDLNA") -> None:
|
|
45
|
+
super().__init__()
|
|
46
|
+
self.library = library
|
|
47
|
+
self.root_title = root_title
|
|
48
|
+
self._actions = {
|
|
49
|
+
"Browse": self._browse,
|
|
50
|
+
"Search": self._search,
|
|
51
|
+
"GetSearchCapabilities": lambda args, base_url: {"SearchCaps": ""},
|
|
52
|
+
"GetSortCapabilities": lambda args, base_url: {"SortCaps": ""},
|
|
53
|
+
"GetSystemUpdateID": lambda args, base_url: {"Id": "1"},
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
def _browse(self, args: Mapping[str, str], base_url: str) -> dict[str, str]:
|
|
57
|
+
object_id = args.get("ObjectID", "0")
|
|
58
|
+
browse_flag = args.get("BrowseFlag", "BrowseDirectChildren")
|
|
59
|
+
start = _to_int(args.get("StartingIndex"), 0)
|
|
60
|
+
count = _to_int(args.get("RequestedCount"), 0)
|
|
61
|
+
|
|
62
|
+
if browse_flag == "BrowseMetadata":
|
|
63
|
+
didl, returned, total = self._metadata(object_id, base_url)
|
|
64
|
+
elif object_id == "0":
|
|
65
|
+
files = self.library.files
|
|
66
|
+
selection = files[start:] if count == 0 else files[start:start + count]
|
|
67
|
+
didl = XMLFormatter.didl_items(selection, base_url)
|
|
68
|
+
returned, total = len(selection), len(files)
|
|
69
|
+
else:
|
|
70
|
+
# The library is flat: only the root container has children.
|
|
71
|
+
raise UPnPError(701, f"No such object: {object_id}")
|
|
72
|
+
return {
|
|
73
|
+
"Result": didl,
|
|
74
|
+
"NumberReturned": str(returned),
|
|
75
|
+
"TotalMatches": str(total),
|
|
76
|
+
"UpdateID": "1",
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
def _metadata(self, object_id: str, base_url: str) -> tuple[str, int, int]:
|
|
80
|
+
if object_id == "0":
|
|
81
|
+
didl = XMLFormatter.didl_root_container(self.root_title, len(self.library))
|
|
82
|
+
return didl, 1, 1
|
|
83
|
+
media = self.library.get(object_id)
|
|
84
|
+
if media is None:
|
|
85
|
+
raise UPnPError(701, f"No such object: {object_id}")
|
|
86
|
+
return XMLFormatter.didl_items([media], base_url), 1, 1
|
|
87
|
+
|
|
88
|
+
def _search(self, args: Mapping[str, str], base_url: str) -> dict[str, str]:
|
|
89
|
+
# Searching is out of scope for a flat library; answer with an empty
|
|
90
|
+
# (but valid) result instead of an error so renderers don't choke.
|
|
91
|
+
return {
|
|
92
|
+
"Result": XMLFormatter.didl_items([], base_url),
|
|
93
|
+
"NumberReturned": "0",
|
|
94
|
+
"TotalMatches": "0",
|
|
95
|
+
"UpdateID": "1",
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class ConnectionManagerService(UPnPService):
|
|
100
|
+
"""Implements ``ConnectionManager:1`` for HTTP-pull streaming."""
|
|
101
|
+
|
|
102
|
+
service_type = "urn:schemas-upnp-org:service:ConnectionManager:1"
|
|
103
|
+
|
|
104
|
+
def __init__(self, library: "MediaLibrary") -> None:
|
|
105
|
+
super().__init__()
|
|
106
|
+
self.library = library
|
|
107
|
+
self._actions = {
|
|
108
|
+
"GetProtocolInfo": self._protocol_info,
|
|
109
|
+
"GetCurrentConnectionIDs": lambda args, base_url: {"ConnectionIDs": ""},
|
|
110
|
+
"GetCurrentConnectionInfo": self._connection_info,
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
def _protocol_info(self, args: Mapping[str, str], base_url: str) -> dict[str, str]:
|
|
114
|
+
mime_types = sorted({media.mime_type for media in self.library if media.mime_type})
|
|
115
|
+
source = ";".join(f"http-get:*:{mime}:*" for mime in mime_types)
|
|
116
|
+
return {"Source": source, "Sink": ""}
|
|
117
|
+
|
|
118
|
+
def _connection_info(self, args: Mapping[str, str], base_url: str) -> dict[str, str]:
|
|
119
|
+
raise UPnPError(701, "Invalid connection ID")
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _to_int(value: object, default: int) -> int:
|
|
123
|
+
try:
|
|
124
|
+
return int(value) # type: ignore[arg-type]
|
|
125
|
+
except (TypeError, ValueError):
|
|
126
|
+
return default
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: PyDLNA
|
|
3
|
+
Version: 0.1.4
|
|
4
|
+
Summary: A simple, zero-dependency DLNA Python library for discovery, streaming, and device control.
|
|
5
|
+
Author: SurrealisticRabbit
|
|
6
|
+
License: MIT
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Requires-Python: >=3.8
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE.md
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
|
|
15
|
+
# PyDLNA
|
|
16
|
+
|
|
17
|
+
A simple, zero-dependency Python library for DLNA streaming, discovery, and device control.
|
|
18
|
+
|
|
19
|
+
Most DLNA libraries out there are either overly complex, poorly documented, or unmaintained. **PyDLNA** is built to be fit-for-purpose, straightforward, and ready to use in a few lines of code.
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install PyDLNA
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Quick Concepts
|
|
29
|
+
|
|
30
|
+
* **`DLNADiscovery()`** — Your best friend for finding DLNA devices (renderers, media servers, etc.) on your local network.
|
|
31
|
+
* **`DLNAServer()`** — Hosts and serves your local media files over HTTP with proper DLNA headers.
|
|
32
|
+
* **`device.controller()`** — Gives you an active playback control session (`.play()`, `.pause()`, track info) for a specific renderer.
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Usage Examples
|
|
37
|
+
|
|
38
|
+
### 1. Basic Streaming — `examples/basic.py`
|
|
39
|
+
|
|
40
|
+
Serve a directory, find a smart TV or speaker on the network, and throw a video/audio track directly to it:
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
import time
|
|
44
|
+
from pydlna import DLNADiscovery, DLNAServer
|
|
45
|
+
|
|
46
|
+
folder = "D:/Movies"
|
|
47
|
+
|
|
48
|
+
with DLNAServer(name="PyDLNA Example") as server:
|
|
49
|
+
server.library.add_directory(folder)
|
|
50
|
+
|
|
51
|
+
target = None
|
|
52
|
+
with DLNADiscovery().discover() as devices:
|
|
53
|
+
for device in devices:
|
|
54
|
+
if device.is_renderer:
|
|
55
|
+
print(f"Found renderer: {device.name}")
|
|
56
|
+
target = device
|
|
57
|
+
break
|
|
58
|
+
|
|
59
|
+
if target is None:
|
|
60
|
+
print("No renderer found.")
|
|
61
|
+
else:
|
|
62
|
+
with target.controller() as session:
|
|
63
|
+
media = server.library[0]
|
|
64
|
+
print(f"Sending: {media.url_for(target.address)}")
|
|
65
|
+
session.play(media)
|
|
66
|
+
|
|
67
|
+
while session.is_playing():
|
|
68
|
+
current = session.currently_playing
|
|
69
|
+
print(f"Playing: {media.title[:18]} > {current.position}:{current.duration}", end="\r")
|
|
70
|
+
time.sleep(1)
|
|
71
|
+
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
### 2. Inspecting Network Devices — `examples/client_info.py`
|
|
77
|
+
|
|
78
|
+
Scan your network and inspect the hardware, model numbers, and supported playback formats of discovered renderers:
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
from pydlna import DLNADiscovery
|
|
82
|
+
|
|
83
|
+
for client in DLNADiscovery().discover():
|
|
84
|
+
if client.is_renderer:
|
|
85
|
+
print(f"{client.name or '(unnamed)'} @ {client.address}")
|
|
86
|
+
print(f" Manufacturer : {client.manufacturer} ({client.manufacturer_url})")
|
|
87
|
+
print(f" Model : {client.model_name} {client.model_number}")
|
|
88
|
+
print(f" Playable formats : {client.playback_formats}")
|
|
89
|
+
print(f" Supported protocols: {client.supported_protocols}")
|
|
90
|
+
print("-" * 50)
|
|
91
|
+
|
|
92
|
+
```
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
pydlna/__init__.py,sha256=oHQR8EN_Ok3VTp2erwfjSUQHOxCb_mN45AnUHfgC0jQ,650
|
|
2
|
+
pydlna/_version.py,sha256=dCmqTphVTtnLNv5-BupgeR8iw0z5iLBjWjRFRXqOyso,520
|
|
3
|
+
pydlna/client.py,sha256=Vb-rYfy2UYpDv5iYSFHEhTj1J6YEk_mIhhzAb1urBU4,12051
|
|
4
|
+
pydlna/discovery.py,sha256=NTIPcZK2SUCeknuBPGsNi2cshxbmReyN-yZviNuhB5I,7084
|
|
5
|
+
pydlna/errors.py,sha256=BKMG-Cyx4vs5uBw2SwWX09WoByVTsN5Yd4Kdv8cUE6Y,483
|
|
6
|
+
pydlna/file.py,sha256=I4erClK_LgWEfXicim8dtMjeAqvrVr3ItOJ7xdjwTV8,3182
|
|
7
|
+
pydlna/formatters.py,sha256=Xna6rD4NwIHuUXHOPPB-WRMfBiEwzLJITvn4VJ9az0w,13563
|
|
8
|
+
pydlna/handlers.py,sha256=jWp8dlnylYy4KTcAzY-8XR5TqLIKjuuyTu8OdQa5zXo,6813
|
|
9
|
+
pydlna/server.py,sha256=IwWToQWBMr2l4zzJ0BxLGOu4xTg9oN9Md6VOW_nFUvY,5916
|
|
10
|
+
pydlna/services.py,sha256=MmuzWGnwqfAH93eE_FdrJcwLZeYWSr0CEBDDkRwschU,4865
|
|
11
|
+
pydlna-0.1.4.dist-info/licenses/LICENSE.md,sha256=KmK9PMRvBFTlsTb2osIH71l-c0fhVNi3BHH40-5S28Y,1074
|
|
12
|
+
pydlna-0.1.4.dist-info/METADATA,sha256=4VoaysDtndD36HucZh_QqcLP__V_LKeA5rqVNFMbtCI,2962
|
|
13
|
+
pydlna-0.1.4.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
14
|
+
pydlna-0.1.4.dist-info/top_level.txt,sha256=YZmueIFNHkmoxU4pZYjW-fzJioyt30lttuG6-_rGC8s,7
|
|
15
|
+
pydlna-0.1.4.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 SurrealisticRabbit
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pydlna
|