Obtain the Host IP address using Python

Source: Internet
Author: User

Linux:

Import socket import fcntl import struct def get_ip_address (ifname): S = socket. socket (socket. af_inet, socket. sock_dgram) return socket. inet_ntoa (fcntl. IOCTL (S. fileno (), 0x8915, # siocgifaddr struct. pack ('256s ', ifname [: 15]) [20:24]) # get_ip_address ('lo') loop address # get_ip_address ('eth0') Host IP Address

Windows:

Import re, urllib2 from subprocess import popen, pipe print "the private IP address of the local machine is:" + RE. search ('/d + /. /d + /. /d + /. /d + ', popen ('ipconfig', stdout = pipe ). stdout. read ()). group (0) print "the public IP address of the local machine is:" + RE. search ('/d + /. /d + /. /d + /. /d + ', urllib2.urlopen ("http://www.whereismyip.com "). read ()). group (0)

Source: http://blog.csdn.net/imzkz/article/details/5577794

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.