Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 文本分析的If语句困境_Python_Parsing_Text Parsing - Fatal编程技术网

Python 文本分析的If语句困境

Python 文本分析的If语句困境,python,parsing,text-parsing,Python,Parsing,Text Parsing,我正在解析一个大文本文件以获得正确的TCP连接,我需要做的一件事是检查服务器和主机IP地址(有30个不同的主机)是否相互发送了一个SYN数据包 主机列表: HostList1 = ['150.100.12.130','150.100.12.131','150.100.12.132','150.100.12.133','150.100.12.134','150.100.12.135','150.100.12.136','150.100.12.137','150.100.12.138','150

我正在解析一个大文本文件以获得正确的TCP连接,我需要做的一件事是检查服务器和主机IP地址(有30个不同的主机)是否相互发送了一个SYN数据包

主机列表:

HostList1 =   ['150.100.12.130','150.100.12.131','150.100.12.132','150.100.12.133','150.100.12.134','150.100.12.135','150.100.12.136','150.100.12.137','150.100.12.138','150.100.12.139']  
文本中的示例行(有数千行):

我的当前代码,服务器地址是150.100.0.2:

with open("serverLanTraceWithErrors.txt" , "r") as ParseFile1:
   for line in ParseFile1:
     if 'TCP' in line:               #Check if TCP proto is in the line
      Split1 = line.split(",")       #Split each line by a comma for future splits                                       
      Split2 = Split1[7].split(">")  #Split1[7] contains the two IP addresses, host and server and the direction of the packet
      Split3 = Split1[1].split(" ")  #Split3[3] will show the # hops 

      for item in HostList1:
          if 'S' in line:            #check if SYN flag is in the line

            if item in Split2[1] and '150.100.0.2' in Split2[2] and '63' in Split3[3]:
               print("SYN packet from host computer: %s to server completed with right # of hops %s" %(item, Split3[3]))

            elif item in Split2[2] and  '150.100.0.2' in Split2[1] and '64' in Split3[3] and 'ack' in Split1[9]:
               print("SYN packet from server to host computer: %s completed with right # of hops %s" %(item, Split3[3])) 

            elif: item hasn't been found sending or receving SYN flag...
               print("SYN pack from wherever to wherever not completed...")
理想情况下,应该有两种情况,在服务器和主机之间找到SYN标志。主机到服务器,服务器回到主机。我的问题是我找不到使最后一个elif语句工作的方法,我需要一种方法来说明,如果前两个语句中只有一个曾经出现过,那么它使用的是哪个IP地址。因为对于主机150.100.12.139,它只向主机发送一个数据包,主机从不回复一个数据包,我需要代码才能指出这一点。我对python的了解非常有限,所以我在这里坐了一段时间,感到很困惑

您说过“理想情况下,应该有两种情况,在服务器和主机之间找到SYN标志”,因此我猜最后一个
elif
条件是检查上述任一条件是否有效。我建议不要使用最后一个
elif
语句,而是尝试使用
else
,如下所示

for item in HostList1:
          if 'S' in line:            #check if SYN flag is in the line

            if item in Split2[1] and '150.100.0.2' in Split2[2] and '63' in Split3[3]:
               print("SYN packet from host computer: %s to server completed with right # of hops %s" %(item, Split3[3]))

            elif item in Split2[2] and  '150.100.0.2' in Split2[1] and '64' in Split3[3] and 'ack' in Split1[9]:
               print("SYN packet from server to host computer: %s completed with right # of hops %s" %(item, Split3[3])) 

            else:  ## item hasn't been found sending or receving SYN flag...
               print("SYN pack from wherever to wherever not completed...")
如果对你有帮助,请告诉我

编辑1: 在研究了这些评论之后,我向你提出以下建议

            if item in Split2[1] and '150.100.0.2' in Split2[2] and '63' in Split3[3]:
               print("SYN packet from host computer: %s to server completed with right # of hops %s" %(item, Split3[3]))

            elif item in Split2[2] and  '150.100.0.2' in Split2[1] and '64' in Split3[3] and 'ack' in Split1[9]:
               print("SYN packet from server to host computer: %s completed with right # of hops %s" %(item, Split3[3])) 

            elif item not in Split2[1] or item in Split2[2] :  ## item hasn't been found sending or receving SYN flag...
               print("SYN pack from wherever to wherever not completed...")

我会这样做:将两个布尔变量(比如a和b)分配给两个if条件的值,并将if更改为“if a和b…”。。。埃利夫a。。。以利夫b…’。然后在第二个和第三个分支中,您知道这两个事件中只有一个发生。谢谢您的回复,我将尝试一下。嘿,谢谢您的回复。如果我写一个这样的语句,问题是它会在其他IP地址不在同一行的情况下打印出来。因此,其中一个打印来自正确的IP地址,但是,如果HostList中的其他IP地址都不在该行中,它也会为它们打印。那么告诉我第三个条件是什么?你到底想检查什么?我想我想最后一句话要检查的是,如果在整个文件的任何一行中都没有找到带有“S”标志的Split2[1]或Split2[2]中的IP地址,那么就打印它是哪个IP地址。您之前的建议基本上就是我要寻找的,只是因为我要逐行打印其他IP地址,而这些IP地址既不在Split2[1]中,也不在Split2[2]中。
            if item in Split2[1] and '150.100.0.2' in Split2[2] and '63' in Split3[3]:
               print("SYN packet from host computer: %s to server completed with right # of hops %s" %(item, Split3[3]))

            elif item in Split2[2] and  '150.100.0.2' in Split2[1] and '64' in Split3[3] and 'ack' in Split1[9]:
               print("SYN packet from server to host computer: %s completed with right # of hops %s" %(item, Split3[3])) 

            elif item not in Split2[1] or item in Split2[2] :  ## item hasn't been found sending or receving SYN flag...
               print("SYN pack from wherever to wherever not completed...")