나는 ip리스트에서 모든 URL을 추출해야하는데, 이 파이썬 스크립트를 작성했지만 같은 IP를 여러 번 추출하는 문제가있다. (더 많은 쓰레드가 같은 IP로 생성된다.) 멀티 스레드를 사용하여 내 솔루션을 향상시킬 수 있습니까? 내 영어 감사합니다 죄송합니다파이썬 멀티 쓰레드
모든
import urllib2, os, re, sys, os, time, httplib, thread, argparse, random
try:
ListaIP = open(sys.argv[1], "r").readlines()
except(IOError):
print "Error: Check your IP list path\n"
sys.exit(1)
def getIP():
if len(ListaIP) != 0:
value = random.sample(ListaIP, 1)
ListaIP.remove(value[0])
return value
else:
print "\nListaIPs sa terminat\n"
sys.exit(1)
def extractURL(ip):
print ip + '\n'
page = urllib2.urlopen('http://sameip.org/ip/' + ip)
html = page.read()
links = re.findall(r'href=[\'"]?([^\'" >]+)', html)
outfile = open('2.log', 'a')
outfile.write("\n".join(links))
outfile.close()
def start():
while True:
if len(ListaIP) != 0:
test = getIP()
IP = ''.join(test).replace('\n', '')
extractURL(IP)
else:
break
for x in range(0, 10):
thread.start_new_thread(start,())
while 1:
pass
'os'를 한 번 가져 오면 잘됩니다. 두 번 가져올 필요가 없습니다. –