Python-Python tutorial

Source: Internet
Author: User
This article describes how to implement ip address query in python. For more information, see the following code.

Handler

The code is as follows:


Import OS, time

Def getip (filepath ):
Ip2city = {}
File = open (filepath, 'r ')
Lines = file. readlines ()
File. close ()
For line in lines:
Ip = line. split ('') [0]
City = line. split ('') [1]
Haship = hashm (ip)
If haship in ip2city:
Pass
Else:
Ip2city [haship] = city
Print ('hash done! ')
Return ip2city

Def hashm (ip ):
Iplist = ip. split ('.')
Ip = int (iplist [0]) * 4 + int (iplist [1]) * 2 + int (iplist [2])
Return ip

Def getcityfromip (filepath, ipandcity ):
Outputstr = []
For file in OS. listdir (filepath ):
File_handler = open (filepath + '\' + file, 'r ')
Line = file_handler.readline ()
While line:
Ip = hashm (line. rstrip ())
If ip in ipandcity:
Outputstr. append (line. rstrip () + ''+ ipandcity [ip])
Line = file_handler.readline ()
File_handler.close ()
Outfile_handler = open (filepath + '\ '{file.split('.'{0}}'_out.txt', 'A + ')
Outfile_handler.writelines (outputstr)
Outfile_handler.close ()
Print(file.split('.'{}0}}'_out.txt '+ 'Done! ')

Def splitfile (filepath ):
File = open (filepath, 'r ')
Blocking _size = 8000000
Filecount = 1
Temp = []
Count = 0
Line = file. readline ()
While line or temp:
If count = block_size:
Wfile = open ('d: \ ipfile \ file_'{str(filecount}'.txt ', 'A + ')
Wfile. writelines (temp)
Temp = []
Count = 0
Wfile. close ()
Filecount + = 1
Print ('Split '+ str (filecount) + 'Done! ')
Else:
Count + = 1
Temp. append (line)
Line = file. readline ()
File. close ()
Return OS. path. join ('d: \ '+ 'ipfile ')

If _ name _ = '_ main __':
Start = time. clock ()
Filepath = 'd: \ ip.txt'
Ippath = 'd: \ citys.txt'
Ip2city = getip (ippath)
Splitfilepath = splitfile (filepath)
Getcityfromip ('d: \ '+ 'ipfile', ip2city)
End = time. clock ()
Print (end-start)

Generate IP

The code is as follows:


# Generate 100 millon ip
Import random
Import time

Def generateIpAdd (file, num ):
Ip = []
File = open (file, 'A + ')
For I in range (num ):
IpAdd = '1970. 192. '+ str (random. randint (168) +'. '+ str (random. randint (0,255 ))
Ip. append (ipAdd + '\ n ')
File. writelines (ip)
File. close ()

If _ name __= = '_ main __':
Start = time. clock ()
For I in range (10000 ):
GenerateIpAdd ('d: \ ip.txt ', 10000)
End = time. clock ()
Print (end-start)

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.