50行Python代码输出各种网络协议报文结构的纯文本表格图 - Go语言中文社区

50行Python代码输出各种网络协议报文结构的纯文本表格图


经常与TCP/IP协议族打交道,免不了要画各种协议报文的纯文本结构图。为何需要纯文本结构图?想想在工程的源代码中,您能贴张jpeg或png的图片上去?

     示范代码以输出 IP 包头为例, 使用python 2.7 编写(Python 3 应该也能运行)。本程序还可以用来画类似结构的表格图,再次抛砖引玉,欢迎大家使用交流。


代码

  1. #!/usr/bin/env python  
  2. # -*- coding: utf-8 -*-  
  3. ''''' 
  4. Copyright (c) 2014, Thomas Hu. All rights reserved. 
  5.  
  6. Created on 2014-8-3 
  7.  
  8. @author: Thomas Hu 
  9. @email : thomashtq#163.com 
  10.  
  11. '''  
  12. import math  
  13. from string import ljust, rjust, center  
  14.   
  15. def format_protocol(head_list, just_type):  
  16.     just_dict = {"ljust": ljust,  
  17.                  "rjust": rjust,  
  18.                  "center": center,  
  19.                  }  
  20.     just_func = just_dict.get(just_type.lower(), center)  
  21.     vchar = "|"  
  22.     head = vchar  
  23.     width = 2  
  24.     max_key = 0  
  25.     # Get the maximum width  
  26.     for item in head_list:  
  27.         tmp = math.ceil(float(len(item[0])) / item[1])  
  28.         if tmp > width:  
  29.             width = int(tmp)  
  30.         if len(item[0]) > max_key:  
  31.             max_key = len(item[0])  
  32.     for i in range(32):  
  33.         head += " "*(width - len(str(i))) + str(i)  + vchar  
  34.     hline = vchar + "-" *(len(head) - 2) + vchar  
  35.     print(head)  
  36.     print(hline)  
  37.     index = 0  
  38.     line = vchar  
  39.     # Print protocol head  
  40.     for item in head_list:  
  41.         line += item[0].center(item[1] * (width + 1) - 1" ") + vchar  
  42.         index += item[1]  
  43.         if index % 32 == 0:  
  44.             print(line)  
  45.             print(hline)  
  46.             line = vchar  
  47.     # Print protocol description  
  48.     print("rnThe protocol head items description are as follows:")  
  49.     for item in head_list:  
  50.         print("%s : %s"%(just_func(item[0], max_key, " "), item[2]))  
  51.               
  52. if __name__ == '__main__':  
  53.     head_list = [("Ver"4"Protocol version"),   
  54.                  ("Head"4"Head length(in bytes)"),   
  55.                  ("TOS"8"Type of service"),   
  56.                  ("Total Length"16"The total length of IP packet(in bytes)"),  
  57.                  ("ID"16"The identification of packet"),   
  58.                  ("Flag"3"The flag"),   
  59.                  ("Offset"13"The offset"),  
  60.                  ("TTL"8"Time to live"),  
  61.                  ("Proto"8"The protocol id"),  
  62.                  ("Checksum"16"The head check sum"),  
  63.                  ("Source Address"32"The source IP address"),  
  64.                  ("Destination Address"32"The destination IP address"),  
  65.                  ("Opitons(if has)"32"The options block, if has that"),  
  66.                  ("Data"32"The data block of IP packet"),  
  67.                  ]  
  68.     format_protocol(head_list, "ljust")  


输出结果

 
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/chengxuyuanyonghu/article/details/41078205
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢