首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从python程序中获取缩进输出

无法从python程序中获取缩进输出
EN

Stack Overflow用户
提问于 2017-12-07 02:03:29
回答 1查看 30关注 0票数 0

我正在编写一个脚本来捕获给定接口的流量。但在执行程序时,我得到的消息是:

代码语言:javascript
复制
"Process finished with exit code 0"

请注意下面我的代码:

代码语言:javascript
复制
import socket
from struct import *
import datetime
import pcapy
import sys


def main(argv):
    devices = pcapy.findalldevs()
    print (devices)

    for d in devices:
        print "Available devices are ", d

    #Device to be sniffed

    input_dev = raw_input("Enter deice name to sniff")

    print "Following device would be printed", input_dev

    #open
    #device
    # Arguments here are:
    #   device
    #   snaplen (maximum number of bytes to capture _per_packet_)
    #   promiscious mode (1 for true)
    #   timeout (in milliseconds)


    cap = pcapy.open_live(input_dev,65536,1,0)

    #Interating all packets captured in to a variable
    while(1):
        (header, packet) = cap.next()
        parse_packet(packet)

        #Convert a string of 6 characters of ethernet address into a dash separated hex string
    def  eth_addr(a):
        b = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (ord(a[0]), ord(a[1]), ord(a[2]), ord(a[3]), ord(a[4]), ord(a[5]))


    # Parsing captured packets

    def parse_packet(packet):

        eth_length = 14

        # Get the protocol used from the captured packet
        eth_header = packet[:eth_length]
        eth = unpack('!6s6sH', eth_header)
        eth_protocol = socket.ntohs(eth[2])
        print 'Destination MAC : ' + eth_addr(packet[0:6])+'Source MAC: ' + eth_addr(packet[0:12]) + 'Protocol: ' +str(eth_protocol)

        # Parse the IP packet
        #
        if eth_protocol == 8:
         #Parse IP header
        #take first 20 characters for the ip header
           ip_header = packet[eth_length:20+eth_length]

        #Now unpack them
        iph = unpack('!BBHHHBBH4s4s', ip_header)
        version_ihl = iph[0]
        version = version_ihl >> 4
        ihl = version_ihl & oxf # need to know what is the oxf
        iph_length = ihl * 4

        ttl = iph[5]
        print "ttl value is = ", str(ttl)

        protocol =  ip[6]
        print "protocol = ", str(protocol)

        s_addr = socket.inet_ntoa(iph[8]);
        print "source address = ", str(s_addr)

        d_address = socket.inet_ntoa(iph[9])
        print "Destination address = ", str(d_address)


        #Parsing TCP header

        if protocol == 6:

            #if protocol = 6, it would be TCP
            t = iph_length + eth_length
            tcp_heder = packet

            #Unpack the packet
            tcph = unpack('!HHLLBBHHH', tcp_heder)

            source_port = tcph[0]
            print "Source port =", source_port

            dst_port = tcph[1]
            print "Dst port =", dst_port

            ack_no = tcph[3]
            print "ack_No = ", ack_no

            doff_deserverved = tcph[4]
            print "doff_deserved = ", doff_deserverved
            tcp_length = doff_deserverved >> 4

            h_size = eth_length + iph_length + tcp_length * 4
            data_size = len(packet) - h_size

            #get the data from the packet
            data = packet[data_size:]
            print "Data is = ", data

        elif protocol == 1:
            u = iph_length + eth_length
            icmp_length = 4
            icmp_header= packet[u:u+4]

            #Now unpack them
            icmp = unpack('!BBH' , icmp_header)

            icmp_type = icmp[0]
            print "Type is = ", icmp

            code = icmp[1]
            print "Code is = ", code

            checkSum = icmp[3]
            print "Check sum is ", checkSum

            h_size = eth_length + iph_length + icmp_length
            data_sze = len(packet)- h_size
            print "Data size = ", data_size

            #Get Data from the packet
            data2 = packet[h_size:]
            print "Data is = ", data2


         #Parsing UDP packet
        elif protocol == 17:
            u = iph_length + eth_length
            udph_leungth = 8
            udp_header = packet[u:u+8]

            #Now unpack them
            udp_packet = unpack('!HHHH', udp_header)

            s_port = udp_packet[0]
            print "Source Port = ", s_port

            d_port = udp_packet[1]
            print "Udp Packet = ", d_port


            lenth_of_packet = udp_packet[2]
            print "Length of packet = ", lenth_of_packet

            check_sum = udp_packet[3]
            print "check sum is ", check_sum

            h_size_of_packet = eth_length + iph_length + udph_leungth

            actaul_size = len(packet) - h_size_of_packet

            #Retrieving data packet from the size

            data = packet[h_size_of_packet]

            print "Actual Data = ", data

            #IF there any other some protocol
        else:
            print "Protol other than TCP/UDP/ICMP"



    main(sys.argv)

请让我知道什么是不产生缩进输出或任何其他方式获得相同的原因。

EN

回答 1

Stack Overflow用户

发布于 2017-12-07 02:56:10

您的代码中存在多个缩进问题。我建议你重温一下你的代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47680840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档