Summary of how python obtains an Internet IP Address

Source: Internet
Author: User

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.

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.