help with request

Asked

Viewed 36 times

-2

I am writing a subdomain scan and did the following code (I’m sorry for making you cry with such):

import requests
while True:
 url = raw_input("url: ")
 lista = ['blog', 'ftp', 'cpanel', 'intranet']
 for list in lista:
     url2 = url.replace("www", list)
     req = requests.get(url2)
     if req == True:
        print ("Ok " + url2)
     else:
        print ("Nop" + url2)

Could you tell me where the mistake is and how to fix it? Thank you

  • What exactly is the problem? Doesn’t it work? It’s a mistake? What happens if you run the program?

1 answer

0


Looking at your code, it seems to me you want something like this:

import requests
while True:
    url = input("url: ")
    lista = ['blog', 'ftp', 'cpanel', 'intranet']
    for list in lista:
        url2 = url.replace("www", list)
        try:
            req = requests.get('http://{}'.format(url2))
        except:
            print("Erro", url2)
            continue
        if req.status_code == requests.codes.ok:
            print("Ok", url2)
        else:
            print("NOK", url2)

Where the changes I made were:

  • Add "http://" at request
  • Add a structure "Try except" to capture connection errors (domain not found, for example)
  • Use the status_code in the successful request test, where, in coarse mode, failure does not mean domain not found, but rather something like page not found.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.