Python and Domain Name System (DNS) forward query

Source: Internet
Author: User

The Domain Name System (DNS) is a distributed database used to replace the host name with an IP address.

There are two reasons for DNS:

(1) It makes it easy for users to remember their names, rather than pure IP addresses;

(2) allow the server to change the IP address, but use the original domain name.

The most basic query in the system is forward query, which searches for IP addresses based on a host name. For example, if you want to download a web page from www.example.com, you must first find the IP address. The query will help you complete this task. It will translate a name into an IP address.

A socket. getaddrinfo () function is defined in Python to complete this forward query. It is defined as follows:

Getaddrinfo (host, Port [, family [, sockettype [, proto [, flags])

The host parameter is the domain name you want to search for. Other parameters are used only when you want to directly pass the result to socket. socket () or socket. Connect.

Below is a small example of forward query:

Import sys, socketresult = socket. getaddrinfo (sys. argv [1], none, 0, socket. sock_stream) Counter = 0; for item in result: Print "%-2D: % s" % (counter, item [4]) Counter + = 1

Running result:

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.