I got class (subclassed from QThread), that recieve data from server at many sockets by select.select():
# -*- coding: utf-8 -*-
from PyQt4.QtCore import QThread, pyqtSignal
import json
import select
class MainSocketThread(QThread) :
disconnected_by_admin = pyqtSignal()
disconnected_by_network = pyqtSignal(bool)
def __init__(self, connects_dict=None) :
QThread.__init__(self)
self.connects = connects_dict
if not self.connects:
self.connects={}
def run(self) :
try:
while 1 :
inputready, outputready, exceptready = select.select(self.connects.keys(),
[], [])
for s in inputready :
try :
data = self.s_[s].recv(4096)
if not data :
s.close()
self.connects.pop(s)
else :
cmd = json.loads(data)
print s, cmd, 'asd'
# ProcessCommand(s, cmd)
except Exception as e:
s.close()
self.connects.pop(s)
except Exception as e:
print e
self.disconnected_by_network.emit(False)
self.exec_()
And that’s how i create socket(in other class) :
self.connections_dict = {}
self.main_socket_thread = MainSocketThread(self.connections_dict)
if not self.s :
try:
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.connect((host, port))
except Exception as e:
print e, e.__class__()
self.display_connection_status(False)
else:
self.connections_dict[self.s] = self
self.main_socket_thread.start()
self.s.send(json.dumps({'command': 'operator_insite',
'login': self.settings_dict['login'],
'password': hashlib.md5(self.settings_dict['password']).hexdigest()}))
self.display_connection_status(True)
But i got select.error 10022 every time trying to select from sockets. What is wrong with my code?
Okay. I solved this problem long ago. And here is solution using Pyqt: