Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- def get_local_ip():
- """Get the local IP address of the PC."""
- try:
- # Use subprocess to execute 'ipconfig' command on Windows or 'ifconfig' command on Unix-based systems
- result = subprocess.run(['ipconfig'], capture_output=True, text=True)
- if result.returncode == 0:
- output = result.stdout
- # Extracting IPv4 address from the output
- local_ip_index = output.find('IPv4 Address')
- if local_ip_index != -1:
- local_ip_start = output.find(':', local_ip_index)
- local_ip_end = output.find('\n', local_ip_start)
- local_ip = output[local_ip_start + 2: local_ip_end].strip()
- return local_ip
- else:
- print("Error:", result.stderr)
- except Exception as e:
- print("Error:", e)
- return None
- local_ip = get_local_ip()
- if local_ip:
- print("Local IP:", local_ip)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement