Use Python to implement bulk query IP address attribution address

Source: Internet
Author: User
Tags python script

Today you need to query the client IP nginx access is the same as scheduling!
First use the shell to intercept the IP in the file:

The Python script looks like this: (Haha, the novice wrote it hastily)
#!/usr/bin/env
#--Coding:utf-8-

Import JSON
Import Urllib
Import socket
Import Sys,os,re
Import Signal

url = "http://ip.taobao.com/service/getIpInfo.php?ip="

Def ip_list ():
IP_Address = []
With open ('/tmp/gcl/ip.txt ', ' R ') as F:
For IP in F.readlines ():
If IP! = None:
Ip_address.append (IP)
F.close ()
For I in IP_Address:
City_address = ip_local (i)
Print I.strip () + ":" + city_address
def ip_local (IP):
data = Urllib.urlopen (URL + IP). Read ()
Datadict=json.loads (data)
For Oneinfo in Datadict:
If "code" = = Oneinfo:
If datadict[oneinfo] = = 0:
return datadict["Data" ["City"]

Ip_list ()

Unfortunately, it's finished. You cannot redirect the results to a TXT file, manually copy ....
And that's how it turns out!

You can actually do all the work with a Python script ....
Wait till I get a little deeper.

Use Python to implement bulk query IP address attribution address

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.