基于Python实现IP摄像头访问的用户输入键值提取脚本开发问询
Python Script to Launch Camera Feeds via EC2 NAT
Here's a practical, easy-to-use script that does exactly what you need—takes a vehicle identifier, handles port input (or auto-maps if you predefine pairs), and opens the camera feed in your default browser.
Basic Version (Manual Port Input)
This version prompts for both the vehicle ID and port, with basic validation to avoid invalid inputs breaking the flow:
import webbrowser def launch_camera_feed(): # Get vehicle identifier from user vehicle_id = input("Enter the vehicle identifier (e.g., truck22): ").strip() if not vehicle_id: print("Error: Vehicle identifier can't be empty!") return # Get and validate port number while True: port_input = input(f"Enter the port for {vehicle_id}: ").strip() if port_input.isdigit(): port = int(port_input) if 1 <= port <= 65535: break else: print("Error: Port must be between 1 and 65535.") else: print("Error: Please enter a valid numeric port.") # Replace with your EC2 instance's public IP ec2_public_ip = "YOUR_EC2_PUBLIC_IP" camera_url = f"http://{ec2_public_ip}:{port}" # Open the URL in default browser print(f"Launching camera feed for {vehicle_id} at {camera_url}...") webbrowser.open(camera_url) if __name__ == "__main__": launch_camera_feed()
Pro Version (Predefined Vehicle-Port Mapping)
Since you have hundreds of cameras, manually entering ports every time will get tedious. This version uses a pre-configured dictionary to map vehicle IDs directly to ports—no more port typing:
import webbrowser # Predefine your vehicle-port mappings here CAMERA_PORT_MAP = { "truck22": 8001, "van15": 8002, "bus07": 8003, # Add all your vehicle-port pairs here } def launch_camera_feed(): vehicle_id = input("Enter the vehicle identifier (e.g., truck22): ").strip() if vehicle_id not in CAMERA_PORT_MAP: print(f"Error: No port found for vehicle {vehicle_id}. Check your input or update the mapping.") return port = CAMERA_PORT_MAP[vehicle_id] ec2_public_ip = "YOUR_EC2_PUBLIC_IP" camera_url = f"http://{ec2_public_ip}:{port}" print(f"Launching camera feed for {vehicle_id} at {camera_url}...") webbrowser.open(camera_url) if __name__ == "__main__": launch_camera_feed()
Quick Tips:
- Replace
YOUR_EC2_PUBLIC_IPwith your actual EC2 instance's public IP (e.g.,1.2.3.4). - The
webbrowsermodule is part of Python's standard library—no extra packages to install. - For the pro version, if maintaining a huge dictionary in code is messy, you can load the mapping from a CSV/JSON file instead. Just add a quick function to read the file and populate
CAMERA_PORT_MAP.
内容的提问来源于stack exchange,提问作者Alec M
相关产品推荐
相关产品推荐

