I run this python. everything is fine.
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
out put
<type 'str'>
192.168.1.250
But I tried to set a config file
here is my config file socketConfig.conf
[config]
ethname = 'eth0'
here is the python code
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)
then out put these error
<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
Is anyone knows what is going on? thank you.
The quotes are being picked up as part of the configuration option.
Your configuration file should look like this;
[config]
ethname = eth0
Note the omission of quotes.