Python Forum
Need help can't find out how to do it.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help can't find out how to do it.
#1
I got a task at where I am learning python to write a code that let me see ip of lo eth and nettwork card.
Then i got tast to print out both the ethernet and the nettwork card at same time.
I stuck there since still pretty new to python and don't know how to do it and been trying to find out for few days with no luck. Any suggestion?

#!/usr/bin/env python
def finn_ip():
import netifaces


netti = netifaces.interfaces()
for i in netti:

print i
p = raw_input('skriv inn her: ')
ia = netifaces.ifaddresses(p)
if 2 in ia.keys():
return ia[2][0]['addr']
else:
return False


ip = finn_ip()
if ip is False:
print 'No IP found'
else:
print ip
Reply
#2
Please post your code in Python code tags. You can find help here.
Reply
#3
I got a task at where I am learning python to write a code that let me see ip of lo eth and nettwork card.
Then i got tast to print out both the ethernet and the nettwork card at same time.
I stuck there since still pretty new to python and don't know how to do it and been trying to find out for few days with no luck. Any suggestion?


#!/usr/bin/env python
def finn_ip():
 import netifaces


 netti = netifaces.interfaces()
 for i in netti:
     print i
 p = raw_input('skriv inn her: ')
 ia = netifaces.ifaddresses(p)
 if 2 in ia.keys():
     return ia[2][0]['addr']
 else:
     return False


ip =  finn_ip()
if ip is False:
    print 'No IP found'
else:
    print ip
Reply
#4
First don't have imports inside function definition, leave it outside, at beginning of your Python code.
Then your indentation seems off. Indent by 4 (four) spaces what you want inside finn_ip function, not 1 space.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020