I have multiple Network Interface Cards on my computer, each with its own IP address.
When I use gethostbyname(gethostname()) from Python’s (built-in) socket module, it will only return one of them. How do I get the others?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use the
netifacesmodule. Because networking is complex, using netifaces can be a little tricky, but here’s how to do what you want:This can be made a little more readable like this:
If you want IPv6 addresses, use
AF_INET6instead ofAF_INET. If you’re wondering whynetifacesuses lists and dictionaries all over the place, it’s because a single computer can have multiple NICs, and each NIC can have multiple addresses, and each address has its own set of options.