三分钟学会Python字典
如果我们要用python写一个端口扫描工具,那我们该如何定义与维护所要扫描的端口呢?
上一篇文章使用了列表(List)变量进行维护,但不是很灵活,比如只能通过索引来获取端口,加入我要获取stmp端口,我只能去看定义属索引了...我想直接获取指定端口数据,怎么办?使用字典
Python的字典(Dic)变量类型提供了一个可以存储任意数量的python对象的哈希表(键值对)。使用字典存储的对象内部是无序的,即不支持索引,但支持我们所需要的根据端口类型(key)获取端口号(value)的方式。使用示例代码如下:
1. ****新建字典
新建一个字典存储已知的http、https默认端口

2. ****增加字典元素
这时要新增ftp和smtp的默认端口,怎么办?

3. ****获取字典长度
想要获取当前已添加了多少个端口了,怎么办?

4. ****字段键与值得获取
• 获取字典所有已存入的键****(key)
想要获取当前都添加了哪些类型的端口,怎么办?

• 获取字典中已存入键的对应值****(value)
想要直接获取smtp对应的端口值,怎么办?

5. ****判断键值对是否存在
忘记ssh端口是否添加过了,怎么办?

6. ****删除字典中的指定键值对
我不想要扫描https,也不好渗透,怎么删除?

网友评论