1
如何在Linux和Windows中獲取Python中的網絡信息?我嘗試在Ubuntu 12.10 64位的Python 2.7中使用netinfo軟件包(ver 0.3.2),但是使用這個軟件包會使句柄沒有關閉,如下所示。我的情況沒有被接受。如何在沒有資源泄漏的情況下在python下讀取ip地址
import netinfo def countOpenFiles(): import resource, fcntl, os n_open = 0 names = [] soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) for fd in range(0, soft): try: f = fcntl.fcntl(fd, fcntl.F_GETFD) n_open += 1 except IOError: continue return n_open for i in range(10): netinfo.get_ip('eth0') print countOpenFiles()
它產生:
4 5 6 7 8 9 10 11 12 13
我想有類似的NetInfo包沒有資源泄漏。
感謝您的任何幫助。
我想讀取wlan0接口的ip地址。這個過程是連續的,例如,應該每隔一秒查詢一次。 netinfo.get_ip方法不釋放資源(處理程序),如示例中所示。經過一段時間(python中的1024個處理程序的限制),Python進程退出時出錯。 – rnowak2
感謝您的回答。我發現庫的netifaces運行良好,不會泄漏。 – rnowak2