import urllib2
import cookielib
import os
import time
import ClientForm
from BeautifulSoup import BeautifulSoup
import urlparse
import random
import shutil
from Tkinter import *
import ImageTk
import win32clipboard, win32con
import bz2
import wx
import sys
import inspect
import ctypes
from ConfigParser import SafeConfigParser as Config
from wx.lib.mixins.listctrl import ColumnSorterMixin
import threading
class DumbProxyPasswordMgr:
def __init__(self):
self.user = self.passwd = None
def add_password(self, realm, uri, user, passwd):
self.user = user
self.passwd = passwd
def find_user_password(self, realm, authuri):
return self.user, self.passwd
class Authoriz:
def GetClipboardText(self):
win32clipboard.OpenClipboard(0)
clip_data = win32clipboard.GetClipboardData(win32con.CF_TEXT)
clip_text = str(clip_data)
win32clipboard.CloseClipboard()
return clip_text
def buttonPress(self,entry_string):
self.capatcha_value=entry_string
self.window.destroy()
def login(self,username,password,cookieFile,loginURL,capatcha=False,capatchaURL=None,
capatchaForm=None,capatcha_name=None,capatchaType='FineReader',
serviceINI=None,proxy_addres=None,proxy_user=None,proxy_pass=None,loginForm="username",passwordForm="password"):
cj = cookielib.LWPCookieJar()
if os.path.isfile(cookieFile):
cj.load(cookieFile)
cookie_handler=urllib2.HTTPCookieProcessor(cj)
if loginURL[:7]!='http://':
loginURL='http://'+loginURL
if proxy_addres!=None:
proxy= urllib2.ProxyHandler({"http" : "http://"+proxy_addres})
proxy_auth_handler = urllib2.ProxyBasicAuthHandler(DumbProxyPasswordMgr ())
proxy_auth_handler.add_password(None, None, proxy_user, proxy_pass)
opener = urllib2.build_opener(proxy,proxy_auth_handler,cookie_handler)
else:
proxy= urllib2.ProxyHandler()
opener = urllib2.build_opener(cookie_handler,proxy)
urllib2.install_opener(opener)
headers = { 'User-Agent' : "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
'Accept-Language' : 'en-us',
'Accept' : 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*',
'Connection' : 'Keep-Alive' }
data=None
prov=urllib2.urlopen(urllib2.Request('http://'+urlparse.urlsplit(loginURL)[1],
data,headers))
if prov.read().find(username)==-1: #если еще не залогинено - то надо залогиниться
req1=urllib2.Request(loginURL,data,headers)
forms = ClientForm.ParseResponse(urllib2.urlopen(req1))
form = forms[0]
form[loginForm] = username
form[passwordForm] = password
if capatcha==True and capatchaType=='FineReader': # до этой строки все оч просто и понятно,
#а после нее кусок кода не используеться
req=urllib2.Request(capatchaURL,data,headers)
svf = open(capatcha_name,'wb')
src = urllib2.urlopen(req).read()
svf.write(src)
svf.close()
OCRparams = ['FineOCR.exe', capatcha_name, '/lang English', '/send Clipboard']
os.spawnv(os.P_WAIT, 'ABBYY FineReader 8.0 Professional Edition\\FineOCR.exe', OCRparams)
k=GetClipboardText()
form[capatchaForm] = k
if capatcha==True and capatchaType=='Cap':
req=urllib2.Request(capatchaURL,data,headers)
svf = open(capatcha_name,'wb')
src = urllib2.urlopen(req).read()
svf.write(src)
svf.close()
log=open('log.txt','a')
log.writelines('\n'+time.asctime() +' '+loginURL + ' ' +username+ ' ' + str(proxy_addres) + ' capatcha saved, Cap method started')
log.close()
OCRparams = ['Cap.exe',
'/set Section=CAP Name=FileNameCapture value='+capatcha_name + ' File='+serviceINI]
os.spawnv(os.P_WAIT, 'Cap\\Cap.exe', OCRparams)
shutil.move(capatcha_name, "Cap\\"+capatcha_name)
OCRparams = ['Cap.exe', '/ini '+serviceINI]
os.spawnv(os.P_WAIT, 'Cap\\Cap.exe', OCRparams)
log=open('log.txt','a')
log.writelines('\n'+time.asctime() +' '+loginURL + ' ' +username+ ' ' + str(proxy_addres) + ' capatcha solved by Cap')
log.close()
k=GetClipboardText()
form[capatchaForm] = k
if capatcha==True and capatchaType=='Main':
req=urllib2.Request(capatchaURL,data,headers)
svf = open(capatcha_name,'wb')
src = urllib2.urlopen(req).read()
svf.write(src)
svf.close()
log=open('log.txt','a')
log.writelines('\n'+time.asctime() +' '+loginURL + ' ' +username+ ' ' + str(proxy_addres) + ' capatcha saved, Main method started')
log.close()
self.window = Tk()
self.window.title('Cap')
tv = StringVar()
entry1 = Entry(self.window,textvariable=tv,takefocus=True)
entry1.grid(row=1, column=0)
imtk = ImageTk.PhotoImage(file = capatcha_name)
panel1 = Label(self.window, image=imtk)
panel1.grid(row=0, column=0)
button = Button(self.window,text='Ok', command=lambda name="Guido": auth.buttonPress(entry1.get()))
button.grid(row=3, column=0)
self.window.mainloop()
k=auth.capatcha_value
form[capatchaForm] = k
#-------------------------------------------
res=urllib2.urlopen(form.click()).read() # заполнил форму, отправил...
if urllib2.urlopen(urllib2.Request('http://'+urlparse.urlsplit(loginURL)[1], # проверил, вошло ли?
data,headers)).read().find(username)==-1:
cj.save(cookieFile)
return 0,'login feild'
else:
cj.save(cookieFile)
return 1,'login complit'
else:
cj.save(cookieFile)
return 1,'allready logined'
serf=Authoriz()
#serf.login('124bit','1993715','cookie2.txt','http://www.thinkbux.com/','128.223.8.113:3124')
#serf.login('blackm','777777','cookie1.txt','http://www.thinkbux.com/','128.2.223.63:3128')
по одному работает.. а одновременно в 2 потоках не хочет.. ошибки лезут.. зашло или не зашло надо в кукисах глядеть… кстати, нет ли советов как это оптимизировать,,?