Skip to content

Instantly share code, notes, and snippets.

@likewhoa
Forked from newbiethetest/ftp.py
Created January 7, 2014 05:43
Show Gist options
  • Save likewhoa/8295109 to your computer and use it in GitHub Desktop.
Save likewhoa/8295109 to your computer and use it in GitHub Desktop.
# -*- coding:utf8 -*-
__author__ = 'newbie0086#foxmail.com'
import Queue
import threading
from ftplib import FTP
import os,sys,string,datetime,time
import socket
class MyFtp:
def __init__(self,host,username,password,port=21):
self.host=host
self.username=username
self.password=password
self.port=port
self.ftp=FTP()
def __del__(self):
self.ftp.close()
def login(self):
ftp=self.ftp
timeout=30
socket.setdefaulttimeout(timeout)
ftp.set_pasv(True)
try:
ftp.connect(self.host,self.port)
ftp.login(self.username,self.password)
print "success %s\t%s\t%s"%(self.host,self.username,self.password)
ftp.close()
except Exception as e:
print self.host,e
queue = Queue.Queue()
class ThreadUrl(threading.Thread):
"""Threaded Url Grab"""
def __init__(self, queue):
#self._stopevent=threading.Event()
threading.Thread.__init__(self)
self.queue = queue
def run(self):
while True:
#grabs host from queue
Host,User,Pass = self.queue.get()
#grabs urls of hosts and look 200 is ok?
myftp=MyFtp(Host,User,Pass)
myftp.login()
self.queue.task_done()
# start = time.time()
def main_task(t=5):
#spawn a pool of threads, and pass them queue instance
for i in range(t):
t = ThreadUrl(queue)
t.setDaemon(True)
t.start()
#put the hosts to queue
o=open("../ftp.txt",'r')
data=o.readlines()
for line in data:
line=line.strip()
#print line.strip()
Host=line.split(":")[0]
User=line.split(":")[1]
Pass=line.split(":")[2]
queue.put((Host,User,Pass))
queue.join()
main_task()
#my ftp.txt contents:
www.njytouch1.com:njytouch:njytouch
www.google.com:test:test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment