代码如下:
# -*- coding: utf-8 -*- import socket def find_service_name(): protocolname = 'tcp' for port in [80, 25]: print 'Port:%s =>service name:%s' % (port, socket.getservbyport(port, protocolname)) print 'Port:%s =>service name:%s' % (53, socket.getservbyport(53, 'udp')) if __name__ == '__main__': find_service_name()
运行结果如下:
Port:80 =>service name:http Port:53 =>service name:domain Port:25 =>service name:smtp Port:53 =>service name:domain