【问题标题】:Flask for loop issue烧瓶循环问题
【发布时间】:2019-06-19 13:38:44
【问题描述】:

这是我的 dir.py 烧瓶

import requests

def dir(domain):
    paths = ['/test','/html','/go','/.git']

    for path in paths:
        path = path.strip()
        url = 'http://'+domain+path

        result = requests.get(url)
        if result.status_code == 200:
            return url

dir("localhost")

这是run.py

@app.route('/brute', methods=["GET", "POST"])
def brute():
    erroro = ""
    if request.method == "POST":
        domain = None
        try:
            if request.form["domain"] == "":
                erroro += ("<p>{!r} Input Empty</p>").format(request.form["domain"])
            else:
                domain = request.form["domain"]
        except:
            erroro += ("<p>{!r} Input Empty</p>").format(request.form["domain"])

        if domain is not None:
            brute_res = brute_admin(domain)
        return render_template("brute_result.html", brute_res = brute_res) 
    return render_template('brute.html').format(erroro=erroro)

现在的问题是我正在尝试在网页中打印 url 列表,例如:

http://localhost/test

http://localhost/html

但它只是打印带有路径的第一个 url,我尝试了其他方法但没有运气。我现在可以做什么。此外,这个过程需要一些时间,并且结果在完成该过程之前不会显示,我能做到吗在后台执行此操作,稍后我可以下载在dir.py 中编码的 url 列表,如果 status_code == 200 然后返回 url。如果有任何其他方法可以执行此过程,请帮助我。

谢谢,

【问题讨论】:

  • return url - 那是你的问题。只要您return,循环就会结束。
  • 顺便说一句,重用像 dir 这样的内置名称是个坏主意
  • 如果我没有返回它,那么 print func 在控制台中工作。

标签: python python-3.x flask python-requests


【解决方案1】:

您在第一次收到 200 响应后返回 url

试试这样的:

urls = []
for path in paths:
    path = path.strip()
    url = 'http://'+domain+path
    result = requests.get(url)
    if result.status_code == 200:
         urls.append(url)
return urls

收集所有要返回的url,在for循环外返回列表

【讨论】:

  • 我已经使用了它但不能正常工作它只是在 html 渲染上显示 ['localhost/.git/'] 只有单个响应
猜你喜欢
  • 1970-01-01
  • 2021-10-27
  • 2012-07-23
  • 1970-01-01
  • 2012-11-18
  • 2021-10-15
  • 2020-05-25
  • 2018-06-15
相关资源
最近更新 更多