Advertisement
FlyFar

networking

Feb 28th, 2023
842
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | Cybersecurity | 0 0
  1. import socket
  2. from urllib.request import urlopen
  3. import urllib
  4.  
  5. def get_private_ip():
  6.     """
  7.    Gets private IP address of this machine.
  8.    This will be used for scanning other computers on LAN.
  9.    Returns:
  10.        private IP address
  11.    """
  12.     ip = socket.gethostbyname(socket.gethostname())
  13.     return ip
  14.  
  15.  
  16. def get_public_ip():
  17.     """
  18.    Gets public IP address of this network.
  19.    You can access the router with this ip too.
  20.    Returns:
  21.        public IP address
  22.    """
  23.     import re
  24.     data = str(urlopen('http://checkip.dyndns.com/').read())
  25.     return re.compile(r'Address: (\d+.\d+.\d+.\d+)').search(data).group(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement