Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Python Networking

You're reading from   Learning Python Networking Utilize Python 3 to get network applications up and running quickly and easily

Arrow left icon
Product type Paperback
Published in Jun 2015
Publisher
ISBN-13 9781784396008
Length 320 pages
Edition 1st Edition
Languages
Concepts
Arrow right icon
Toc

Table of Contents (12) Chapters Close

Preface 1. Network Programming and Python 2. HTTP and Working with the Web FREE CHAPTER 3. APIs in Action 4. Engaging with E-mails 5. Interacting with Remote Systems 6. IP and DNS 7. Programming with Sockets 8. Client and Server Applications 9. Applications for the Web A. Working with Wireshark Index

Retrieving the network configuration of a local machine


Before doing anything else, let's ask in the Python language, What's my name?. In networking terms, this is equivalent to finding out the machine's name or the host's name. On the shell command-line, this can be discovered by using the hostname command. In Python, you can do this by using the socket module.

>>> import socket
>>> socket.gethostname()
'debian6box.localdomain.loc'

Now, we would like to see the local machine IP. This can be seen by using the ifconfig command in Linux and by using the ipconfig command in the Windows OS. But, we'd like to do this in Python by using the following built-in function:

>>> socket.gethostbyname('debian6box.localdomain.loc')
'10.0.2.15'

As you can see, this is the IP of the first network interface. It can also show us the IP of the loopback interface (127.0.0.1) if your DNS or hostfile has not been configured properly. In Linux/UNIX, the following line can be added to...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime