2017-10-30 3 views
0

私はこのpythonを実行します。すべて順調。なぜconfigparserでローカルIPを取得できないのですか?

import socket 
import sys 
def get_local_ip(ifname): 
    print type(ifname) 
    import fcntl, struct 
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
    inet = fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15])) 
    ret = socket.inet_ntoa(inet[20:24]) 
    return ret 

アウトが

<type 'str'> 
192.168.1.250 

を置くしかし、私はここに
[config] 
ethname = 'eth0' 

socketConfig.conf設定ファイルここ

私の設定ファイルで設定しようとしたPythonコード

です
import socket 
import sys 
import configparser 
conf = configparser.ConfigParser() 
conf.read("socketConfig.conf") 


def get_local_ip(ifname): 
    print type(ifname) 
    import fcntl, struct 
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
    inet = fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15])) 
    ret = socket.inet_ntoa(inet[20:24]) 
    return ret 

eth = conf.get('config', 'ethname').encode('utf-8') 
print get_local_ip(eth) 

その後、アウトこれらのエラーを置く

<type 'str'> 
Traceback (most recent call last): 
    File "socketClient.py", line 28, in <module> 
    print get_local_ip(eth) 
    File "socketClient.py", line 23, in get_local_ip 
    inet = fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15])) 
IOError: [Errno 19] No such device 

は、誰もが何が起こっているか知っていますか?ありがとうございました。

+0

'print eth'。私はそれがあなたの考えではないと仮定しています。 – Shadow

+0

@ Shadow試しました。 display 'eth0' – user3751111

+0

あなたの設定ファイルの引用符を削除してみてください。見積もりがピックアップされて渡されているようです。 – Shadow

答えて

1

設定オプションの一部として引用符が選択されています。

設定ファイルは次のようになります。

[config] 
ethname = eth0 

引用符の省略に注意してください。

関連する問題