File transfer complete

This commit is contained in:
david rice
2026-04-07 15:58:01 +01:00
parent 6e65c85c07
commit c19e8138df
3 changed files with 65 additions and 24 deletions

Binary file not shown.

View File

@@ -1,32 +1,36 @@
import socket import socket
from pathlib import Path
from smb.SMBConnection import SMBConnection from smb.SMBConnection import SMBConnection
SERVER = "192.168.45.4" SERVER = "192.168.45.4"
SHARE = "AGILENT" SHARE = "AGILENT"
USERNAME = "" # leave empty for guest/anonymous USERNAME = ""
PASSWORD = "" PASSWORD = ""
CLIENT_NAME = socket.gethostname() CLIENT_NAME = socket.gethostname()
SERVER_NAME = SERVER # use IP for UNC path in TREE_CONNECT SERVER_NAME = SERVER
DATA_DIR = Path(__file__).parent / "data"
def list_csv_files(server, share, username="", password=""): def _connect():
"""Search an SMB1 share recursively and return all .csv file paths."""
conn = SMBConnection( conn = SMBConnection(
username, password, USERNAME, PASSWORD,
CLIENT_NAME, SERVER_NAME, CLIENT_NAME, SERVER_NAME,
use_ntlm_v2=True, use_ntlm_v2=True,
is_direct_tcp=True # use port 445 directly is_direct_tcp=True
) )
if not conn.connect(SERVER, 445):
raise ConnectionError("Failed to connect to SMB share.")
return conn
if not conn.connect(server, 445):
print("Failed to connect to the share.")
return []
def list_csv_files(server=SERVER, share=SHARE, username=USERNAME, password=PASSWORD):
"""Return a list of all .csv paths found on the share (recursive)."""
conn = _connect()
csv_files = [] csv_files = []
def walk(path): def walk(path):
entries = conn.listPath(share, path) for entry in conn.listPath(share, path):
for entry in entries:
if entry.filename in (".", ".."): if entry.filename in (".", ".."):
continue continue
full_path = f"{path}/{entry.filename}" full_path = f"{path}/{entry.filename}"
@@ -43,13 +47,52 @@ def list_csv_files(server, share, username="", password=""):
return csv_files return csv_files
if __name__ == "__main__": def transfer_csv_files():
print(f"Searching \\\\{SERVER}\\{SHARE} for CSV files...\n") """
files = list_csv_files(SERVER, SHARE, USERNAME, PASSWORD) Copy all .csv files from the scope share to DATA_DIR,
then delete them from the scope.
Returns (copied, failed) counts.
"""
DATA_DIR.mkdir(exist_ok=True)
conn = _connect()
copied = 0
failed = 0
if files: try:
print(f"Found {len(files)} CSV file(s):") csv_files = []
for f in files:
print(f" {f}") def walk(path):
else: for entry in conn.listPath(SHARE, path):
print("No CSV files found.") if entry.filename in (".", ".."):
continue
full_path = f"{path}/{entry.filename}"
if entry.isDirectory:
walk(full_path)
elif entry.filename.lower().endswith(".csv"):
csv_files.append(full_path)
walk("/")
for remote_path in csv_files:
filename = Path(remote_path).name
local_path = DATA_DIR / filename
try:
with open(local_path, "wb") as f:
conn.retrieveFile(SHARE, remote_path, f)
conn.deleteFiles(SHARE, remote_path)
print(f" TRANSFERRED: {filename}")
copied += 1
except Exception as e:
print(f" FAILED: {filename}{e}")
failed += 1
finally:
conn.close()
return copied, failed
if __name__ == "__main__":
print(f"Transferring CSV files from \\\\{SERVER}\\{SHARE} to {DATA_DIR}...\n")
copied, failed = transfer_csv_files()
print(f"\nDone. {copied} transferred, {failed} failed.")

View File

@@ -265,10 +265,8 @@ def mgmt_worker():
print("[MGMT] PAUSING TEST — RUNNING MANAGEMENT TASKS...") print("[MGMT] PAUSING TEST — RUNNING MANAGEMENT TASKS...")
resume_event.clear() resume_event.clear()
try: try:
files = ai_mgmt.list_csv_files(ai_mgmt.SERVER, ai_mgmt.SHARE) copied, failed = ai_mgmt.transfer_csv_files()
print(f"[MGMT] FOUND {len(files)} CSV FILE(S) ON SCOPE DRIVE.") print(f"[MGMT] TRANSFERRED {copied} FILE(S) TO DATA FOLDER. {failed} FAILED.")
for f in files:
print(f" {f}")
except Exception as e: except Exception as e:
print(f"[MGMT] ERROR: {e}") print(f"[MGMT] ERROR: {e}")
finally: finally: