简体   繁体   English

如何获取函数的输出并将其用作另一个函数python的输入?

[英]How to take output of function and use as input of another function python?

More specifically my function grabs all the domains in a my database table and returns them. 更具体地说,我的函数获取数据库表中的所有域并返回它们。 I want to know how to input these domains into another function that will run the Kali Linux tool URLcrazy for each of the domains in that table. 我想知道如何将这些域输入到另一个函数中,该函数将对该表中的每个域运行Kali Linux工具URLcrazy。

For example my function that outputs these: 例如我的函数输出以下内容:

google.com google.com
yahoo.com yahoo.com
Here is the function: 这是函数:

def grab_domains():
    try:
        db = pymysql.connect(host="localhost",
                         user="root", passwd="root",
                         db="typosquat")
except ConnectionAbortedError as e:
    print(e, "\n")

temp = ""
cursor = db.cursor()
cursor.execute('SELECT name From domains')
for rows in cursor.fetchall():
    for entries in rows:
        temp += entries
        domains = entries[0:]
        print(domains)

return temp

Here is the output: 这是输出:

google.com google.com
yahoo.com yahoo.com

How do I write another function that will run the script URLcrazy on each of these domains? 如何编写另一个将在每个域上运行脚本URLcrazy的函数? Assuming all scripts are in same file location. 假设所有脚本都在同一文件位置。

This is all I have I cant figure out how to run it for each domain, only know how to for a single output. 这就是我无法弄清楚如何为每个域运行它的全部,只知道如何为单个输出运行。

def run_urlcrazy():
    np = os.system("urlcrazy " + grab_domains())
    print(np)
    return np

How to I get this function to run URLcrazy for each domain?^^ 我如何获得此功能以为每个域运行URLcrazy?^^

This is my first post ever on stack overflow let me know what I can do to improve it and help me with question if possible! 这是我有史以来第一篇关于堆栈溢出的文章,让我知道我可以做些什么来改进它,并在可能的情况下帮助我解决问题! Thanks 谢谢

You'll need a loop: 您将需要一个循环:

def run_urlcrazy():
    ret_vals = []
    for domain in grab_domains():
        np = os.system("urlcrazy " + domain)
        ret_vals.append(np)
    return ret_vals

I recommend a for loop because it can efficiently iterate over whatever your function returns. 我建议使用for循环,因为它可以有效地迭代函数返回的任何内容。


You'll need to make a minor modification to your grab_domains() function as well: 您还需要对grab_domains()函数进行较小的修改:

temp = []
cursor = db.cursor()
cursor.execute('SELECT name From domains')
for rows in cursor.fetchall():
    for entries in rows:
        domains = entries[0:]
        temp.extend(domains)

return temp

Now, your function returns a list of domains. 现在,您的函数将返回域列表。 You can iterate over this. 您可以对此进行迭代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用一个 function 的 output 作为 Z23EEEB4347BDD26BDDFC6B7EE 中另一个 function 的输入 - how to use output of one function as input of another function in python 如何将功能输出用作另一个功能的输入 - How to use a function output as a input for another function 使用函数输出作为另一个函数的输入 - Use function output as input to another function 使用一个功能的输出作为另一个功能的输入 - Use output of one function as input of another function 如何从文本中提取一行用作python函数的输入 - how to take a line from a text to use as input for a function python 如何获取此函数的输出并将其用作字典? - How to take output of this function and use it as a dictionary? 如何让一个function的output成为另一个function的输入 - How to get the output of a function to be the input of another function 如何使用函数的输出作为函数内另一个函数的输入 - How do I use the output of a function as the input for another function inside a function 蛇形在python函数中使用通配符输入/输出 - snakemake use wildcard input/output in python function 如何获取一个程序的输出并将其用作另一个程序的输入? - How do I take the output of one program and use it as the input of another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM