1

我正在制作一个简单的脚本,可以找到网站的管理面板。
我可以使用命令行界面来做到这一点,但我也想使用 easygui 制作一个简单的 gui。
我有几个小时的时间,但我在第 33 行收到此错误:
'TypeError: writelines requires an iterable argument'

这是我的代码:

import os, httplib
import easygui as eg

admpagelist = ['admin/','administrator/','admin1/','admin2/']

title = 'Test Easygui'
msg='Enter Your Target: (without http://) '
value = []
value = eg.enterbox(msg,title)
value = value.replace('Enter Your Target: (without http:// )','')

collectornf = []
collectorf = []

try:
    con = httplib.HTTPConnection(value)
    con.connect()
except:
    eg.msgbox('Host is offline or invalid url! ')

for adm in admpagelist:
    adm = '/%s' %adm
    host = value + adm
    con = httplib.HTTPConnection(value)
    con.connect()
    request = con.request('GET',adm)
    response = con.getresponse()
    if response.status == 200:
        collectorf = collectorf.append(str(host))
        found = open('C:/Users/Andi/Desktop/found.txt','w')
        found.writelines(collectorf)
        found.close()
    else:
        collectornf = collectornf.append(str(host))
        notfound = open('C:/Users/Andi/Desktop/notfound.txt','w')
        notfound.writelines(collectornf)
        notfound.close()
4

2 回答 2

4

你的问题是你这样做collectorf = collectorf.append(str(host))

list.append()就地修改列表,并返回None. 然后,您将分配该None值并覆盖列表。

只需这样做collectorf.append(str(host))

if response.status == 200:
    filename = "found.txt"
    data = collectorf
else:
    filename = "notfound.txt"
    data = collectornf
data.append(str(host))
with open('C:/Users/Andi/Desktop/' + filename,'w') as found:
    found.writelines(data)

(重构以避免复制/粘贴编码并使用我作为评论发布的一些建议)。

请注意,在循环之后写入数据会更有意义,而不是在循环期间重复写入。

于 2013-06-01T16:30:09.913 回答
0

我会保持文件打开。不断地打开和关闭它们对我来说似乎是很多开销。

with open('C:/Users/Andi/Desktop/found.txt','w') as found, open('C:/Users/Andi/Desktop/notfound.txt','w') as notfound: # before 2.7, write this as 2 with statements
    for adm in admpagelist:
        adm = '/%s' % adm
        host = value + adm
        con = httplib.HTTPConnection(value)
        con.connect()
        request = con.request('GET', adm)
        response = con.getresponse()
        if response.status == 200:
            targetlist = collectorf
            targetfile = found
        else:
            targetlist = collectornf
            targetfile = notfound
        targetlist.append(str(host))
        targetfile.write(str(host) + '\n')
        # if you are paranoid, you can as well do:
        # targetfile.flush()

with东西负责之后再次关闭文件。

如果您之后不需要这些列表,您可以省略它们,因为我们会在每次循环运行时写入文件。

于 2013-06-01T18:44:21.037 回答