如何使用python将协议编号转换为名称? [英] how to turn protocol number to name with python?

查看:80
本文介绍了如何使用python将协议编号转换为名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像tcp和udp这样的协议都是用数字表示的.

the protocol like tcp and udp is all represented by a number.

import socket
socket.getprotocobyname('tcp') 

上面的代码会返回6.

如果我知道协议编号,如何获得协议名称?

How I can get the protocol name if I know the protocol number?

推荐答案

我想说几乎肯定有比这更好的方法,但所有协议名称(和值)都存储为以 "IPPROTO" 这样你就可以通过迭代模块中的值来创建一个查找表:

I'm going to say there is almost definitely a better way then this, but all the protocol names (and values) are stored as constants prefixed by "IPPROTO" so you can create a lookup table by iterating over the values in the module:

import socket

table = {num:name[8:] for name,num in vars(socket).items() if name.startswith("IPPROTO")}

>>> table[6]
'TCP'

这篇关于如何使用python将协议编号转换为名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆