Python 2 to 3 Conversion
Wildman
best_lay at yahoo.com
Sat Feb 17 00:15:25 EST 2018
I have a bit of code I found on the web that will return
the ip address of the named network interface. The code
is for Python 2 and it runs fine. But, I want to use the
code with Python 3. Below is the code followed by the error
message. Suggestions appreciated.
#!/usr/bin/env python3
import socket
import fcntl
import struct
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
print(get_ip_address("eth0"))
print(get_ip_address("lo"))
Traceback (most recent call last):
File "./test.py", line 14, in <module>
print(get_ip_address("eth0"))
File "./test.py", line 12, in get_ip_address
struct.pack('256s', ifname[:15])
struct.error: argument for 's' must be a bytes object
--
<Wildman> GNU/Linux user #557453
The cow died so I don't need your bull!
More information about the Python-list
mailing list