【问题标题】:Argparse 'argument 1 must be str, bytes or bytearray, not None'argparse '参数 1 必须是 str、bytes 或 bytearray,而不是 None'
【发布时间】:2021-09-09 21:55:32
【问题描述】:
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
import sys
from scapy.all import *
import argparse
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("--remote_host")
parser.add_argument("--verbose")
parser.add_argument("--help", action='store_true')
args = parser.parse_args()


if args.help:
    print("HELP...")

# Define end host and TCP port range
hostInput    = args.remote_host
host  = socket.gethostbyname(hostInput)
port_range = [21,22,23,25,53,80,110,135,137,138,139,443,1433,1434,8080]

# Send SYN with random Src Port for each Dst port
for dst_port in port_range:
    src_port = random.randint(1025,65534)
    resp = sr1(
        IP(dst=host)/TCP(sport=src_port,dport=dst_port,flags="S"),timeout=1,
        verbose=int(args.verbose),
    )

    if resp is None:
        print(f"{host}:{dst_port} is filtered (silently dropped).")

    elif(resp.haslayer(TCP)):
        if(resp.getlayer(TCP).flags == 0x12):
            # Send a gratuitous RST to close the connection
            send_rst = sr(
                IP(dst=host)/TCP(sport=src_port,dport=dst_port,flags='R'),
                timeout=1,
                verbose=int(args.verbose),
            )
            print(f"{host}:{dst_port} is open.")

        elif (resp.getlayer(TCP).flags == 0x14):
            print(f"{host}:{dst_port} is closed.")

    elif(resp.haslayer(ICMP)):
        if(
            int(resp.getlayer(ICMP).type) == 3 and
            int(resp.getlayer(ICMP).code) in [1,2,3,9,10,13]
        ):
            print(f"{host}:{dst_port} is filtered (silently dropped).")

使用--remote_host 127.0.0.1 --verbose 1 运行正常,没有错误

使用--help 运行会产生:

TypeError: gethostbyname() argument 1 must be str, bytes or bytearray, not None

注意:当然只有当我包含print("Help...")下面的所有行时才会出现上述错误

这是完整的回溯:

Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
= RESTART: C:\Users\00082563\AppData\Local\Programs\Python\AT2_PythonNetworkApplications.v1.1a\Scenario 1\logger.py
HELP...
Traceback (most recent call last):
  File "C:\Users\00082563\AppData\Local\Programs\Python\AT2_PythonNetworkApplications.v1.1a\Scenario 1\logger.py", line 48, in <module>
    host  = socket.gethostbyname(hostInput)
TypeError: gethostbyname() argument 1 must be str, bytes or bytearray, not None
>>>

【问题讨论】:

  • 这能回答你的问题吗? Argparse expected one argument
  • 如果你会搜索错误消息,而不是在 SO 上发布并等待我这样做,这会更快。
  • 然后研究那个错误。重复直到代码工作。这就是编程。
  • 以及不再重现问题的示例,请注意标题也已过时。
  • 这不是argparse 错误!如果您需要帮助,请显示完整的回溯。我的猜测是scapy 正在使用args.remote_host 作为gethostbyname 命令发出。但是该解析器参数的默认值是None。如果你想在不运行其余代码的情况下显示help,你可以使用exit(或者只使用默认的argparse帮助机制)。

标签: python-3.x argparse


【解决方案1】:

按照上面cmets的建议,我需要在之后退出程序:

if args.help:
    print("HELP...")

因此,在下面的脚本中,用户可以在 CLI 中添加 --remote_host google.com --verbose 0(或 verbose 1 以获得完整的详细信息...)

用户可以改为在 CLI 中使用 --help 来打印自定义帮助部分,之后程序将退出。

#!/usr/bin/env python


import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
import sys
from scapy.all import *
import argparse
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("--remote_host")
parser.add_argument("--verbose")
parser.add_argument("--help", action='store_true')
args = parser.parse_args()


if args.help:
    print("Help section...")
    exit()
 
# Define end host and TCP port range
hostInput    = args.remote_host
host  = socket.gethostbyname(hostInput)
port_range = [21,22,23,25,53,80,110,135,137,138,139,443,1433,1434,8080]

# Send SYN with random Src Port for each Dst port
for dst_port in port_range:
    src_port = random.randint(1025,65534)
    resp = sr1(
        IP(dst=host)/TCP(sport=src_port,dport=dst_port,flags="S"),timeout=1,
        verbose=int(args.verbose),
    )

    if resp is None:
        print(f"{host}:{dst_port} is filtered (silently dropped).")

    elif(resp.haslayer(TCP)):
        if(resp.getlayer(TCP).flags == 0x12):
            # Send a gratuitous RST to close the connection
            send_rst = sr(
                IP(dst=host)/TCP(sport=src_port,dport=dst_port,flags='R'),
                timeout=1,
                verbose=int(args.verbose),
            )
            print(f"{host}:{dst_port} is open.")

        elif (resp.getlayer(TCP).flags == 0x14):
            print(f"{host}:{dst_port} is closed.")

    elif(resp.haslayer(ICMP)):
        if(
            int(resp.getlayer(ICMP).type) == 3 and
            int(resp.getlayer(ICMP).code) in [1,2,3,9,10,13]
        ):
            print(f"{host}:{dst_port} is filtered (silently dropped).")

【讨论】:

    猜你喜欢
    • 2017-07-10
    • 2019-08-27
    • 2020-07-11
    • 1970-01-01
    • 2021-06-17
    • 2018-03-27
    • 2022-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多