10 examples of 'python get local ip' in Python

Every line of 'python get local ip' 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
this disclaimer
15def local_ip():
16 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
17 s.connect(("8.8.8.8", 80))
18 try:
19 return s.getsockname()[0]
20 finally:
21 s.close()
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

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
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
75@staticmethod
76def get_local_ip():
77 try:
78 return [l for l in (
79 [ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1], [
80 [(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in
81 [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) if l][0][0]
82 except:
83 return None
16def get_my_ip():
17 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
18 s.connect(("8.8.8.8", 80))
19 print(s.getsockname()[0])
20 s.close()
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
165def get_local_ip_addresses():
166 """Get a list of non loopback IP addresses configured on the local host.
167
168 :rtype: list[str]
169 """
170 addresses = []
171 for interface in netifaces.interfaces():
172 for address in netifaces.ifaddresses(interface).get(2, []):
173 if address["addr"] != "127.0.0.1":
174 addresses.append(address["addr"])
175 return addresses
25def get_local_ip(target_host: Optional[str] = None) -> str:
26 """Try to get the local IP of this machine, used to talk to target_url."""
27 target_host = target_host or EXTERNAL_IP
28 target_port = 80
29
30 try:
31 temp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
32 temp_sock.connect((target_host, target_port))
33 local_ip = temp_sock.getsockname()[0] # type: str
34 return local_ip
35 finally:
36 temp_sock.close()
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'
113def get_ip(req=None):
114 """ Returns the IP address of the currently in scope request. The approach is to define a list of trusted proxies
115 (in this case the local network), and only trust the most recently defined untrusted IP address.
116 Taken from http://stackoverflow.com/a/22936947/4285524 but the generator there makes no sense.
117 The trusted_proxies regexes is taken from Ruby on Rails.
118
119 This has issues if the clients are also on the local network so you can remove proxies from config.py.
120
121 CTFd does not use IP address for anything besides cursory tracking of teams and it is ill-advised to do much
122 more than that if you do not know what you're doing.
123 """
124 if req is None:
125 req = request
126 trusted_proxies = app.config["TRUSTED_PROXIES"]
127 combined = "(" + ")|(".join(trusted_proxies) + ")"
128 route = req.access_route + [req.remote_addr]
129 for addr in reversed(route):
130 if not re.match(combined, addr): # IP is not trusted but we trust the proxies
131 remote_addr = addr
132 break
133 else:
134 remote_addr = req.remote_addr
135 return remote_addr

Related snippets