Skip to content

Commit 84b6ff3

Browse files
committed
Improved commands
1 parent b5b898c commit 84b6ff3

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

static/index.html

+6-4
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ <h3 id="curl">Using cURL</h3>
3535
curl -JLO https://transit.sh/<code style="color:#fbfbaa">music-for-dad</code>/</pre>
3636
<h3 id="python">Using Python</h3>
3737
<p>This <code>python</code> one-liner uses websockets and doesn't have any limitations.<br>The script will install the <a href="https://pypi.org/project/websockets/">websockets</a> package then ask for your file and identifier.<br>Just paste the following in your terminal:</p>
38-
<pre><b># Send</b>
39-
python -c 'exec("""\nimport pip\npip.main(["install", "websockets"])\nimport json\nimport asyncio\nimport websockets\nfrom pathlib import Path\ndef ask_for_path():\n while True:\n path = Path(input("File path: ")).resolve()\n if path.is_file():\n break\n print("Invalid file path. Example: /tmp/file.txt /Users/jean/Documents/movie.mp4")\n return path\ndef ask_for_identifier():\n while True:\n identifer = input("ID: ")\n if identifer is not None:\n break\n return identifer\nasync def send():\n file_path = ask_for_path()\n identifier = ask_for_identifier()\n async with websockets.connect(f"wss://transit.sh/send/{identifier}") as websocket:\n file = Path(file_path).resolve()\n file_name, file_size, file_type = file.name, file.stat().st_size, "application/octet-stream"\n header = json.dumps({"file_name": file_name, "file_size": file_size, "file_type": file_type})\n await websocket.send(header)\n print("Waiting for other peer to connect...")\n while (msg := await websocket.recv()) != "Go for file chunks":\n print(f"Unexpected message: {msg}")\n print("Peer connected. Starting transfer.")\n with file.open("rb") as fd:\n bytes_read = 0\n while chunk := fd.read(2**15):\n await websocket.send(chunk)\n bytes_read += len(chunk)\n print(f"Transfering... {bytes_read/file_size*100:.2f}%", end="\\r", flush=True)\n await websocket.send(str().encode())\n print("\\nDone.")\nasyncio.run(send())\n""")'
38+
<pre>
39+
<b># Send</b>
40+
python -c 'exec("""\nimport subprocess\nsubprocess.run(["pip", "install", "-q", "websockets"], check=False)\n\nimport json\nimport asyncio\nimport mimetypes\nimport websockets\nfrom pathlib import Path\n\ndef ask_for_path():\n while True:\n path = Path(input("\\nFile path (you can drag-and-drop): ")).resolve()\n if path.exists() and path.is_file():\n break\n print(f"{path} is not a valid file.")\n return path\n\ndef ask_for_identifier():\n while True:\n identifier = input("Transfer ID: ")\n if identifier:\n return identifier\n\nasync def send():\n file_path = ask_for_path()\n identifier = ask_for_identifier()\n\n async with websockets.connect(f"wss://transit.sh/send/{identifier}") as websocket:\n file = Path(file_path).resolve()\n file_name, file_size, file_type = file.name, file.stat().st_size, mimetypes.guess_type(file.name, strict=False)[0]\n header = json.dumps({"file_name": file_name, "file_size": file_size, "file_type": file_type})\n await websocket.send(header)\n\n print("Waiting for other peer to connect...")\n while (msg := await websocket.recv()) != "Go for file chunks":\n print(f"Unexpected message: {msg}")\n \n print("Peer connected. Starting transfer.")\n with file.open("rb") as fd:\n bytes_read = 0\n while chunk := fd.read(2**15):\n await websocket.send(chunk)\n bytes_read += len(chunk)\n print(f"Transfering... {bytes_read/file_size*100:.2f}%", end="\\r", flush=True)\n await websocket.send(str().encode())\n print("\\nDone.")\n\nasyncio.run(send())\n""")'
4041
</pre>
41-
<pre><b># Receive</b>
42-
python -c 'exec("""\nimport pip\npip.main(["install", "websockets"])\nimport json\nimport asyncio\nimport websockets\nfrom pathlib import Path\ndef ask_for_identifier():\n while True:\n identifer = input("ID: ")\n if identifer is not None:\n break\n return identifer\nasync def receive():\n identifier = ask_for_identifier()\n path = Path(".").resolve()\n async with websockets.connect(f"wss://transit.sh/receive/{identifier}") as websocket:\n if (response := await websocket.recv()) == "File not found":\n print(f"No transfer with ID {identifier} found.")\n return\n else:\n header = json.loads(response)\n file_name, file_size, file_type = header["file_name"], float(header["file_size"]), header["file_type"]\n print(f"File: {file_name} - Size: {file_size / 1024**2:.2f}MB")\n with open(path / file_name, "wb") as fd:\n await websocket.send("Go for file chunks")\n bytes_received = 0\n while (chunk := await websocket.recv()) != b"":\n fd.write(chunk)\n bytes_received += len(chunk)\n print(f"Receiving... {bytes_received/file_size*100:.2f}%", end="\\r", flush=True)\n print("\\nTransfer complete.")\nasyncio.run(receive())\n""")'
42+
<pre>
43+
<b># Receive</b>
44+
python -c 'exec("""\nimport subprocess\nsubprocess.run(["pip", "install", "-q", "websockets"], check=False)\n\nimport json\nimport asyncio\nfrom pathlib import Path\nfrom websockets import connect\n\ndef ask_for_path():\n while True:\n path = Path(input("\\nDest. directory: ")).resolve()\n if path.exists() and path.is_dir():\n break\n print(f"{path} is not a valid directory.")\n return path\n\ndef ask_for_identifier():\n while True:\n identifier = input("Transfer ID: ")\n if identifier:\n return identifier\n\nasync def receive():\n path = ask_for_path()\n identifier = ask_for_identifier()\n\n async with connect(f"wss://transit.sh/receive/{identifier}") as websocket:\n response = await websocket.recv()\n if response == "File not found":\n print(f"No transfer with ID {identifier} found.")\n return\n\n header = json.loads(response)\n file_name, file_size, file_type = header["file_name"], header["file_size"], header["file_type"]\n print(f"File: {file_name} - Size: {file_size / 1024**2:.2f}MB\\nSaving to: {path / file_name}")\n\n with open(path / file_name, "wb") as fd:\n await websocket.send("Go for file chunks")\n bytes_received = 0\n while (chunk := await websocket.recv()) != b"":\n fd.write(chunk)\n bytes_received += len(chunk)\n print(f"Receiving... {bytes_received/file_size*100:.2f}%", end="\\r", flush=True)\n\n print("\\nTransfer complete.")\n\nasyncio.run(receive())\n""")'
4345
</pre>
4446
<p>The received file will be saved in the same directory the script was executed.</p>
4547
<h2 id="information">Information</h3>

0 commit comments

Comments
 (0)