Forums

Can't connect to API and JSON Pretty the file

So I've been learning python for a little bit now and I thought I would test out some cool ideas I had for an API search.

To explain what I want to do with this idea, I'm:

  • Trying to connect to the API and search for something. e.g. iPhone
  • I'm then grabbing all of the data and trying to Pretty it so that my search function can do a link by link search.
  • Then I'm doing a string search line by line and if that string is in that line I find what's between the two different strings. e.g "iPhone" and ":"
  • I'm then printing the lines to my websites page.

I've been trying to get my head around this but it's a lot harder then it seams. If someone knows what I'm doing wrong or knows of a different way for me to do this I would be more the grateful for your help.

Python code:

@app.route("/api", methods=['GET','POST'])
def APIlookup(myfile, sort=True, indents=4):
    global myfile
    if request.method == "POST":
        link = ""https://api.shodan.io/shodan/host/search?..."
        f = urllib.urlopen(link)
        myfile = f.read()
        text_file = open(os.path.join(app.root_path, "data/Write_it.txt"), "w")
        text_file.write((json.dumps(json.loads(myfile), sort_keys=sort, indent=indents)))
        test = (json.dumps(json.loads(myfile), sort_keys=sort, indent=indents))
        text_file.close()
        with open(os.path.join(app.root_path, "data/Write_it.txt"), "r") as input_file:
            for line in input_file:
                if '"str":' in line:
                    version2 = line
                    s = line
                    start = '"str":"'
                    end = '"'

                    result = re.search('%s(.*)%s' % (start, end), s).group(1)
                    socket.setdefaulttimeout( 2 )  # timeout in seconds

                    url = 'http://'+ result
                else:
                    fail = "Couldn't find any URLs\n"
                    return render_template("api.html", pressedSubmit="True", fail=fail)
            else:    
                return render_template("api.html", pressedSubmit="True", url=url)
    else:
        return render_template("api.html")

I started this Topic because I'm completely lost at this point and I thought this might be helpful for someone else that's currently or in the future trying to do something like this.

what are the errors that you are seeing?