И вот, прошу вас помочь переделать мой код под многопоточность.
Сам код:
import os
import sys
import gui
import urllib.request
import urllib.parse
import re
import codecs
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from base64 import b64encode
import time, random
from PyQt4 import QtGui, QtCore
vk = urllib.request.build_opener()
def send_cap(key, fn):
data=open(fn, 'rb')
s = b64encode(data.read())
image = s.decode('latin-1')
params = urllib.parse.urlencode({'method': 'base64', 'key': key, 'body':image, 'ext':'jpg'})
f = urllib.request.urlopen("http://antigate.com/in.php", params)
res = str(f.read().decode())
print(res)
cap_id = re.search('\d+',res)
if cap_id == None:
return False
else:
idc = cap_id.group(0)
return idc
def get_cap_text(key, cap_id):
''' Ожидаем и получаем текст капчи '''
time.sleep(5)
# получаем результат
res_url= 'http://antigate.com/res.php'
res_url+= "?" + urllib.parse.urlencode({'key': key, 'action': 'get', 'id': cap_id})
flag=False
while flag==False:
res= urllib.request.urlopen(res_url).read()
res = res.decode()
if res == 'CAPCHA_NOT_READY':
time.sleep(1)
else:
flag=True
res= re.split('[\W]+',res)
if len(res[0]) == 2:
return res[1]
else:
return ('ERROR', res[0])
def login (mail, pas):
global vk
res = urllib.request.urlopen('http://login.vk.com/?act=login&email='+mail+'&pass='+pas+'&expire=&vk=')
res= str(res.read())
if len(res)== 699:
s = re.search(r" name=\\\'s\\\' id=\\\'s\\\' value=\\\'(.*?)\\\' />",res)
sid = s.group(1)
cookie = 'remixap=1; remixlang=0; remixchk=5; remixsid='+sid;
vk.addheaders = [('cookie:', cookie)]
else:
cookie=0
return cookie
def displaymatch(match):
if match is None:
return None
return '<Match: %r, groups=%r>' % (match.group(), match.groups())
class MyClass(QMainWindow,
gui.Ui_mainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self,parent)
self.setupUi(self)
def capch(w,res):
global cs,line
print('hello capcha!')
s = re.findall(r'\d+',res)
cs = s[1]
file = str(random.randint(100, 1000)) + '.jpeg'
print('We use antigate')
url = 'http://vkontakte.ru/captcha.php?sid='+cs
urllib.request.urlretrieve(url, file)
res = send_cap(w.lineEdit_3.text(), file)
code = get_cap_text(w.lineEdit_3.text(), res)
os.remove(file)
params = urllib.parse.urlencode({'act': 'register', 'first_name': w.lineEdit.text(), 'last_name': w.lineEdit_2.text(),'regemail':line, 'regpass':w.lineEdit_4.text(),'sex':'2','timezone':'300','by_login':'0','captcha_sid':cs, 'captcha_key':code})
res = urllib.request.urlopen('http://vkontakte.ru/register.php',params)
res= str(res.read().decode('cp1251'))
print(res)
if res=='{"do_login":"1"}':
w.textBrowser.insertPlainText("Успешно зарегестрирован"+line+":"+w.lineEdit_4.text()+"\n")
f = open('log.txt','a')
f.write(line+":"+w.lineEdit_4.text()+"\n")
f.close()
def bal(w):
res = urllib.request.urlopen('http://antigate.com/res.php?key='+w.lineEdit_3.text()+'&action=getbalance')
res = res.read().decode('utf-8')
w.textBrowser.insertPlainText("Ваш баланс:"+res+"\n")
def st(w):
global line
with open('emails.txt', 'r') as f:
data = f.read()
f.close()
m = re.split('[\n]+',data)
for line in m:
print(line)
params = urllib.parse.urlencode({'act': 'register', 'first_name': w.lineEdit.text(), 'last_name': w.lineEdit_2.text(),'regemail':line, 'regpass':w.lineEdit_4.text(),'sex':'2','timezone':'300','by_login':'0'})
res = urllib.request.urlopen('http://vkontakte.ru/register.php',params)
res = res.read().decode('utf-8')
c = re.search('captcha_sid',res)
if (c == None):
print('1')
else :
print('2')
print(res)
w.capch(res)
app = QApplication(sys.argv)
w = MyClass()
w.connect(w.pushButton,SIGNAL("clicked()"),w.st)
w.connect(w.pushButton_2,SIGNAL("clicked()"),w.bal)
w.show()
app.exec_()