下面介紹在linux上利用python獲rvzxfghp取本機ip的方法.
經過網上調查, 發現大致有兩種方法, 一種是呼叫shell指令碼,另一種是利用python中的s等模組來得到,下面是這兩種方法的原始碼:
#!/usr/bin/env python
#encoding: utf-8
#description: get local ip address
import os
import socket, fcntl, struct
def get_ip():
#注意外圍使用雙引號而非單引號,並且假設預設是第乙個網絡卡,特殊環境請適當修改**
out = os.popen("ifconfig | grep 'inet ad程式設計客棧dr:' | grep -v '127.0.0.1' | cut -d: -f2 | awk '' | head -1").read()
print out
#另一種方法, 只需要指定網絡卡介面, 我更傾向於這個方法
def get_ip2(ifname):
s = socket.socket(socket.af_inet, socket.sock_dgram程式設計客棧)
return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))[20:24])
if __name__ == '__main__':
get_ip()
print get_ip2('eth0')
print get_ip2('lo')
下面是執行截圖
參考文獻
[1].
總結本文標題: linux下通過python獲取本機ip方法示例
本文位址:
python中根據時間獲取週數,通過週數獲取時間
時間 時間和週數 import time import datetime 獲取今天是第幾周 print time.strftime w 獲取當前是週幾 0 6,0代表周一 today datetime.datetime.now weekday 獲取指定日期屬於當年的第幾周 week datetime...
python通過呼叫百度天氣API介面獲取天氣資訊
coding utf 8 author herman tang import requests import re import json import csv import sys import os apikey eiaoyez8eo9mpu3wkaz0wda64xagu2fu city inp...
Linux下通過ODBC連線SQL Server
一 測試環境 作業系統 fedora 8 資料庫 sqlserver2000 installed in windows xp professional,資料庫有使用者sa,密碼是syth7777,資料庫檔案是qjkzdb 注意 1 ms從來沒有提供過sqlserver for linux,所以也不要...