从教科书运行参数脚本时出错



我有一个Python的初学者编码类。我们正在使用的书是"渗透测试人员构建更好工具的编码"。在第二章中,我们开始创建Python脚本,我似乎无法弄清楚我应该从书中重新键入的这个脚本有什么问题。请参见下文。

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 + ")"

运行代码后,我在第20行(最后一行打印)出现错误,说明:

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

所有代码都在Ubuntu 14.04中运行,并使用Konsole在虚拟机中创建。任何帮助都将不胜感激!

替换中的打印行

print host + " : DOWN! (" + resp.status + " , " + resp.reason + ")"

带有:

print '%s DOWN! (%d, %s)' % (host, resp.status, resp.reason)

原始行试图将int(resp.status)附加到字符串中,如错误消息所示。

最新更新