TypeError:generate_input()缺少1个必需的位置参数:“ n”

时间:2019-06-28 18:22:06

标签: python python-3.x

我想制作基于Python的模糊器。所以我正在运行一个简单的示例,但出现错误。

我正在下面的链接上执行代码。 https://jmcph4.github.io/2018/01/19/writing-a-simple-fuzzer-in-python/

我认为,这不是重新声明班级的问题。当前在stackoverflow上发布的问题没有帮助。请帮助我。

这是代码:

import sys
from mph.program import Program
from fuzzbang.alphanumericfuzzer import AlphaNumericFuzzer

def generate_input(n):
    """
    Returns an alphanumeric string with a length no greater than n.
    """
    fuzzer = AlphaNumericFuzzer(0, n)

    return fuzzer.generate()

if __name__ == '__main__':
    # usage
    if len(sys.argv) != 3:
        print("usage: python3 fuzztut.py num_cases max_length")
        exit(1)

    # command-line arguments    
    num_cases = int(sys.argv[1]) # number of test cases to run
    max_length = int(sys.argv[2]) # maximum length of each string

    results = [] # list for storing the result of each test

    # main loop
    for i in range(num_cases):
        input = generate_input() # generate input string
        return_value = run(input) # run name with our input

        # save test results to our global results list
        test_result = {}
        test_result["num"] = i
        test_result["input"] = input
        test_result["output"] = return_value
        results.append(test_result)

    # print summary
    for test in results:
        print("Case #{:d}:".format(test["num"]))
        print("    IN: " + test["input"])
        print("    OUT: {:4d}".format(test["output"]))
        print("\n")

类(包括另一个文件)

import sys
from random import randint, sample
from .fuzzer import Fuzzer

class AlphaNumericFuzzer(Fuzzer):
    """
    A fuzzer that produces unstructured alphanumeric output
    """
    def __init__(self, min_length, max_length):
        super().__init__()
        self._min_length = min_length
        self._max_length = max_length

        self._alphabet = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")

    @property
    def min_length(self):
        return self._min_length

    @property
    def max_length(self):
        return self._max_length

    def generate(self):
        data = []

        start = self.min_length
        end = 0

        if self.max_length is not None:
            end = randint(start, self.max_length)
        else:
            end = randint(start, sys.maxsize)

        for i in range(start, end):
            data.append(sample(self._alphabet, 1)[0])

        self._cases.append("".join(data))

        return "".join(data)

结果:

$ python3 fuzztut.py 10 32
Traceback (most recent call last):
  File "fuzztut.py", line 40, in <module>
    input = generate_input() # generate input string
TypeError: generate_input() missing 1 required positional argument: 'n'

1 个答案:

答案 0 :(得分:0)

在代码中,各行

# main loop
for i in range(num_cases):
    input = generate_input() # generate input string
    ...

根据对函数generate_input()的定义,您需要提供一个参数/参数n

def generate_input(n):
    """
    Returns an alphanumeric string with a length no greater than n.
    """
    ...

所以您的代码必须看起来像这样,为函数调用提供数字或变量:

input = generate_input(10)