Summary of how python obtains an Internet IP Address
This example summarizes how python obtains an Internet IP address. Share it with you for your reference. The details are as follows:
I. Use the Script Engine library to directly obtain
?
1 2 3 4 5 6 7 |
Import console; Import web. script Import inet. http; Var jsVm = web. script ("JavaScript ") JsVm. AddCode (inet. http (). get ("http://fw.qq.com/ipaddress ")) Var ipAddr = jsVm. CodeObject. IPData [0]; Console. log ("your Internet IP Address:", ipAddr) |
Ii. Obtain the http library through mode matching
?
1 2 3 4 5 6 7 8 9 10 |
Import inet. http; GetIp = function (){ Var http = inet. http () Http. flags = 0x80000000/* _ INTERNET_FLAG_RELOAD force file downloading from the server is not a cache */ | 0x4000000/* _ INTERNET_FLAG_DONT_CACHE * No data cache */ Varstr = http. get ("http://fw.qq.com/ipaddress ") Return str? String. match (str, '"(. + ?) "') } Io. open () Io. print (getIp ()) |
Iii. whttp library, get through pattern matching
?
1 2 3 4 5 6 7 8 9 10 11 |
// Obtain the IP address Import inet. whttp; GetIp = function (){ Var whttp = inet. whttp () Var str = whttp. get ("http://www.ip138.com/ip2city.asp? R = "+ tonumber (time. now ())) Whttp. close () Return str? String. match (str ,"\[(.*?) \] "); } Io. open () Io. print (getIp ()) |
Iv. API methods
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Io. open (); Var IPHLPAPI = raw. loadDll ("IPHLPAPI. DLL ") Var GetIpAddrTable = IPHLPAPI. api ("GetIpAddrTable", "int (struct & pIpAddrTable, int & pdwSize, int border )") IPInfo = class { Int dwAddr; Int dwIndex; Int dwMask; Int dwBCastAddr; Int dwReasmSize; Word unused1; Word unused2 } Var PMIB_IPADDRTABLE = class { Int dEntrys; Struct mIPInfo [255] = {.. IPInfo ()}; } Var ipStruct = PMIB_IPADDRTABLE (); Var re, ipStruct, ret = GetIpAddrTable (ipStruct, 0, 1 ); Var re, ipStruct, ret = GetIpAddrTable (ipStruct, re, 0 ); Var ipData = ipStruct. mIPInfo [ipStruct. dEntrys-1]. dwAddr; Var ip = raw. convert ({int n = ipData}, {BYTE data [4]}); Io. print (string. format ("% d. % d. % d. % d ", ip. data [1], ip. data [2], ip. data [3], ip. data [4]); |
I hope this article will help you with Python programming.