Python detects service names via ports and protocols

Source: Internet
Author: User

Find the service name with the specified port and protocol if you want to find a network service, it is best to know which port the service is running on TCP or UDP protocol. If you know the port that the Network service uses, you can call the Getservbyport () function in the socket library to get the name of the service.


Technical Point decomposition:

1, define the Find_service_name () function, note the code indentation in the function

2, Getservbyport (Port,port_type), through the port lookup service,port_type for ' TCP ' and ' UDP ' two

3, For-in loop a set of variables. In each traversal, get the service name for the port


The code is as follows:

Import Socketdef find_service_name (): Port_type = ' tcp ' port_type1 = ' udp ' for port in [25,80,22]: Print    ("Port:%s = Service Name:%s"% (Port,socket.getservbyport (port,port_type))) Print ("Port:%s = = Service Name:%s"% (in Socket.getservbyport (+ (), ' UDP ')) Find_service_name ()

Note: Socket.getservbyport This function defines the port kind too little, after testing like 3306 MySQL This common display error, only as the test use it, personal feeling not much practical value

Operation Result:

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/9E/81/wKioL1mSohbz7ehQAAA7vhPXu6o038.jpg-wh_500x0-wm_ 3-wmp_4-s_523623929.jpg "title=" qq20170815152251.jpg "alt=" Wkiol1msohbz7ehqaaa7vhpxu6o038.jpg-wh_50 "/>


This article is from the "Old White blog" blog, please be sure to keep this source http://laobaiv1.blog.51cto.com/2893832/1956451

Python detects service names via ports and protocols

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.