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 ;-)