-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexample.py
More file actions
57 lines (43 loc) 路 1.58 KB
/
Copy pathexample.py
File metadata and controls
57 lines (43 loc) 路 1.58 KB
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
import asyncio
import contextlib
import sys
from logging import DEBUG, basicConfig
import pyquickshare
import pyquickshare.common
from pyquickshare.mdns.send import trigger_devices
basicConfig(level=pyquickshare.common.SILLY)
async def send(file: str) -> None:
quickshare = await pyquickshare.discover_services()
quickshare.qr_code.print()
async for device in quickshare:
if device.connectable:
return await device.send(file=file)
return None
async def main(argv: list[str]) -> None:
if len(argv) < 2:
print("Usage: example.py <mode> [args...]")
return
if argv[1] == "send":
if len(argv) < 3:
print("Usage: example.py send <file>")
return
await send(argv[2])
elif argv[1] == "receive":
# if you want to appear like different devices, you can change the endpoint_id like this:
# asnyc for request in pyquickshare.receive(endpoint_id=b"1234"):
# (the endpoint_id must be 4 bytes long, you can use pyquickshare.generate_enpoint_id())
async for request in pyquickshare.receive(endpoint_id=pyquickshare.generate_endpoint_id()):
results = await request.accept()
print(results)
elif argv[1] == "advertise":
pyquickshare.common.create_task(trigger_devices())
else:
print("Unknown mode:", argv[1])
async def wrapper():
try:
await main(sys.argv)
finally:
await pyquickshare.common.clear_tasks()
if __name__ == "__main__":
with contextlib.suppress(KeyboardInterrupt):
asyncio.run(wrapper())