TypeError:无法转换' NoneType'对象在运行python脚本时隐式出现错误

时间:2016-08-09 15:42:22

标签: python

尝试运行python脚本进行网站扫描时出现错误TypeError:无法转换' NoneType' object to str隐含地保持python代码的发生是

main.py

from general import *
from domain_name import *
from ip_address import *
from robots_txt import *
from whois import *
from nmap import *


ROOT_DIR = 'Targets'
create_dir(ROOT_DIR)

def gather_info(name, url):
    domain_name = get_domain_name(url)
    ip_address = get_ip_address(url)
    robots_txt = get_robots_txt(url)
    whois = get_whois(domain_name)
    nmap = get_nmap(input('Nmap Options:'), ip_address)
    create_report(name, url, domain_name, robots_txt, whois, nmap)

def create_report(name, full_url, domain_name, robots_txt, whois, nmap):
    project_dir = ROOT_DIR + '/' + name
    create_dir(project_dir)
    write_file(project_dir + '/full_url.txt', full_url)
    write_file(project_dir + '/domain_name.txt', domain_name)
    write_file(project_dir + '/robots_txt.txt', robots_txt)
    write_file(project_dir + '/whois.txt', whois)
    write_file(project_dir + '/nmap.txt', nmap)



gather_info(input('Target Name:'), input('Target Domain:'))

nmap.py

import os 

def get_nmap(options, ip):
    command = "nmap " + options + " " + ip
    process = os.popen(command)
    results = str(process.read())
    return results

当我运行它时,它会出现

python ./main.py 
Target Name:test
Target Domain:https://www.google.ie
Nmap Options:-F
Traceback (most recent call last):
  File "./main.py", line 31, in <module>
    gather_info(input('Target Name:'), input('Target Domain:'))
  File "./main.py", line 17, in gather_info
    nmap = get_nmap(input('Nmap Options:'), ip_address)
  File "/Projects/WebScanner/nmap.py", line 9, in get_nmap
    command = "nmap " + options + " " + ip
TypeError: Can't convert 'NoneType' object to str implicitly

我已经搜索了其他类似的错误,但找不到解决方法来解决它,任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

Python告诉你,它不能使用NoneType类型的对象并将其转换为串联字符串。您的代码中的某些位置会将options变量或ip变量分配给None。与上面说的Lost一样,打印出ipoptions并查看它们是否打印出None。或者您可以说command = "nmap " + str(options) + " " + str(ip),看看您的ip变量或options变量是None