Linux under Python get IP address code _python

Source: Internet
Author: User
Tags get ip

"Lnmp a key installation package" needs to obtain IP address, there are 2 kinds of cases: if the server only private network address does not have public network address , this time to obtain the IP (that is, private network address) can not be used to determine the location of the server, so take its gateway address Used to determine whether the server is domestic or foreign (script to enable domestic users to download quickly, the Yum source automatically set to 163, this situation needs to obtain the gateway address); If the server has a public network address , then the IP address can be obtained to directly determine the location of the server.

Get the server IP, if there is a public network address to take public network address, no public network address to access the private Web site
Here's how I used the shell to get the local IP script:

ip= ' Ifconfig | grep ' inet addr: ' | Cut-d:-f2 | Grep-v ^10\. | Grep-v ^192\.168 | Grep-v ^172\. | \
grep-v ^127\. | awk ' {print '} ' | awk ' {print;exit} '
 
[!-n ' $IP '] && ip= ' ifconfig | grep ' inet addr :' | Cut-d:-f2 | Grep-v ^127\. | \
awk ' {print '} ' | awk ' {print;exit} '

Python version: get_local_ip.py:

#!/usr/bin/env python
import socket
def get_local_ip (): "" "
 Returns the actual IP of the local machine.< C5/>this code figures out what source address would is used if some traffic out to IS were out to
 sent down some add Ress on the Internet. In this case
 , a Google DNS server was used, but the specific address does not
 matter much. No traffic is actually sent.
 "" " Try:
  csock = Socket.socket (socket.af_inet, socket. SOCK_DGRAM)
  csock.connect ((' 8.8.8.8 ', +))
  (addr, port) = Csock.getsockname ()
  csock.close
  () Return addr
 except Socket.error: Return
  "127.0.0.1"
 
if __name__ = "__main__":
 local_ip = get_ LOCAL_IP () 
 print local_ip

have a public network address directly to obtain, no public network address to get the gateway address (used to determine the geographical location of IP address):get_public_ip.py

#!/usr/bin/env python
import re,urllib2
class Get_public_ip:
 def getip (self):
  try:
   Myip = Self.visit ("http://www.whereismyip.com/")
  except:
   try:
    Myip = Self.visit ("http://www.ip138.com/ Ip2city.asp ")
   except:
    Myip =" so sorry!!! "
  Return MYIP
 def visit (Self,url):
  opener = Urllib2.urlopen (URL)
  if url = = Opener.geturl ():
   str = Opener.read () return
  re.search (' \d+\.\d+\.\d+\.\d+ ', str). Group (0)
 
if __name__ = = "__main__":
 Getmyip = Get_public_ip ()
 print Getmyip.getip ()

Determine server IP location: get_ip_area.py

 #!/usr/bin/env python #coding: utf-8 try:import Sys,urllib2,json apiurl = "http://ip.taobao.com /service/getipinfo.php?ip=%s "% sys.argv[1] content = Urllib2.urlopen (apiurl). Read () data = json.loads (content) [' Data ' ] Code = json.loads (content) [' Code '] if code = = 0:print data[' country_id '] else:print data except:print ' Usage: %s IP '% sys.argv[0] 
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.