Because recently in Learning Network programming, like to play with IP addresses.
First, I need to figure out how to get the native IP.
1, the use of Linux system
Enter ifconfig in bash to get the following code:
(EV1) [Email protected] bin]#ifconfiglo Link encap:local Loopback inet addr:127.0.0.1Mask:255.0.0.0Inet6 Addr: ::1/ -scope:host up LOOPBACK RUNNING MTU:16436Metric:1RX Packets:8647Errors0Dropped0Overruns:0Frame0TX Packets:8647Errors0Dropped0Overruns:0Carrier0Collisions:0Txqueuelen:0RX Bytes:1768577(1.6MiB) TX Bytes:1768577(1.6MiB) ppp0 Link encap:point-to-Point Protocol inet addr:121.249.203.104P-t-p:192.168.100.1Mask:255.255.255.255Up pointopoint RUNNING noarp multicast MTU:1492Metric:1RX Packets:773361Errors0Dropped0Overruns:0Frame0TX Packets:693122Errors0Dropped0Overruns:0Carrier0Collisions:0Txqueuelen:3RX Bytes:956180205(911.8MiB) TX Bytes:74066832(70.6MiB) p4p1 Link encap:ethernet HWaddr -: fe:b5:c5:cb:0b inet6 Addr:2001:D A8:7013:d 129:16fe:b5ff:fec5:cb0b/ -Scope:global Inet6 addr:fe80::16fe:b5ff:fec5:cb0b/ -scope:link up broadcast RUNNING multicast MTU: theMetric:1RX Packets:1681710Errors0Dropped0Overruns:0Frame0TX Packets:710030Errors0Dropped0Overruns:0Carrier0Collisions:0Txqueuelen: +RX Bytes:1087076647(1.0GiB) TX Bytes:91505807(87.2MiB) Interrupt: theBase Address:0x4000wlan0 Link encap:ethernet HWaddr AC: the: the: in: B5: AboutInet6 addr:fe80::ae72:89ff:fe71:b574/ -scope:link up broadcast multicast MTU: theMetric:1RX Packets: theErrors0Dropped0Overruns:0Frame0TX Packets: theErrors0Dropped0Overruns:0Carrier0Collisions:0Txqueuelen: +RX Bytes:5265(5.1KiB) TX Bytes:6045(5.9KiB)
From the PPP0 (point-to-point) network, you can view the IP address as 121.249.203.104.
2. Use Python language to get native IP
Python 3.3.3 (Default, Sep 6 2014, 10:53:40) [GCC4.4.6 20120305 (Red Hat 4.4.6-4)] on Linuxtype" Help","Copyright","credits" or "License" forMore information.>>>ImportSocket>>>Print(Socket.gethostbyname (Socket.gethostname ()))219.146.13.36
The native IP address is 219.146.13.36.
3, oddly, use the following code to establish a socket socket interface connection with any HTTP host
Import Socket Import sysport=80Host="www.zhihu.com"]sock= Socket.socket (Socket.af_inet,socket. Sock_stream) Sock.connect ((host,port))print(Sock.getsockname (), Sock.getpeername ())
Sock.getsockname (), the Sock.getpeername () method returns the local IP address, port, and the IP address and port of the server for this socket connection.
('121.249.203.104', 44447) ('60.28.215.71', 80)
The 121.249.203.104 is the local IP that is connected to the current set of interfaces.
What is the native IP? Not yet ready to order.
linux--acquiring IP and the problems found in it