Determines the IP address of the default network interface. Sends a UDP packet to Cloudflare's DNS (``1.1.1.1``), which should go through the default interface. This populates the source address of the socket, which we then inspect and return.
()
| 293 | |
| 294 | |
| 295 | def _fetch_ip_using_dns(): |
| 296 | # type: () -> str |
| 297 | """ |
| 298 | Determines the IP address of the default network interface. |
| 299 | |
| 300 | Sends a UDP packet to Cloudflare's DNS (``1.1.1.1``), which should go through |
| 301 | the default interface. This populates the source address of the socket, |
| 302 | which we then inspect and return. |
| 303 | """ |
| 304 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 305 | s.connect(("1.1.1.1", 53)) |
| 306 | ip = s.getsockname()[0] |
| 307 | s.close() # NOTE: sockets don't have context manager in 2.7 :( |
| 308 | return ip |
| 309 | |
| 310 | |
| 311 | class Method: |
no test coverage detected