我正在編寫一個python腳本來讀取域列表,找出Mcafee的Siteadvisor服務提供的評級,然後將域和結果輸出到CSV。我該如何解決這個多線程的Python腳本?
我已經將我的腳本關閉了this previous answer。它使用urllib來抓取有問題域名的Siteadvisor頁面(不是最好的方法,我知道,但Siteadvisor沒有提供其他選擇)。不幸的是,它沒有生產任何東西 - 我一直得到這個錯誤:
Traceback (most recent call last):
File "multi.py", line 55, in <module>
main()
File "multi.py", line 44, in main
resolver_thread.start()
File "/usr/lib/python2.6/threading.py", line 474, in start
_start_new_thread(self.__bootstrap,())
thread.error: can't start new thread
這裏是我的腳本:
import threading
import urllib
class Resolver(threading.Thread):
def __init__(self, address, result_dict):
threading.Thread.__init__(self)
self.address = address
self.result_dict = result_dict
def run(self):
try:
content = urllib.urlopen("http://www.siteadvisor.com/sites/" + self.address).read(12000)
search1 = content.find("didn't find any significant problems.")
search2 = content.find('yellow')
search3 = content.find('web reputation analysis found potential security')
search4 = content.find("don't have the results yet.")
if search1 != -1:
result = "safe"
elif search2 != -1:
result = "caution"
elif search3 != -1:
result = "warning"
elif search4 != -1:
result = "unknown"
else:
result = ""
self.result_dict[self.address] = result
except:
pass
def main():
infile = open("domainslist", "r")
intext = infile.readlines()
threads = []
results = {}
for address in [address.strip() for address in intext if address.strip()]:
resolver_thread = Resolver(address, results)
threads.append(resolver_thread)
resolver_thread.start()
for thread in threads:
thread.join()
outfile = open('final.csv', 'w')
outfile.write("\n".join("%s,%s" % (address, ip) for address, ip in results.iteritems()))
outfile.close()
if __name__ == '__main__':
main()
任何幫助將不勝感激。
嘗試調試它。 – 2010-06-26 00:24:03
您創建了多少個線程? – 2010-06-26 00:24:54