python中的readlines()错误

2024-10-02 20:32:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我是scapy和python的新手,并在暴力python的一个代码示例上模拟了下面的代码:一个黑客的食谱。在我的wlan0接口上运行程序后,我得到错误AttributeError:readlines

我的理解是readlines()将capture转换成一个列表,然后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()

通过以下回溯:

^{pr2}$

不使用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

Tags: theinpyimportreparserifmain