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