def generate_fs():
    """Receive and handle a request to generate a PCAP."""

    formobj = request.form

    # Generate flowsynth file
    flow_init_opts = ""

    # build src ip statement
    src_ip = str(formobj.get("l3_src_ip"))
    if src_ip == "$HOME_NET":
        src_ip = "192.168.%s.%s" % (random.randint(1, 254), random.randint(1, 254))
    else:
        src_ip = "172.16.%s.%s" % (random.randint(1, 254), random.randint(1, 254))

    # build dst ip statement
    dst_ip = str(formobj.get("l3_dst_ip"))
    if dst_ip == "$HOME_NET":
        dst_ip = "192.168.%s.%s" % (random.randint(1, 254), random.randint(1, 254))
    else:
        dst_ip = "172.16.%s.%s" % (random.randint(1, 254), random.randint(1, 254))

    # build src port statement
    src_port = str(formobj.get("l4_src_port"))
    if src_port.lower() == "any":
        src_port = random.randint(10000, 65000)

    # build dst port statement
    dst_port = str(formobj.get("l4_dst_port"))
    if dst_port.lower() == "any":
        dst_port = random.randint(10000, 65000)

    # initialize the tcp connection automatically, if requested.
    if "l3_flow_established" in formobj:
        flow_init_opts = " (tcp.initialize;)"

    # define the actual flow in the fs syntax
    synth = "flow default %s %s:%s > %s:%s%s;" % (
        str(formobj.get("l3_protocol")).lower(),
        src_ip,
        src_port,
        dst_ip,
        dst_port,
        flow_init_opts,
    )

    payload_fmt = str(formobj.get("payload_format"))
    payload_cmds = ""
    if payload_fmt == "raw":
        payload_cmds = payload_raw(formobj)
    elif payload_fmt == "http":
        payload_cmds = payload_http(formobj)
        if payload_cmds is None:
            return render_template(
                "pcapwg/error.html",
                error_text="Unable to process submitted HTTP Wizard content. See log for more details.",
            )
    elif payload_fmt == "cert":
        payload_cmds = payload_cert(formobj)
        if payload_cmds is None:
            return render_template(
                "pcapwg/error.html",
                error_text="Unable to process submitted certificate. See log for more details.",
            )

    synth = "%s\n%s" % (synth, payload_cmds)
    return render_template("pcapwg/compile.html", page="compile", flowsynth_code=synth)
Receive and handle a request to generate a PCAP.
app/flowsynth.py