通过指定的端口号和协议获得服务名
如果想找到网络服务,就要知道该服务运行在tcp,udp的哪个端口上
import socket
def find_serv_name():
protocolname = 'tcp'
for each in [80,15]:
try:
print ("PORT: %s,PROTOCOL: %s,==>SERVER: %s" % (each,protocolname,socket.getservbyport(each,protocolname)))
except socket.error as err_msg:
print ("PORT: %s --- %s" %(each,err_msg))
print ("PORT: %s,PROTOCOL: %s, ==>SERVER: %s " % (53,'udp',socket.getservbyport(53,'udp')))
if __name__ == '__main__':
find_serv_name()
socket.getservbyport(serv,proto) 找不到端口号在协议上对应的服务时
抛出socket.error异常