Source code for cgl.core.utils.network
# from ctypes import windll
import logging
#
[docs]
def get_all_drives():
"""
Returns all drives computer is connected to.
Return:
List of drives
"""
import win32api
drives = win32api.GetLogicalDriveStrings()
drives = drives.split('\\\000')[:-1]
return drives
[docs]
def get_local_drives():
"""
returns all the local drives. (Probably a more elegant way to do this.)
Returns:
List of local drives
"""
all_drives = get_all_drives()
network_drives = get_all_connected_network_drives()
for n in network_drives:
letter = n['local']
if letter in all_drives:
all_drives.remove(letter)
return all_drives
[docs]
def get_all_connected_network_drives(simple=False):
"""
Get all network drives mapped on current computer
Returns:
List of dictionaries for each connected network drive
Example:
{ "Y:" : {"local" : "Y:", "remote" : "\\10.0.0.100\VPShare"}}
"""
return_list = []
resume = 0
import win32net
while 1:
(_drives, total, resume) = win32net.NetUseEnum(None, 0, resume)
for drive in _drives:
if drive['local'] and drive['remote']:
if not simple:
return_list.append(drive)
else:
return_list.append(drive['local'])
return_list.append(drive['remote'])
if not resume: break
return return_list
[docs]
def check_for_network_drive_connection(address, drive):
"""
Check if network drive is currently mapped to computer
Args:
address: Network location
drive: Drive that network is mapped to
Returns:
True if network is found. False is network address is not found
"""
network_list = get_all_connected_network_drives()
for network in network_list:
if network['remote'] == address and network['local'] == drive:
return True
elif network['remote'] == address and not network['local'] == drive:
logging.debug("Network {} found! But it is not mapped to drive {}".format(address, drive))
return True
return False
[docs]
def map_network_drive(drive, address, user, password):
"""
Args:
drive: drive letter - should be in the following: "Y"
address:
user:
password:
Returns:
True if network is found. False is network address is not found
"""
from cgl.core.utils.general import cgl_execute
if ":" in drive:
drive = drive.replace(':', '')
command = "net use {}: {} /user:{} {}".format(drive, address, user, password)
output = cgl_execute(command, return_output=True, print_output=True)
if output['printout'][0] == "System error 53 has occurred.":
logging.info("Error: Network Path {} Not Found".format(address))
return False
if output['printout'][0] == "The command completed successfully.":
logging.info("Network {} Successfully Mapped to {}".format(address, drive))
return True
[docs]
def get_ip_address(external=False):
"""
returns the computer's external ip address.
Returns:
ip address
"""
import socket
import requests
if external:
ip_address = requests.get("https://api.ipify.org").content.decode('utf8')
else:
computer = socket.gethostname()
ip_address = socket.gethostbyname(computer)
return ip_address
[docs]
def get_machine_name():
"""
Returns the local machine's hostname.
Cross-platform, no dependencies.
"""
import socket
try:
return socket.gethostname()
except Exception:
return None