Python obtains local and remote host information

Source: Internet
Author: User
#-*-Coding: cp936-*-import socketprint "Creating socket connecting... ", s = socket. socket (socket. AF_INET, socket. SOCK_STREAM) # convert the network service name (such as http, ftp, email) and Protocol name (such as TCP, UDP, IPX/SPX) # To the port number corresponding to the service name. If the protocol name is specified, it must be TCP or UDP # Otherwise it will match other protocol port = socket. getservbyname ("http", "tcp") print "conection to remote host by port % d" % port result = s. connect_ex ("www.baidu.com", port) # display the local IP address and port number that link to the remote host print "Connected from", s. getsockname () # x displays the local IP address and port number print "Connected to", s. getpeername () # testif result! = 0: print "waiting..." else: print "Done .! "



The result is as follows:

Creating socket connecting... conection to remote host by port 80
Connected from ('192. 168.1.102 ', 192)
Connected to ('192. 181.112.143 ', 80)
Done .!

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.