New to the index function. I am trying to display the hostname and IP address of my PC, but I'm stuck at this index part. If I comment it out and print the whole list, what I'm looking for is right there. So why can't .index see it?
#!/usr/bin/python3.6
import socket
import string
import subprocess
import tkinter
from tkinter import *
hostname = socket.gethostname()
arg='ip route list'
p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)
data = p.communicate()
sdata = data[0].split()
ipaddr = sdata[ sdata.index('src')+1 ]
root = tkinter.Tk()
root.title(hostname)
L1 = Label(root, text=ipaddr)
L1.grid(row=1,column=2,sticky=W+E,padx=100,pady=10)
root.mainloop()
Contents of sdata:
[b'default', b'via', b'10.30.5.1', b'dev', b'ens33', b'proto', b'dhcp',
b'metric', b'100', b'10.30.5.0/24', b'dev', b'ens33', b'proto', b'kernel',
b'scope', b'link', b'src', b'10.30.5.7', b'metric', b'100', b'169.254.0.0/16',
b'dev', b'ens33', b'scope', b'link', b'metric', b'1000']