10 examples of 'get ip address python' in Python

Every line of 'get ip address python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
33def ipaddress(addr):
34 """Return an IPy.IP object from the given string IP."""
35 return _IPy.IP(addr)
170def ip_address(ifname):
171 """
172 Get the IP address associated with an interface.
173 """
174
175 with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as tmp_socket:
176 return socket.inet_ntoa(fcntl.ioctl(
177 tmp_socket.fileno(),
178 0x8915, # SIOCGIFADDR
179 struct.pack('256s', ifname[:15].encode())
180 )[20:24])
43def _is_ipaddress(address: Optional[HostAddress]) -> bool:
44 if address is None:
45 return False
46
47 try:
48 socket.inet_pton(socket.AF_INET, address)
49 return True
50 except socket.error:
51 # not a ipv4 address
52 pass
53
54 try:
55 socket.inet_pton(socket.AF_INET6, address)
56 return True
57 except socket.error:
58 # no ipv6 address either
59 return False
72def get_ip():
73 """Returns one of the IP addresses of the associated hosts"""
74 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
75 sock.connect(('1.1.1.1', 9000)) # IP and port are arbitrary
76 ip = sock.getsockname()[0] # returns (hostaddr, port)
77 sock.shutdown(socket.SHUT_RDWR)
78 sock.close()
79 return ip
133def get_ip(device):
134 """ Get the IP, as string, of a device """
135 return socket.inet_ntoa(device.address)
23def get_ip():
24 try:
25 hostname = socket.getfqdn(socket.gethostname())
26 ipaddr = socket.gethostbyname(hostname)
27 except Exception as msg:
28 print(msg)
29 ipaddr = ''
30 return ipaddr
78def get_ip_address(self, interface='eth0'):
79 # Just return '127.0.0.1'. Laptops are often only connected
80 # on wlan0, and looking for eth0 would return an empty string.
81 return '127.0.0.1'
59def ip_to_network(address, prefix):
60 """
61 Returns CIDR notation to represent the address and its prefix.
62 """
63 network = ip_network(unicode("{}/{}".format(address, prefix)),
64 strict=False)
65 return "{}/{}".format(network.network_address, prefix)
53def _get_my_ip():
54 """Return the actual ip of the local machine.
55
56 This code figures out what source address would be used if some traffic
57 were to be sent out to some well known address on the Internet. In this
58 case, a Google DNS server is used, but the specific address does not
59 matter much. No traffic is actually sent.
60 """
61 csock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
62 csock.connect(('8.8.8.8', 80))
63 (addr, _port) = csock.getsockname()
64 csock.close()
65 return addr
105def host_ipaddr(interface = None):
106 if sys.platform == "win32":
107 return HostUtil.get_inst().gethostbyname()
108
109 cmd = "/sbin/ifconfig"
110 if interface:
111 cmd = '%s %s' % (cmd, interface)
112 fd = os.popen(cmd)
113
114 this_host = None
115 interfaces = {}
116 name = None
117
118 for line in fd.readlines():
119 match = re.match("(\S+)", line)
120 if match: name = match.group(1)
121 match = re.search("inet addr:(\S+)", line)
122 if match:
123 addr = match.group(1)
124 if name:
125 interfaces[name] = addr
126
127 if interfaces.has_key(interface):
128 this_host = interfaces[interface]
129 else:
130 for name, addr in interfaces.items():
131 if re.match("ppp", name):
132 this_host = addr
133 break
134 elif re.match("eth", name):
135 this_host = addr
136
137 fd.close()
138
139 return this_host or HostUtil.get_inst().gethostbyname()

Related snippets