我有一个为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所有代码都在Ubuntu14.04中运行,在一个VM中使用Konsole,并在Gedit中创建。任何帮助都将不胜感激!
发布于 2015-02-07 22:01:12
从以下位置替换打印行:
print host + " : DOWN! (" + resp.status + " , " + resp.reason + ")"通过以下方式:
print '%s DOWN! (%d, %s)' % (host, resp.status, resp.reason)正如错误消息所述,原始行试图将int (resp.status)附加到字符串中。
https://stackoverflow.com/questions/28387874
复制相似问题