繁体   English   中英

python中的readlines()错误

[英]readlines() error in python

我是Scapy和python的新手,并且已根据Violent Python:A Hacker's Cookbook的代码示例对以下代码进行了建模。 在wlan0接口上运行程序后,出现错误AttributeError:readlines

我的理解是readlines()将捕获转换为列表,然后findall()可以在字符串行上进行操作。

有人可以帮忙吗? 干杯。

   #!/usr/bin/python

import optparse
import logging
import re       # provides support for regular expressions
logging.getLogger("scapy.runtime").setLevel(logging.ERROR) # suppresses messages which have a lower level of seriousness than error messages

from scapy.all import *

def findguest(capture):
        for line in capture.splitlines():
                IPaddr = re.findall("(?:\d{1,3}\.){3}\d{1,3}",line) # findall returns a LIST
                                                # must use ,raw

                if IPaddr:
                        print 'found IP addr '+IPaddr[0]

def main():
        parser = optparse.OptionParser() # create parser object

        parser.add_option('-i', dest='interface', type='string', help='specify interface to listen on')
        (opts, args) = parser.parse_args()

# the below if-else clause is just some error handling.. if no interface is given after -i it will print the usage menu again
# conf.iface is the standard interface which will be used by the program, so we set it to whatever interface the user gives us

        if opts.interface == None:
                print parser.usage
                exit(0)
        else:
                conf.iface = opts.interface

        try:
                sniff(filter='tcp', prn=findguest, store=0)

        except KeyboardInterrupt:
                exit(0)

if __name__ == "__main__":
        main()

具有以下回溯:

    lanix@lanix ~/python/wirelessmayhem $ sudo ./hotelguest2.py -i wlan0
['__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__div__', '__doc__', '__eq__', '__format__', '__getattr__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__len__', '__lt__', '__metaclass__', '__module__', '__mul__', '__ne__', '__new__', '__nonzero__', '__rdiv__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_do_summary', 'add_payload', 'add_underlayer', 'aliastypes', 'answers', 'build', 'build_done', 'build_padding', 'build_ps', 'canvas_dump', 'clone_with', 'command', 'copy', 'decode_payload_as', 'default_fields', 'default_payload_class', 'delfieldval', 'dispatch_hook', 'display', 'dissect', 'dissection_done', 'do_build', 'do_build_payload', 'do_build_ps', 'do_dissect', 'do_dissect_payload', 'do_init_fields', 'explicit', 'extract_padding', 'fields', 'fields_desc', 'fieldtype', 'firstlayer', 'fragment', 'from_hexcap', 'get_field', 'getfield_and_val', 'getfieldval', 'getlayer', 'guess_payload_class', 'hashret', 'haslayer', 'hide_defaults', 'init_fields', 'initialized', 'lastlayer', 'libnet', 'lower_bonds', 'mysummary', 'name', 'overload_fields', 'overloaded_fields', 'packetfields', 'payload', 'payload_guess', 'pdfdump', 'post_build', 'post_dissect', 'post_dissection', 'post_transforms', 'pre_dissect', 'psdump', 'remove_payload', 'remove_underlayer', 'route', 'self_build', 'sent_time', 'setfieldval', 'show', 'show2', 'show_indent', 'sprintf', 'summary', 'time', 'underlayer', 'upper_bonds']
Traceback (most recent call last):
  File "./hotelguest2.py", line 41, in <module>
    main()
  File "./hotelguest2.py", line 35, in main
    sniff(filter='tcp', prn=findguest, store=0)
  File "/usr/lib/python2.7/dist-packages/scapy/sendrecv.py", line 586, in sniff
    r = prn(p)
  File "./hotelguest2.py", line 12, in findguest
    for line in capture.splitlines():
  File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 176, in __getattr__
    fld,v = self.getfield_and_val(attr)
  File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 172, in getfield_and_val
    return self.payload.getfield_and_val(attr)
  File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 172, in getfield_and_val
    return self.payload.getfield_and_val(attr)
  File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 172, in getfield_and_val
    return self.payload.getfield_and_val(attr)
  File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 1057, in getfield_and_val
    raise AttributeError(attr)
AttributeError: splitlines

不使用splitlines()进行追溯

Traceback (most recent call last):
  File "./hotelguest2.py", line 38, in <module>
    main()
  File "./hotelguest2.py", line 32, in main
    sniff(filter='tcp', prn=findguest, store=0)
  File "/usr/lib/python2.7/dist-packages/scapy/sendrecv.py", line 586, in sniff
    r = prn(p)
  File "./hotelguest2.py", line 11, in findguest
    IPaddr = re.findall("(?:\d{1,3}\.){3}\d{1,3}",capture) # findall returns a LIST
  File "/usr/lib/python2.7/re.py", line 177, in findall
    return _compile(pattern, flags).findall(string)
TypeError: expected string or buffer

readlines() does not convert capture into a list where capture is a string

它适用于文件对象。改为使用capture.split("\\n")capture.splitlines()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM