-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_transfer_xp.py
More file actions
410 lines (356 loc) · 16.5 KB
/
file_transfer_xp.py
File metadata and controls
410 lines (356 loc) · 16.5 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import socket
import os
import sys
import shutil
import json
import time
from datetime import datetime
# Optional Windows APIs for printing
try:
import win32api # type: ignore
import win32print # type: ignore
except Exception:
win32api = None # type: ignore
win32print = None # type: ignore
# Basic configuration (will be overridden by config file if present)
CONFIG = {
"port": 25565,
"printer_name": "No Printer",
"print_filetypes": [".pdf", ".png"],
}
PORT = CONFIG["port"]
CHUNK_SIZE = 8192 # Smaller chunks for compatibility
def get_application_path():
return os.path.dirname(os.path.abspath(sys.argv[0]))
def get_config_path():
return os.path.join(get_application_path(), "pyprint-filetransfer-config.json")
def load_config():
global CONFIG, PORT
try:
cfg_path = get_config_path()
if os.path.exists(cfg_path):
f = open(cfg_path, 'r')
try:
data = json.load(f)
finally:
f.close()
# merge known keys only
for k in list(CONFIG.keys()):
if k in data:
CONFIG[k] = data[k]
# normalize extensions
if isinstance(CONFIG.get("print_filetypes"), list):
CONFIG["print_filetypes"] = [
(ft if str(ft).startswith('.') else ('.' + str(ft))).lower()
for ft in CONFIG["print_filetypes"]
]
PORT = int(CONFIG.get("port", 25565))
log("Loaded config from %s" % cfg_path)
except Exception as e:
log("Warning: failed to load config: %s" % str(e))
def print_file(filepath):
try:
printer_name = CONFIG.get("printer_name", "No Printer")
if not printer_name or printer_name == "No Printer":
return
# Default printer
if printer_name == "Default Printer":
try:
os.startfile(filepath, "print")
log("Sent %s to default printer" % os.path.basename(filepath))
return
except Exception as e:
log("Default print failed, trying printto: %s" % str(e))
# Specific printer via ShellExecute 'printto'
if win32api is not None:
try:
win32api.ShellExecute(0, 'printto', filepath, '"%s"' % printer_name, '.', 0) # type: ignore
log("Sent %s to printer '%s'" % (os.path.basename(filepath), printer_name))
return
except Exception as e:
log("printto failed: %s" % str(e))
# Fallback: temporarily set default and use startfile
if win32print is not None:
try:
current = None
try:
current = win32print.GetDefaultPrinter() # type: ignore
except Exception:
current = None
try:
win32print.SetDefaultPrinter(printer_name) # type: ignore
os.startfile(filepath, "print")
log("Printed %s via temporary default '%s'" % (os.path.basename(filepath), printer_name))
finally:
try:
if current:
win32print.SetDefaultPrinter(current) # type: ignore
except Exception:
pass
except Exception as e:
log("Temp default print failed: %s" % str(e))
except Exception as e:
log("Error printing file: %s" % str(e))
def log(message):
"""Write message to log and console"""
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log_message = "[%s] %s" % (timestamp, message)
print(log_message)
try:
with open("file_transfer.log", "a") as f:
f.write(log_message + "\n")
except:
pass
def send_mode(server_ip):
"""Client mode - watch for files and send them"""
base_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
sent_dir = os.path.join(base_dir, "sent")
# Create sent folder if it doesn't exist
if not os.path.exists(sent_dir):
try:
os.makedirs(sent_dir)
except Exception as e:
log("Error creating sent directory: %s" % str(e))
return
log("Running in SEND mode - watching for files to send to %s:%d" % (server_ip, PORT))
log("Press Ctrl+C to stop")
# Main loop - watch directory for files
try:
while True:
try:
# Get list of files in current directory
files = os.listdir(base_dir)
for filename in files:
# Full path to file
filepath = os.path.join(base_dir, filename)
# Skip directories, hidden files, and special files
if (os.path.isfile(filepath) and
not filename.startswith('.') and
not filename.endswith('.exe') and
not filename.endswith('.pyc') and
not filename.endswith('.pyd') and
not filename.endswith('.dll') and
filename != 'pyprint-filetransfer-config.json' and
not filename.endswith('.log') and
not filename == os.path.basename(sys.argv[0])):
# Process the file
try:
# Get file size
file_size = os.path.getsize(filepath)
# Skip empty files
if file_size == 0:
log("Skipping empty file: %s" % filename)
continue
# Connect to server
log("Sending file: %s (%d bytes)" % (filename, file_size))
log("Connecting to %s:%d..." % (server_ip, PORT))
# Create socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(30) # 30 second timeout
try:
# Connect to server
s.connect((server_ip, PORT))
# Send filename length (8 bytes)
name_bytes = filename.encode('utf-8')
name_len = str(len(name_bytes)).zfill(8).encode('ascii')
s.sendall(name_len)
# Send filename
s.sendall(name_bytes)
# Send file size (16 bytes)
size_str = str(file_size).zfill(16).encode('ascii')
s.sendall(size_str)
# Send file data in chunks
with open(filepath, 'rb') as f:
bytes_sent = 0
while bytes_sent < file_size:
chunk = f.read(CHUNK_SIZE)
if not chunk:
break
s.sendall(chunk)
bytes_sent += len(chunk)
# Successful transfer
log("Successfully sent %s (%d bytes)" % (filename, bytes_sent))
# Move file to sent folder
dest_path = os.path.join(sent_dir, filename)
log("Moving file to sent folder")
shutil.move(filepath, dest_path)
log("File moved to: %s" % dest_path)
except socket.error as e:
log("Socket error: %s" % str(e))
finally:
s.close()
except Exception as e:
log("Error processing file %s: %s" % (filename, str(e)))
# Wait before checking for new files
time.sleep(1)
except KeyboardInterrupt:
log("Stopping file monitoring")
break
except Exception as e:
log("Error in main loop: %s" % str(e))
time.sleep(5) # Longer delay on error
except Exception as e:
log("Fatal error: %s" % str(e))
def receive_mode(listen_ip=None):
"""Server mode - listen for connections and receive files"""
base_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
received_dir = os.path.join(base_dir, "received")
# Create received folder if it doesn't exist
if not os.path.exists(received_dir):
try:
os.makedirs(received_dir)
except Exception as e:
log("Error creating received directory: %s" % str(e))
return
# Create server socket
server = None
try:
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Bind to specified or all interfaces
ip = listen_ip or ''
server.bind((ip, PORT))
server.listen(5)
log("Running in RECEIVE mode - listening on %s:%d" % (ip or '*', PORT))
log("Files will be saved to: %s" % received_dir)
log("Press Ctrl+C to stop")
# Main server loop
while True:
try:
# Accept connection
client, addr = server.accept()
log("Connection from: %s:%d" % addr)
try:
# Set timeout
client.settimeout(30)
# Get filename length
name_len_data = client.recv(8)
if not name_len_data:
log("Client disconnected")
client.close()
continue
# Parse filename length
try:
name_len = int(name_len_data.decode('ascii'))
except:
log("Invalid filename length received")
client.close()
continue
# Get filename
name_data = client.recv(name_len)
if not name_data:
log("No filename received")
client.close()
continue
# Parse filename
try:
filename = name_data.decode('utf-8')
except:
log("Invalid filename received")
client.close()
continue
# Get file size
size_data = client.recv(16)
if not size_data:
log("No file size received")
client.close()
continue
# Parse file size
try:
file_size = int(size_data.decode('ascii'))
except:
log("Invalid file size received")
client.close()
continue
# Prepare output file
log("Receiving file: %s (%d bytes)" % (filename, file_size))
output_path = os.path.join(received_dir, filename)
# Receive file data
try:
received = 0
with open(output_path, 'wb') as f:
while received < file_size:
# Calculate remaining bytes
remaining = file_size - received
# Read chunk (or remaining bytes if smaller)
chunk = client.recv(min(CHUNK_SIZE, remaining))
if not chunk:
log("Connection lost during transfer")
break
# Write chunk
f.write(chunk)
received += len(chunk)
# Check if transfer was complete
if received == file_size:
log("Successfully received file: %s (%d bytes)" % (filename, received))
# Auto-print if configured and extension allowed
try:
ext = os.path.splitext(output_path)[1].lower()
ftypes = set(CONFIG.get("print_filetypes", [".pdf", ".png"]))
if CONFIG.get("printer_name", "No Printer") != "No Printer" and ext in ftypes:
print_file(output_path)
except Exception as e:
log("Auto print failed: %s" % str(e))
else:
log("Incomplete file received: %s (%d of %d bytes)" %
(filename, received, file_size))
except Exception as e:
log("Error saving file: %s" % str(e))
except Exception as e:
log("Error handling client: %s" % str(e))
finally:
client.close()
except KeyboardInterrupt:
log("Server stopping")
break
except Exception as e:
log("Server error: %s" % str(e))
except Exception as e:
log("Fatal server error: %s" % str(e))
finally:
try:
if server:
server.close()
except:
pass
log("Server stopped")
def print_help():
print("\nWindows XP File Transfer Utility")
print("===============================\n")
print("Usage:")
print(" As receiver: %s receive [ip_to_listen_on]" % os.path.basename(sys.argv[0]))
print(" As sender: %s send server_ip" % os.path.basename(sys.argv[0]))
print("\nExamples:")
print(" %s receive" % os.path.basename(sys.argv[0]))
print(" %s receive 192.168.1.100" % os.path.basename(sys.argv[0]))
print(" %s send 192.168.1.100\n" % os.path.basename(sys.argv[0]))
if __name__ == "__main__":
try:
load_config()
# No args = print help
if len(sys.argv) < 2:
print_help()
sys.exit(1)
# Get mode
mode = sys.argv[1].lower()
# Process based on mode
if mode == "receive":
# Get listen IP if provided
listen_ip = sys.argv[2] if len(sys.argv) > 2 else None
receive_mode(listen_ip)
elif mode == "send":
# Must provide server IP
if len(sys.argv) < 3:
print("Error: Server IP address required for send mode")
print_help()
sys.exit(1)
send_mode(sys.argv[2])
elif mode in ("-h", "--help", "help"):
print_help()
else:
print("Error: Unknown mode '%s'" % mode)
print_help()
sys.exit(1)
except Exception as e:
print("Error: %s" % str(e))
sys.exit(1)