How to format a string in a fixed position?

1

My program tries to search links on web pages And when the page is valid the application writes [+] Found [+] and if the link is not valid write [+] Error [+] but each link does not have the same length . Here is an example of how the application looks when I run it foobar.com/test [+] Found [+] foobar.com/test21 [+] Error [+] foobar.com/test333 [+] Error [+]

As you can see the position of the message moves because the link is longer. I want the Found or Error message to come out in the same vertical row like this:

foobar.com/test [+] Found [+] foobar.com/test21 [+] Error [+] foobar.com/test333 [+] Error [+]

But not so close to the link because there may be an error that would be if a link is longer than the length between the link and the message Error or Found Here I have my code

for i in array:
    try:
        adminpanel = urllib2.urlopen(website+i)
        checkurl = adminpanel.code
        if checkurl == 200:
            print Fore.GREEN+ website+i," ""                                              " +"[+] Found [+] "
            continue
    except urllib2.URLError, checkurl:
        if checkurl == 404:
            print website+i," ""              Not Found :/"
        else:
            print Fore.RED+ website+i," ""                                              " +"[+] Error [+] " + Style.RESET_ALL
    
asked by Katz 26.07.2016 в 06:33
source

1 answer

2

If you know all the links in advance, which seems to be the case, you can do the following:

# Imaginando que los enlaces sin el prefijo están en 'array'
array = ['test', 
         'longer', 
         'longest'] # este es el más largo

website = 'footest.com/'

length = max(map(len, enlaces)) + len(website)

for i in array:
    try:
        adminpanel = urllib2.urlopen(website+i)
        checkurl = adminpanel.code
        lenght_url = len(website + i)
        if checkurl == 200:
            print(Fore.GREEN + website+i, 
                  " " * (lenght - length_url) + "  [+] Found [+]")
            continue
    except urllib2.URLError, checkurl:
        lenght_url = len(website + i)
        if checkurl == 404:
            print(website + i," ""              Not Found :/")
        else:
            print(Fore.RED + website + i,
                  " " * (lenght - length_url) + "  [+] Error [+] " + style.RESET_ALL)  

The previous code should leave you two spaces at the end of the longest link and more spaces in the rest of the links.

In the part of your code I add the number of spaces required in each print making use of the maximum length of a link and the length of the link in use to be able to enter the number of spaces needed.

[I have not tried the code since I do not have a python2.7 installed on this PC]

    
answered by 26.07.2016 / 11:56
source