Save in list format python results in html

0

I am trying to save the content of the variable x at the end of the code in us.html, but it only saves me the last line of the list, it has to do with something to save in list or save as a list format but I do not know how to do in this code.

def html_create(result):

    template = open("template.html","r")
    output = open("us.html","w")
    text = template.read().format(get_result = result)
    html = output.write(text)
    template.close()
    output.close()

imp_ports = open('scan_ports.txt','r')
regex = '[0-9]+/[\w.]+\s*[\w.]+\s*[\w.]+.[\w.]+'
list_port = []
for line2 in imp_ports:
    line2 = line2.rstrip()
    x = re.findall('[0-9]+/[\w.]+\s*[\w.]+\s*[\w.]+.[\w.]+', line2)
    if len(x) > 0 :
        if x[0] not in list_port:
            list_port.append(x[0])
            html_create(x[0])
            print (x[0])

This is the example of how it looks in the terminal and how it looks in the html:

    
asked by DaveNISC 28.05.2018 в 19:36
source

1 answer

0

Regardless of what you have been told in the comments (you have to call html_create() once just passing the list of results, instead of calling it once in each iteration of the loop), I think the problem is that you are unclear perhaps how to generate a "presentable" HTML from that list.

The creation of the "html" you are carrying out is too simple. As I see you just open a "template.html" (whose content does not show, but we can guess at the result), read it in a string, and apply .format() to the chain by passing the list result as a parameter.

Although you do not show the content of "template.html", since you do not use any sophisticated template system like jinja2 or similar, I understand that this template can not contain loops to process the list that you are going through, but rather the most will contain something the style:

<html><head></head>
<body>
  {get_result} <!-- Lugar donde irá el resultado -->
</body>
</html>

Python will just substitute the {get_result} for the list you pass, for which it will generate the string version of the list, which is something like:

['21/tcp open ftp', '22/tcp open ssh', '25/tcp open smtp', '26/tcp open rsftp', '80/tcp open http', '110/tcp open pop3', '113/tcp closed ident', '143/tcp open imap', '443/tcp open https', '465/tcp open smtps', '587/tcp open submission', '993/tcp open imaps', '995/tcp open pop3s', '8080/tcp open http-proxy', '8443/tcp open https-alt'] 

So the resulting HTML will be something like this:

<html><head></head>
<body>
  ['21/tcp open ftp', '22/tcp open ssh', '25/tcp open smtp', '26/tcp open rsftp', '80/tcp open http', '110/tcp open pop3', '113/tcp closed ident', '143/tcp open imap', '443/tcp open https', '465/tcp open smtps', '587/tcp open submission', '993/tcp open imaps', '995/tcp open pop3s', '8080/tcp open http-proxy', '8443/tcp open https-alt']
</body>
</html>

that would look more or less like samples in your screenshot.

Maybe you expected to generate a table where everything appeared organized by columns, similar to what you see in the terminal. Python will not do such a thing automatically unless you program it in some way.

For example, you could process the list you receive, to generate a string in which those elements appear separated by <br> , and pass that string to the template:

def html_create(result):
    template = open("template.html","r")
    output = open("us.html","w")
    text = template.read().format(get_result = "<br>\n".join(result))
    html = output.write(text)
    template.close()
    output.close()

Using this method, the resulting HTML would be like this (assuming the "template.html" from before):

<html><head></head>
<body>
21/tcp open ftp<br>
22/tcp open ssh<br>
25/tcp open smtp<br>
26/tcp open rsftp<br>
80/tcp open http<br>
110/tcp open pop3<br>
113/tcp closed ident<br>
143/tcp open imap<br>
443/tcp open https<br>
465/tcp open smtps<br>
587/tcp open submission<br>
993/tcp open imaps<br>
995/tcp open pop3s<br>
8080/tcp open http-proxy<br>
8443/tcp open https-alt
</body>
</html>

this HTML, even without being any wonder, will look better than the one that came out to you. At least it will show each chain in a line, very similar to what you see in the terminal.

Ideally, separate each of the chains that make up the lines into its three components, wrap each of them in <td></td> , and the full line in <tr></tr> , and the final set in <table></table> , to thus generate a tabulated output. But I leave that as an exercise ;-)

    
answered by 29.05.2018 в 20:27