#!/usr/bin/env python3 """ Windows Print Service Connection Tester Tests connectivity between Flask server and Windows Print Service """ import sys import os sys.path.append(os.path.join(os.path.dirname(__file__), 'app')) import requests import time from app.print_config import WINDOWS_PRINT_SERVICE_URL, PRINT_SERVICE_TIMEOUT def test_print_service_connection(): """Test connection to Windows Print Service""" print("=== Windows Print Service Connection Test ===\n") print(f"šŸ” Testing connection to: {WINDOWS_PRINT_SERVICE_URL}") print(f"ā±ļø Timeout: {PRINT_SERVICE_TIMEOUT} seconds") print(f"🌐 From: Flask server (Linux)") print(f"šŸŽÆ To: Windows Print Service") print() # Test 1: Health check print("1ļøāƒ£ Testing /health endpoint...") try: start_time = time.time() response = requests.get( f"{WINDOWS_PRINT_SERVICE_URL}/health", timeout=PRINT_SERVICE_TIMEOUT ) elapsed = time.time() - start_time if response.status_code == 200: data = response.json() print(f" āœ… SUCCESS (Status: {response.status_code}, Time: {elapsed:.2f}s)") print(f" šŸ“‹ Service: {data.get('service', 'Unknown')}") print(f" šŸ–„ļø Platform: {data.get('platform', 'Unknown')}") print(f" šŸ“… Timestamp: {data.get('timestamp', 'Unknown')}") health_ok = True else: print(f" āŒ FAILED (Status: {response.status_code})") print(f" šŸ“ Response: {response.text}") health_ok = False except requests.exceptions.ConnectionError as e: print(f" āŒ CONNECTION REFUSED") print(f" šŸ”§ Error: {str(e)}") print(f" šŸ’” This usually means:") print(f" • Windows service is not running") print(f" • Wrong IP address in configuration") print(f" • Firewall blocking connection") print(f" • Network routing issue") health_ok = False except Exception as e: print(f" āŒ ERROR: {str(e)}") health_ok = False print() # Test 2: Printers endpoint (only if health check passed) if health_ok: print("2ļøāƒ£ Testing /printers endpoint...") try: response = requests.get( f"{WINDOWS_PRINT_SERVICE_URL}/printers", timeout=PRINT_SERVICE_TIMEOUT ) if response.status_code == 200: data = response.json() printers = data.get('printers', []) print(f" āœ… SUCCESS - Found {len(printers)} printer(s)") for i, printer in enumerate(printers[:3]): # Show first 3 name = printer.get('name', 'Unknown') if isinstance(printer, dict) else printer print(f" šŸ–Øļø {i+1}. {name}") if len(printers) > 3: print(f" ... and {len(printers)-3} more") else: print(f" āŒ FAILED (Status: {response.status_code})") except Exception as e: print(f" āŒ ERROR: {str(e)}") print() # Test 3: Network diagnostics print("3ļøāƒ£ Network Diagnostics...") # Parse URL to get host and port try: from urllib.parse import urlparse parsed = urlparse(WINDOWS_PRINT_SERVICE_URL) host = parsed.hostname port = parsed.port or 8765 print(f" 🌐 Target Host: {host}") print(f" šŸ”Œ Target Port: {port}") # Test DNS resolution try: import socket ip = socket.gethostbyname(host) print(f" šŸ” DNS Resolution: {host} → {ip}") dns_ok = True except Exception as e: print(f" āŒ DNS Resolution failed: {e}") dns_ok = False # Test raw TCP connection if dns_ok: try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(5) result = sock.connect_ex((host, port)) sock.close() if result == 0: print(f" āœ… TCP Connection: Port {port} is open") else: print(f" āŒ TCP Connection: Port {port} is closed or filtered") print(f" šŸ’” Check Windows Firewall and service status") except Exception as e: print(f" āŒ TCP Test error: {e}") except Exception as e: print(f" āŒ URL parsing error: {e}") print() # Final recommendations print("šŸ”§ TROUBLESHOOTING RECOMMENDATIONS:") print() if not health_ok: print("āŒ CONNECTION FAILED - Try these steps:") print("1. Verify Windows Print Service is running:") print(" • Windows: sc query QualityLabelPrinting") print(" • Or: Get-Service QualityLabelPrinting") print() print("2. Check Windows machine IP address:") print(" • Windows: ipconfig | findstr IPv4") print(" • Update WINDOWS_PRINT_SERVICE_URL in print_config.py") print() print("3. Test firewall:") print(" • Windows: Allow port 8765 in Windows Firewall") print(" • Test: telnet [WINDOWS_IP] 8765") print() print("4. If same machine, verify service binding:") print(" • Service should bind to 0.0.0.0:8765 (not 127.0.0.1)") else: print("āœ… CONNECTION SUCCESS - Print service should work!") print("• Service is reachable and responding") print("• Ready for label printing") print("• Try printing labels from the web interface") print(f"\nšŸ“ Current Configuration:") print(f" Service URL: {WINDOWS_PRINT_SERVICE_URL}") print(f" Timeout: {PRINT_SERVICE_TIMEOUT}s") return health_ok if __name__ == "__main__": test_print_service_connection()