2018年1月9日 星期二

Python: Find local IP addresses using Python's stdlib

Python: Find local IP addresses using Python's stdlib

Python Basic: Exercise-55 with Solution

Write a Python to find local IP addresses using Python's stdlib.
Sample Solution:-
import socket
print([l for l in ([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] 
if not ip.startswith("127.")][:1], [[(s.connect(('8.8.8.8', 53)), 
s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, 
socket.SOCK_DGRAM)]][0][1]]) if l][0][0])

========== RESTART: F:/Python_APSC/py-ex-basic-55.py ===========
192.168.1.101
>>> 

沒有留言:

張貼留言

ASK 調變模擬器程式碼

ASK 調變模擬器程式碼 為什麼第二張圖要「放大 (Zoom)」? 1 kHz  的週期是 1000 us 。 如果不放大,第二張圖會因為波形太密而看起來像一塊實心的紅磚。 import tkinter as tk from tkinter import ttk import n...