python-pylan 0.0.1__py3-none-any.whl → 0.0.3__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.
- pylan/__init__.py +18 -3
- pylan/__main__.py +29 -0
- pylan/cli/__init__.py +25 -0
- pylan/cli/cli.py +36 -0
- pylan/cli/receive.py +201 -0
- pylan/cli/scan.py +103 -0
- pylan/cli/send.py +106 -0
- pylan/client/__init__.py +14 -0
- pylan/core/__init__.py +103 -0
- pylan/core/discovery.py +475 -0
- pylan/core/handshake.py +145 -0
- pylan/core/models.py +77 -0
- pylan/core/net.py +88 -0
- pylan/core/protocol.py +132 -0
- pylan/core/transfer.py +420 -0
- pylan/errors/__init__.py +28 -0
- pylan/errors/base.py +13 -0
- pylan/main.py +6 -0
- pylan/ui/__init__.py +21 -0
- pylan/ui/app.py +188 -0
- pylan/ui/assets/icon.ico +0 -0
- pylan/ui/assets/icon.png +0 -0
- pylan/ui/assets/logo.svg +51 -0
- pylan/ui/components/__init__.py +14 -0
- pylan/ui/components/file_picker.py +243 -0
- pylan/ui/components/navbar.py +250 -0
- pylan/ui/components/peer_tile.py +152 -0
- pylan/ui/components/transfer_row.py +224 -0
- pylan/ui/state.py +823 -0
- pylan/ui/views/__init__.py +13 -0
- pylan/ui/views/home_view.py +245 -0
- pylan/ui/views/receive_view.py +249 -0
- pylan/ui/views/send_view.py +200 -0
- pylan/ui/views/settings_view.py +233 -0
- pylan/utils/utils.py +21 -0
- {python_pylan-0.0.1.dist-info → python_pylan-0.0.3.dist-info}/METADATA +13 -3
- python_pylan-0.0.3.dist-info/RECORD +39 -0
- {python_pylan-0.0.1.dist-info → python_pylan-0.0.3.dist-info}/WHEEL +1 -1
- python_pylan-0.0.3.dist-info/entry_points.txt +2 -0
- python_pylan-0.0.1.dist-info/RECORD +0 -4
pylan/__init__.py
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
-
"""
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"""PyLan - Fast, cross-platform local network file and data sharing.
|
|
2
|
+
|
|
3
|
+
PyLan provides peer discovery, framing protocols, and data transfer
|
|
4
|
+
mechanisms designed for local network environments. It operates both as an
|
|
5
|
+
extensible Python package and as a standalone command-line application or
|
|
6
|
+
graphical user interface.
|
|
7
|
+
|
|
8
|
+
Subpackages:
|
|
9
|
+
cli: Command-line interfaces for scanning, sending, and receiving.
|
|
10
|
+
core: Protocol framing, peer discovery engine, and data models.
|
|
11
|
+
ui: Flet desktop and web graphical user interface module.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
__version__: str = "0.0.3"
|
|
15
|
+
|
|
16
|
+
__all__: list[str] = [
|
|
17
|
+
"__version__",
|
|
18
|
+
]
|
pylan/__main__.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""Main package entrypoint handling CLI vs GUI dispatch for PyLan."""
|
|
2
|
+
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def main() -> None:
|
|
7
|
+
"""Dispatch execution to GUI or CLI based on command-line arguments."""
|
|
8
|
+
if len(sys.argv) > 1 and sys.argv[1] == "gui":
|
|
9
|
+
sys.argv.pop(1)
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
import flet as ft
|
|
13
|
+
|
|
14
|
+
from pylan.ui.app import main as ui_main
|
|
15
|
+
|
|
16
|
+
assets_dir = str(Path(__file__).parent / "ui" / "assets")
|
|
17
|
+
app_fn = getattr(ft, "app", None)
|
|
18
|
+
if callable(app_fn):
|
|
19
|
+
app_fn(target=ui_main, assets_dir=assets_dir)
|
|
20
|
+
else:
|
|
21
|
+
ft.run(ui_main, assets_dir=assets_dir)
|
|
22
|
+
else:
|
|
23
|
+
from pylan.cli.cli import cli
|
|
24
|
+
|
|
25
|
+
cli()
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
if __name__ == "__main__":
|
|
29
|
+
main()
|
pylan/cli/__init__.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Command-line interface commands and entry points for PyLan.
|
|
2
|
+
|
|
3
|
+
This package provides the console commands and interactive CLI workflows
|
|
4
|
+
for scanning nearby peers, transmitting files over framed TCP connections,
|
|
5
|
+
and hosting the background receiver daemon.
|
|
6
|
+
|
|
7
|
+
Modules:
|
|
8
|
+
cli: Root Click command group registering all CLI subcommands.
|
|
9
|
+
receive: TCP receiver daemon and multicast presence broadcaster.
|
|
10
|
+
scan: Staged local network scanner discovering active PyLan nodes.
|
|
11
|
+
send: Client transfer initiator negotiating and streaming files.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from pylan.cli.cli import cli, gui
|
|
15
|
+
from pylan.cli.receive import receive
|
|
16
|
+
from pylan.cli.scan import scan
|
|
17
|
+
from pylan.cli.send import send
|
|
18
|
+
|
|
19
|
+
__all__: list[str] = [
|
|
20
|
+
"cli",
|
|
21
|
+
"gui",
|
|
22
|
+
"receive",
|
|
23
|
+
"scan",
|
|
24
|
+
"send",
|
|
25
|
+
]
|
pylan/cli/cli.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""Click root command group for PyLan CLI."""
|
|
2
|
+
|
|
3
|
+
import click
|
|
4
|
+
|
|
5
|
+
from pylan.cli.receive import receive
|
|
6
|
+
from pylan.cli.scan import scan
|
|
7
|
+
from pylan.cli.send import send
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@click.group()
|
|
11
|
+
def cli() -> None:
|
|
12
|
+
"""PyLan - Local network file transfer tool."""
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@click.command()
|
|
17
|
+
def gui() -> None:
|
|
18
|
+
"""Launch the PyLan desktop graphical interface."""
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
|
|
21
|
+
import flet as ft
|
|
22
|
+
|
|
23
|
+
from pylan.ui.app import main as ui_main
|
|
24
|
+
|
|
25
|
+
assets_dir = str(Path(__file__).parent.parent / "ui" / "assets")
|
|
26
|
+
app_fn = getattr(ft, "app", None)
|
|
27
|
+
if callable(app_fn):
|
|
28
|
+
app_fn(target=ui_main, assets_dir=assets_dir)
|
|
29
|
+
else:
|
|
30
|
+
ft.run(ui_main, assets_dir=assets_dir)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
cli.add_command(scan)
|
|
34
|
+
cli.add_command(receive)
|
|
35
|
+
cli.add_command(send)
|
|
36
|
+
cli.add_command(gui)
|
pylan/cli/receive.py
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"""Receiver daemon for the PyLan local network transfer system.
|
|
2
|
+
|
|
3
|
+
This module provides the core listener service for incoming discovery
|
|
4
|
+
handshakes and file transfers. It initializes a persistent TCP server to
|
|
5
|
+
handle probe requests and coordinates periodic multicast announcements to
|
|
6
|
+
ensure active visibility to scanning peers across the local subnet.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import asyncio
|
|
10
|
+
import json
|
|
11
|
+
import logging
|
|
12
|
+
import socket
|
|
13
|
+
|
|
14
|
+
import click
|
|
15
|
+
|
|
16
|
+
from pylan.core.discovery import (
|
|
17
|
+
DiscoveryEngine,
|
|
18
|
+
MSG_TYPE_PROBE_REQUEST,
|
|
19
|
+
MSG_TYPE_PROBE_RESPONSE,
|
|
20
|
+
)
|
|
21
|
+
from pylan.core.protocol import (
|
|
22
|
+
MSG_TYPE_TRANSFER_REJECT,
|
|
23
|
+
MSG_TYPE_TRANSFER_REQUEST,
|
|
24
|
+
pack_message,
|
|
25
|
+
read_frame,
|
|
26
|
+
)
|
|
27
|
+
from pylan.core.transfer import parse_transfer_request, receive_file
|
|
28
|
+
|
|
29
|
+
logger = logging.getLogger(__name__)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
async def handle_transfer(
|
|
33
|
+
reader: asyncio.StreamReader,
|
|
34
|
+
writer: asyncio.StreamWriter,
|
|
35
|
+
initial_body: bytes,
|
|
36
|
+
output_dir: str,
|
|
37
|
+
) -> None:
|
|
38
|
+
"""Handle incoming file transfer stream."""
|
|
39
|
+
try:
|
|
40
|
+
metadata = parse_transfer_request(initial_body)
|
|
41
|
+
result = await receive_file(reader, writer, output_dir, metadata)
|
|
42
|
+
click.secho(
|
|
43
|
+
f"\nReceived file '{result.file_name}' -> {result.file_path}",
|
|
44
|
+
fg="green",
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
except Exception as exc:
|
|
48
|
+
logger.debug("Transfer failed or was rejected: %s", exc)
|
|
49
|
+
try:
|
|
50
|
+
reject = pack_message(
|
|
51
|
+
MSG_TYPE_TRANSFER_REJECT, {"reason": str(exc)}
|
|
52
|
+
)
|
|
53
|
+
writer.write(reject)
|
|
54
|
+
await writer.drain()
|
|
55
|
+
except Exception:
|
|
56
|
+
pass
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
async def handle_client(
|
|
60
|
+
reader: asyncio.StreamReader,
|
|
61
|
+
writer: asyncio.StreamWriter,
|
|
62
|
+
engine: DiscoveryEngine,
|
|
63
|
+
output_dir: str,
|
|
64
|
+
) -> None:
|
|
65
|
+
"""Process incoming framed TCP connections from remote PyLan nodes.
|
|
66
|
+
|
|
67
|
+
Reads and validates binary protocol headers, parses corresponding payload
|
|
68
|
+
bodies, and responds to discovery probes. Non-probe payloads and connection
|
|
69
|
+
errors are logged and cleanly closed without interrupting the server.
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
reader: Async stream reader connected to the incoming client socket.
|
|
73
|
+
writer: Async stream writer connected to the incoming client socket.
|
|
74
|
+
engine: DiscoveryEngine instance representing this local peer.
|
|
75
|
+
output_dir: Filesystem path configured to store accepted transfers.
|
|
76
|
+
"""
|
|
77
|
+
try:
|
|
78
|
+
msg_type, body = await read_frame(reader)
|
|
79
|
+
|
|
80
|
+
if msg_type == MSG_TYPE_PROBE_REQUEST:
|
|
81
|
+
try:
|
|
82
|
+
peer_data = json.loads(body.decode("utf-8"))
|
|
83
|
+
logger.debug(
|
|
84
|
+
"Received probe request from %s",
|
|
85
|
+
peer_data.get("alias", "unknown"),
|
|
86
|
+
)
|
|
87
|
+
except Exception as exc:
|
|
88
|
+
logger.debug("Failed to decode probe payload: %s", exc)
|
|
89
|
+
|
|
90
|
+
response_payload = engine.get_announcement_payload()
|
|
91
|
+
reply = pack_message(MSG_TYPE_PROBE_RESPONSE, response_payload)
|
|
92
|
+
writer.write(reply)
|
|
93
|
+
await writer.drain()
|
|
94
|
+
elif msg_type == MSG_TYPE_TRANSFER_REQUEST:
|
|
95
|
+
await handle_transfer(reader, writer, body, output_dir)
|
|
96
|
+
else:
|
|
97
|
+
logger.debug("Received unhandled message type: %d", msg_type)
|
|
98
|
+
except (asyncio.IncompleteReadError, ConnectionResetError) as exc:
|
|
99
|
+
logger.debug("Peer disconnected unexpectedly: %s", exc)
|
|
100
|
+
except Exception as exc:
|
|
101
|
+
logger.debug("Error while handling client connection: %s", exc)
|
|
102
|
+
finally:
|
|
103
|
+
writer.close()
|
|
104
|
+
await writer.wait_closed()
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
async def periodic_announce(
|
|
108
|
+
engine: DiscoveryEngine,
|
|
109
|
+
interval: float = 1.0,
|
|
110
|
+
) -> None:
|
|
111
|
+
"""Periodically broadcast multicast UDP announcements across the LAN.
|
|
112
|
+
|
|
113
|
+
Maintains an active presence on the local subnet by continuously invoking
|
|
114
|
+
multicast transmission at regular intervals. Runs until explicitly
|
|
115
|
+
cancelled during daemon shutdown.
|
|
116
|
+
|
|
117
|
+
Args:
|
|
118
|
+
engine: DiscoveryEngine instance providing identity and announcements.
|
|
119
|
+
interval: Delay in seconds between consecutive multicast transmissions.
|
|
120
|
+
"""
|
|
121
|
+
while True:
|
|
122
|
+
try:
|
|
123
|
+
await engine.announce()
|
|
124
|
+
except asyncio.CancelledError:
|
|
125
|
+
break
|
|
126
|
+
except Exception as exc:
|
|
127
|
+
logger.debug("Multicast announcement broadcast failed: %s", exc)
|
|
128
|
+
await asyncio.sleep(interval)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
async def start_receiver_daemon(port: int, output_dir: str) -> None:
|
|
132
|
+
"""Start and supervise the PyLan receiver daemon lifecycle.
|
|
133
|
+
|
|
134
|
+
Binds the local TCP framing listener, joins the discovery multicast
|
|
135
|
+
group, and supervises the periodic announcement background task until
|
|
136
|
+
interrupted. Ensures transports and tasks are terminated cleanly.
|
|
137
|
+
|
|
138
|
+
Args:
|
|
139
|
+
port: TCP port where incoming transfer connections will be accepted.
|
|
140
|
+
output_dir: Destination directory path for storing received data.
|
|
141
|
+
"""
|
|
142
|
+
engine = DiscoveryEngine(
|
|
143
|
+
alias=socket.gethostname(),
|
|
144
|
+
fingerprint="receiver-node",
|
|
145
|
+
tcp_port=port,
|
|
146
|
+
device_model="PyLan Desktop Node",
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
click.echo(f"Device alias : {socket.gethostname()}")
|
|
150
|
+
click.echo(f"Listening on : port {port}")
|
|
151
|
+
click.echo(f"Saving files : {output_dir}")
|
|
152
|
+
click.echo(
|
|
153
|
+
"Ready. Waiting for incoming discovery and file transfers "
|
|
154
|
+
"(Ctrl+C to stop)..."
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
transport = await engine.start_multicast_listener()
|
|
158
|
+
announce_task = asyncio.create_task(
|
|
159
|
+
periodic_announce(engine, interval=1.0)
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
try:
|
|
163
|
+
server = await asyncio.start_server(
|
|
164
|
+
lambda r, w: handle_client(r, w, engine, output_dir),
|
|
165
|
+
host="0.0.0.0",
|
|
166
|
+
port=port,
|
|
167
|
+
)
|
|
168
|
+
async with server:
|
|
169
|
+
await server.serve_forever()
|
|
170
|
+
finally:
|
|
171
|
+
announce_task.cancel()
|
|
172
|
+
try:
|
|
173
|
+
await announce_task
|
|
174
|
+
except asyncio.CancelledError:
|
|
175
|
+
pass
|
|
176
|
+
|
|
177
|
+
if transport is not None:
|
|
178
|
+
transport.close()
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
@click.command()
|
|
182
|
+
@click.option(
|
|
183
|
+
"-p",
|
|
184
|
+
"--port",
|
|
185
|
+
default=44000,
|
|
186
|
+
type=int,
|
|
187
|
+
help="Port to listen for incoming connections.",
|
|
188
|
+
)
|
|
189
|
+
@click.option(
|
|
190
|
+
"-o",
|
|
191
|
+
"--output",
|
|
192
|
+
default="./downloads",
|
|
193
|
+
type=click.Path(),
|
|
194
|
+
help="Target folder where received files will be written.",
|
|
195
|
+
)
|
|
196
|
+
def receive(port: int, output: str) -> None:
|
|
197
|
+
"""Start PyLan in receiver mode to listen for peers and incoming files."""
|
|
198
|
+
try:
|
|
199
|
+
asyncio.run(start_receiver_daemon(port, output))
|
|
200
|
+
except KeyboardInterrupt:
|
|
201
|
+
click.echo("\nReceiver stopped.")
|
pylan/cli/scan.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"""Peer discovery scanner CLI for the PyLan local network transfer system.
|
|
2
|
+
|
|
3
|
+
This module provides the command-line interface and orchestration for scanning
|
|
4
|
+
the local network area to identify active PyLan nodes via multicast responses
|
|
5
|
+
and subnet TCP sweeps.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import asyncio
|
|
9
|
+
import logging
|
|
10
|
+
import socket
|
|
11
|
+
import uuid
|
|
12
|
+
|
|
13
|
+
import click
|
|
14
|
+
|
|
15
|
+
from pylan.core.discovery import DiscoveryEngine
|
|
16
|
+
from pylan.core.models import StatefulDevice
|
|
17
|
+
from pylan.core.net import get_active_interface_details
|
|
18
|
+
|
|
19
|
+
logger = logging.getLogger(__name__)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def get_local_interfaces() -> list[str]:
|
|
23
|
+
"""Retrieve active local IPv4 outbound network addresses.
|
|
24
|
+
|
|
25
|
+
Creates a transient UDP socket directed at an external routing target to
|
|
26
|
+
reliably determine the machine's primary local IP address without sending
|
|
27
|
+
actual network traffic.
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
A list containing the local IPv4 address string, or ['127.0.0.1']
|
|
31
|
+
if network interfaces cannot be probed.
|
|
32
|
+
"""
|
|
33
|
+
ip, _ = get_active_interface_details()
|
|
34
|
+
return [ip]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
async def execute_discovery(timeout: float) -> list[StatefulDevice]:
|
|
38
|
+
"""Initialize the discovery engine and execute staged peer sweeps.
|
|
39
|
+
|
|
40
|
+
Configures a transient scanner engine identity, queries local routing
|
|
41
|
+
interfaces, and runs the discovery progression across multicast and
|
|
42
|
+
fallback subnet probes.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
timeout: Grace period in seconds to wait for peer responses.
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
A list of StatefulDevice instances discovered across all stages.
|
|
49
|
+
"""
|
|
50
|
+
engine = DiscoveryEngine(
|
|
51
|
+
alias=socket.gethostname(),
|
|
52
|
+
fingerprint=uuid.uuid4().hex[:12],
|
|
53
|
+
tcp_port=44000,
|
|
54
|
+
device_model="CLI Scanner",
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
local_ips = get_local_interfaces()
|
|
58
|
+
devices = await engine.discover_staged(
|
|
59
|
+
interface_ips=local_ips,
|
|
60
|
+
grace_period=timeout,
|
|
61
|
+
)
|
|
62
|
+
return devices
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@click.command()
|
|
66
|
+
@click.option(
|
|
67
|
+
"--timeout",
|
|
68
|
+
"-t",
|
|
69
|
+
default=2.0,
|
|
70
|
+
type=float,
|
|
71
|
+
help="Duration in seconds to wait for peer responses.",
|
|
72
|
+
)
|
|
73
|
+
def scan(timeout: float) -> None:
|
|
74
|
+
"""Scan the local area network for active PyLan devices."""
|
|
75
|
+
click.echo(f"Scanning local network (timeout: {timeout}s)...")
|
|
76
|
+
|
|
77
|
+
try:
|
|
78
|
+
devices = asyncio.run(execute_discovery(timeout))
|
|
79
|
+
except Exception as exc:
|
|
80
|
+
click.secho(f"Error scanning network: {exc}", fg="red", err=True)
|
|
81
|
+
raise SystemExit(1)
|
|
82
|
+
|
|
83
|
+
if not devices:
|
|
84
|
+
click.echo("No PyLan devices found on the local network.")
|
|
85
|
+
return
|
|
86
|
+
|
|
87
|
+
click.echo()
|
|
88
|
+
header = f"{'ALIAS / NAME':<22} {'FINGERPRINT':<18} {'ADDRESS':<16}"
|
|
89
|
+
click.secho(header, bold=True)
|
|
90
|
+
click.echo("-" * 58)
|
|
91
|
+
|
|
92
|
+
for item in devices:
|
|
93
|
+
dev = getattr(item, "device", item)
|
|
94
|
+
alias = getattr(dev, "alias", "Unknown")
|
|
95
|
+
fingerprint = getattr(dev, "fingerprint", "N/A")
|
|
96
|
+
channel = getattr(dev, "channel", None)
|
|
97
|
+
address = (
|
|
98
|
+
getattr(channel, "host", "Unknown") if channel else "Unknown"
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
click.echo(
|
|
102
|
+
f"{str(alias):<22} {str(fingerprint):<18} {str(address):<16}"
|
|
103
|
+
)
|
pylan/cli/send.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""Send files to a discovered PyLan peer node."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
from contextlib import ExitStack
|
|
5
|
+
import logging
|
|
6
|
+
import os
|
|
7
|
+
from typing import Any, Optional
|
|
8
|
+
|
|
9
|
+
import click
|
|
10
|
+
|
|
11
|
+
from pylan.core.transfer import (
|
|
12
|
+
DEFAULT_CHUNK_SIZE,
|
|
13
|
+
DEFAULT_TRANSFER_TIMEOUT,
|
|
14
|
+
send_file,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
logger = logging.getLogger(__name__)
|
|
18
|
+
|
|
19
|
+
CHUNK_SIZE = DEFAULT_CHUNK_SIZE # 64 KB per network chunk
|
|
20
|
+
NETWORK_TIMEOUT = DEFAULT_TRANSFER_TIMEOUT # 10s timeout for handshakes
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
async def execute_transfer(host: str, port: int, file_path: str) -> None:
|
|
24
|
+
"""Connect to receiver, negotiate transfer, and stream file contents.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
host: Destination IP address or hostname.
|
|
28
|
+
port: Destination TCP port.
|
|
29
|
+
file_path: Local filesystem path to the file to transmit.
|
|
30
|
+
"""
|
|
31
|
+
file_name = os.path.basename(file_path)
|
|
32
|
+
reported = 0
|
|
33
|
+
|
|
34
|
+
with ExitStack() as stack:
|
|
35
|
+
bar: Optional[Any] = None
|
|
36
|
+
|
|
37
|
+
def on_progress(sent_bytes: int, total_bytes: int) -> None:
|
|
38
|
+
nonlocal bar, reported
|
|
39
|
+
if bar is None:
|
|
40
|
+
bar = stack.enter_context(
|
|
41
|
+
click.progressbar(
|
|
42
|
+
length=total_bytes,
|
|
43
|
+
label=f"Sending {file_name}",
|
|
44
|
+
)
|
|
45
|
+
)
|
|
46
|
+
delta = sent_bytes - reported
|
|
47
|
+
if delta > 0:
|
|
48
|
+
bar.update(delta)
|
|
49
|
+
reported = sent_bytes
|
|
50
|
+
|
|
51
|
+
result = await send_file(
|
|
52
|
+
host,
|
|
53
|
+
port,
|
|
54
|
+
file_path,
|
|
55
|
+
timeout=NETWORK_TIMEOUT,
|
|
56
|
+
handshake_timeout=NETWORK_TIMEOUT,
|
|
57
|
+
chunk_size=CHUNK_SIZE,
|
|
58
|
+
on_progress=on_progress,
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
if not result.accepted:
|
|
62
|
+
if result.unexpected_response_type is not None:
|
|
63
|
+
click.secho(
|
|
64
|
+
f"Unexpected response code: {result.unexpected_response_type}",
|
|
65
|
+
fg="red",
|
|
66
|
+
)
|
|
67
|
+
else:
|
|
68
|
+
click.secho(
|
|
69
|
+
f"Transfer rejected: {result.rejection_reason or 'unknown'}",
|
|
70
|
+
fg="red",
|
|
71
|
+
)
|
|
72
|
+
return
|
|
73
|
+
|
|
74
|
+
click.secho(
|
|
75
|
+
f"\nSuccessfully sent {result.file_name} ({result.file_size} bytes) "
|
|
76
|
+
f"to {host}:{port}",
|
|
77
|
+
fg="green",
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@click.command()
|
|
82
|
+
@click.argument("file", type=click.Path(exists=True, dir_okay=False))
|
|
83
|
+
@click.option(
|
|
84
|
+
"--to",
|
|
85
|
+
"-t",
|
|
86
|
+
"target",
|
|
87
|
+
required=True,
|
|
88
|
+
help="Target peer address in HOST or HOST:PORT format.",
|
|
89
|
+
)
|
|
90
|
+
def send(file: str, target: str) -> None:
|
|
91
|
+
"""Send a file to a remote PyLan receiver node."""
|
|
92
|
+
if ":" in target:
|
|
93
|
+
host, port_str = target.split(":", 1)
|
|
94
|
+
try:
|
|
95
|
+
port = int(port_str)
|
|
96
|
+
except ValueError:
|
|
97
|
+
click.secho(f"Invalid port: {port_str}", fg="red", err=True)
|
|
98
|
+
raise SystemExit(1)
|
|
99
|
+
else:
|
|
100
|
+
host, port = target, 44000
|
|
101
|
+
|
|
102
|
+
try:
|
|
103
|
+
asyncio.run(execute_transfer(host, port, file))
|
|
104
|
+
except Exception as exc:
|
|
105
|
+
click.secho(f"Transfer failed: {exc}", fg="red", err=True)
|
|
106
|
+
raise SystemExit(1)
|
pylan/client/__init__.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"""PyLan — use PyLan as a Python library, independent of the GUI and CLI.
|
|
2
|
+
|
|
3
|
+
This package provides programmatic access to PyLan. For command-line
|
|
4
|
+
usage, install the ``cli`` extra::
|
|
5
|
+
|
|
6
|
+
pip install "pylan[cli]"
|
|
7
|
+
|
|
8
|
+
For the desktop GUI, download the pre-built executable from the
|
|
9
|
+
project's GitHub releases:
|
|
10
|
+
|
|
11
|
+
https://github.com/aryan-202/python-pylan
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
__version__ = "0.0.3"
|
pylan/core/__init__.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"""Core networking, framing, and discovery primitives for PyLan.
|
|
2
|
+
|
|
3
|
+
This package provides low-level network abstractions, binary framing
|
|
4
|
+
protocols, peer discovery mechanisms, and device state management for local
|
|
5
|
+
area network coordination and file transfer.
|
|
6
|
+
|
|
7
|
+
Modules:
|
|
8
|
+
discovery: UDP multicast announcements, TCP probing, and subnet sweeps.
|
|
9
|
+
handshake: Synchronous length-prefixed socket handshake manager.
|
|
10
|
+
models: Data models representing network channels, devices, and stores.
|
|
11
|
+
net: Network adapter inspection and ARP neighbor table queries.
|
|
12
|
+
protocol: Fixed-header binary message framing and serialization.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from pylan.core.discovery import (
|
|
16
|
+
DEFAULT_DISCOVERY_TIMEOUT,
|
|
17
|
+
DEFAULT_MULTICAST_GROUP,
|
|
18
|
+
DEFAULT_MULTICAST_PORT,
|
|
19
|
+
DiscoveryEngine,
|
|
20
|
+
DiscoveryProtocol,
|
|
21
|
+
)
|
|
22
|
+
from pylan.core.handshake import (
|
|
23
|
+
DEFAULT_MAX_PAYLOAD_SIZE,
|
|
24
|
+
HandshakeManager,
|
|
25
|
+
)
|
|
26
|
+
from pylan.core.models import (
|
|
27
|
+
DeviceStore,
|
|
28
|
+
DiscoveredDevice,
|
|
29
|
+
HttpChannel,
|
|
30
|
+
ProtocolType,
|
|
31
|
+
StatefulDevice,
|
|
32
|
+
)
|
|
33
|
+
from pylan.core.net import (
|
|
34
|
+
get_active_interface_details,
|
|
35
|
+
get_arp_neighbors,
|
|
36
|
+
get_default_interface_ip,
|
|
37
|
+
)
|
|
38
|
+
from pylan.core.protocol import (
|
|
39
|
+
HEADER_FORMAT,
|
|
40
|
+
HEADER_SIZE,
|
|
41
|
+
MAX_FRAME_SIZE,
|
|
42
|
+
MSG_TYPE_TRANSFER_ACCEPT,
|
|
43
|
+
MSG_TYPE_TRANSFER_CHUNK,
|
|
44
|
+
MSG_TYPE_TRANSFER_COMPLETE,
|
|
45
|
+
MSG_TYPE_TRANSFER_REJECT,
|
|
46
|
+
MSG_TYPE_TRANSFER_REQUEST,
|
|
47
|
+
pack_message,
|
|
48
|
+
read_frame,
|
|
49
|
+
unpack_header,
|
|
50
|
+
)
|
|
51
|
+
from pylan.core.transfer import (
|
|
52
|
+
DEFAULT_CHUNK_SIZE,
|
|
53
|
+
DEFAULT_HANDSHAKE_TIMEOUT,
|
|
54
|
+
DEFAULT_TRANSFER_TIMEOUT,
|
|
55
|
+
TransferMetadata,
|
|
56
|
+
TransferResult,
|
|
57
|
+
compute_file_sha256,
|
|
58
|
+
parse_transfer_request,
|
|
59
|
+
receive_file,
|
|
60
|
+
resolve_unique_path,
|
|
61
|
+
sanitize_filename,
|
|
62
|
+
send_file,
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
__all__: list[str] = [
|
|
66
|
+
"DEFAULT_CHUNK_SIZE",
|
|
67
|
+
"DEFAULT_DISCOVERY_TIMEOUT",
|
|
68
|
+
"DEFAULT_HANDSHAKE_TIMEOUT",
|
|
69
|
+
"DEFAULT_MAX_PAYLOAD_SIZE",
|
|
70
|
+
"DEFAULT_MULTICAST_GROUP",
|
|
71
|
+
"DEFAULT_MULTICAST_PORT",
|
|
72
|
+
"DEFAULT_TRANSFER_TIMEOUT",
|
|
73
|
+
"DeviceStore",
|
|
74
|
+
"DiscoveredDevice",
|
|
75
|
+
"DiscoveryEngine",
|
|
76
|
+
"DiscoveryProtocol",
|
|
77
|
+
"HEADER_FORMAT",
|
|
78
|
+
"HEADER_SIZE",
|
|
79
|
+
"HandshakeManager",
|
|
80
|
+
"HttpChannel",
|
|
81
|
+
"MAX_FRAME_SIZE",
|
|
82
|
+
"MSG_TYPE_TRANSFER_ACCEPT",
|
|
83
|
+
"MSG_TYPE_TRANSFER_CHUNK",
|
|
84
|
+
"MSG_TYPE_TRANSFER_COMPLETE",
|
|
85
|
+
"MSG_TYPE_TRANSFER_REJECT",
|
|
86
|
+
"MSG_TYPE_TRANSFER_REQUEST",
|
|
87
|
+
"ProtocolType",
|
|
88
|
+
"StatefulDevice",
|
|
89
|
+
"TransferMetadata",
|
|
90
|
+
"TransferResult",
|
|
91
|
+
"compute_file_sha256",
|
|
92
|
+
"get_active_interface_details",
|
|
93
|
+
"get_arp_neighbors",
|
|
94
|
+
"get_default_interface_ip",
|
|
95
|
+
"pack_message",
|
|
96
|
+
"parse_transfer_request",
|
|
97
|
+
"read_frame",
|
|
98
|
+
"receive_file",
|
|
99
|
+
"resolve_unique_path",
|
|
100
|
+
"sanitize_filename",
|
|
101
|
+
"send_file",
|
|
102
|
+
"unpack_header",
|
|
103
|
+
]
|