Python网络编程——通过指定的端口和协议找到服务名

1.通过指定的端口和协议找到对应的服务名,采用socket中getservbyprot()函数实现。

 1 import socket
 2 
 3 
 4 def find_service_name():
 5     protocolname = 'tcp'
 6     for port in [80, 25]:
 7         print("Port: %s => service name: %s" % (port, socket.getservbyport(port, protocolname)))
 8 
 9     print("Port: %s => service name: %s" % (53, socket.getservbyport(53, 'udp')))
10 
11 if __name__ == '__main__':
12     find_service_name()

2.输出结果

1 Port: 80 => service name: http
2 Port: 25 => service name: smtp
3 Port: 53 => service name: domain

3.socket.getservbyport()解释

 1 def getservbyport(port, protocolname=None): # real signature unknown; restored from __doc__
 2     """
 3     getservbyport(port[, protocolname]) -> string
 4 
 5     Return the service name from a port number and protocol name.
 6     The optional protocol name, if given, should be 'tcp' or 'udp',
 7     otherwise any protocol will match.
 8     """
 9     return
10 # 获取对应端口的服务名

 

转载于:https://www.cnblogs.com/ouyangyixuan/p/5871946.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值