I am running a Python based CGI script and am having difficulties printing the output of "dig" command in a browser. I run the same script using Idle and it works.
Here's the script:
#!/usr/bin/python3.5
#================================================================
# Modules
import cgi
import subprocess
import re
#================================================================
# Create HTML page
form = cgi.FieldStorage()
regex_url = re.compile(r'(^https?:\/\/)(.*?)(\/.*)')
url = form.getvalue('url')
origin = form.getvalue('origin')
print ("Content-type:text/html\r\n\r\n")
print ("<html>")
print ("<head>")
print ("<title>Lookup Tool</title>")
print ("<body>")
print ("<h1>DNS lookup tool</h1><br>")
print("<form method='post' action='/cgi-bin/test.py'>")
print("<p>Full URL including http/s: <input type='url' name='url'/></p>")
print("<p>Leave empty: <input type='text' name='origin'/></p>")
print("<input type='submit' value='Submit' />")
print("</form>")
host = regex_url.search(url)
hostname = host.group(2)
print("Hostname is: " + hostname )
resolver = subprocess.Popen(['dig', hostname], stdout=subprocess.PIPE)
print(resolver.communication())
I would expect to see the output of dig www.google.com in a web browser but this is not the case. However, if I do "print(hostname)" then www.google.com would be printed in a web page.
What am I doing wrong
dig
is a command-line utility. I don't understand by what you mean it would be printed in a web page - Do you want to use your python script to open a web browser? In this case you would have to callsubprocess.Popen
on your browser (chrome or firefox or .w.e) – vasia Sep 14 '18 at 15:46.communicate
returns a tuple, you need to extract the stdout data from it. OTOH, I guess you should still see something. Also, it's a Good Idea to send properly structured HTML: give it adoctype
, close yourhead
andbody
tags, etc. – PM 2Ring Sep 14 '18 at 16:13