同时打印所选的选择名称

时间:2016-06-08 07:39:56

标签: python python-2.7

我无法放置用于打印所选选项名称的print命令。代码如下:

import dpkt
from dpkt.ip import IP
from dpkt.ethernet import Ethernet
import struct
import socket
import csv
def ip_to_str(address):
    return socket.inet_ntoa(address)

f = open('ipp.pcap', 'rb')
pcap = dpkt.pcap.Reader(f)
loop = 1
while loop ==1:
    print("Select IPv4 Header Fields:")
    print ("1) Ver")
    print ("2) HeaderLength")
    print ("3) Total Length")
    print ("4) Exit")
    ch = input("Choose your option:")
    choice = ch.split(',')
    for ts, buf in pcap: 
         eth = dpkt.ethernet.Ethernet(buf)
         if eth.type != dpkt.ethernet.ETH_TYPE_IP:
            continue
        ip = eth.data
        Length = "%d" % (ip.len)
        TTL = "%d" % (ip.ttl)
        ver = ip.v
        hdrlen = dpkt.ip.IP_HDR_LEN
        ff = ''
        for k in range(0,len(choice)):
            if int(choice[k]) == 1:
                ff=ff + ', '+ str(ver)
                #print ver 
            if int(choice[k]) == 2:
                #print hdrlen
                ff=ff + ',  '+str(hdrlen)
            if int(choice[k]) == 3:
                ff=ff+ ',  '+ str(Length)
            if int(choice[k]) == 4:
                loop = 0
        print ff

其输出如下:

Select IPv4 Header Fields:
1) Ver
2) HeaderLength
3) Total Length
4) Exit
Choose your option:'1,2,3'
, 4,  20,  60
, 4,  20,  60
, 4,  20,  52
, 4,  20,  193
, 4,  20,  341

但是我希望它的头部名称应该如下所示,所以预期的输出是:

Select IPv4 Header Fields:
1) Ver
2) HeaderLength
3) Total Length
4) Exit
Choose your option:'1,2,3'
, Ver, HeaderLength, Total Length
, 4,  20,  60
, 4,  20,  60
, 4,  20,  52

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

在第internal static bool IsValidLocationForInlining { get { // If there's a SynchronizationContext, we'll be conservative and say // this is a bad location to inline. var ctx = SynchronizationContext.CurrentNoFlow; if (ctx != null && ctx.GetType() != typeof(SynchronizationContext)) return false; // Similarly, if there's a non-default TaskScheduler, we'll be conservative // and say this is a bad location to inline. var sched = TaskScheduler.InternalCurrent; return sched == null || sched == TaskScheduler.Default; } }

之后添加此代码
choice = ch.split(',')

我建议您使用以下内容替换while循环:

dict = {'1':'Ver', '2':'HeaderLength', '3':'TotalLength'}
print(', '.join([dict[i] for i in choice if i in dict.keys()]))

您可能还想检查所有选项是否有效。例如,用户可以输入while loop: print("Select IPv4 Header Fields:") print ("1) Ver") print ("2) HeaderLength") print ("3) Total Length") print ("4) Exit") choice = [i.strip() for i in input("Choose your options:").split(',')] if not set(choice)-set(['1','2','3','4']): # check for valid choices print("Enter valid choices\n") continue if '4' in choice: loop = 0 # you want to break out, after executing the next loop for ts, buf in pcap: eth = dpkt.ethernet.Ethernet(buf) if eth.type != dpkt.ethernet.ET_TYPE_IP: continue ip = eth.data data = {'1':str(ip.ver), '2':str(dpkt.ip.IP_HDR_LEN), '3':str(ip.len)} ff = ','.join([data[i] for i in choice if i in data.keys()]) print ff 作为输入。