I have a beginners coding class for Python. The book we are using is "Coding for Penetration Testers Building Better Tools". During out second chapter we started to create Python scripts and I cannot seem to figure out what is wrong with this script I am supposed to retype from the book. See Below.
import httplib, sys
if len(sys.argv) < 3:
sys.exit("Usage " + sys.argv[0] + " <hostname> <port>\n")
host = sys.argv[1]
port = sys.argv[2]
client = httplib.HTTPConnection(host,port)
client.request("GET","/")
resp = client.getresponse()
client.close()
if resp.status == 200:
print host + " : OK"
sys.exit()
print host + " : DOWN! (" + resp.status + " , " + resp.reason + ")"
After running the code I get an error on line 20 (the last print line) stating:
selmer@ubuntu:~$ python /home/selmer/Desktop/scripts/arguments.py google.com 80
Traceback (most recent call last):
File "/home/selmer/Desktop/scripts/arguments.py", line 20, in <module>
print host + " : DOWN! (" + resp.status + " , " + resp.reason + ")"
TypeError: cannot concatenate 'str' and 'int' objects
All code is being run in Ubuntu 14.04 in a VM with Konsole and created in Gedit. Any help would be appreciated!
Replace the print line from:
print host + " : DOWN! (" + resp.status + " , " + resp.reason + ")"
with:
print '%s DOWN! (%d, %s)' % (host, resp.status, resp.reason)
The original line tries to append an int (resp.status
) to strings, as the error message says.