На lavrsen.dk опубликован новый клиент(c использованием PyQt4) для motion от MarcoMa:
Вот текст:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore
import threading
from socket import *
import Queue
import time
class Conn(threading.Thread):
JPEG_HEAD = """\xff\xd8"""
JPEG_TAIL = """\xff\xd9"""
#HOST = '172.16.24.56'
def __init__(self, parent):
threading.Thread.__init__(self, name='Conn')
self._stopEvent = threading.Event()
self.buf = ''
self.parent = parent
def join(self, timeout=None):
self._stopEvent.set()
def process(self):
begin = end = 0
while True:
try:
begin = self.buf.index(self.JPEG_HEAD, begin)
except Exception, ex:
#print 'no begin'
time.sleep(0.1)
break
try:
end = self.buf.index(self.JPEG_TAIL, begin) + len(self.JPEG_TAIL)
except:
#print 'no end'
time.sleep(0.1)
break
img = self.buf[begin : end]
begin = end
self.parent.img_queue.put(img)
#self.emit(QtCore.SIGNAL("show_picture(QString)"), '')
self.buf = self.buf[begin:]
def run(self):
err_counter = 0
while not self._stopEvent.isSet():
self.sd = socket(AF_INET, SOCK_STREAM)
self.sd.settimeout(16)
try:
self.sd.connect((self.parent.HOST, self.parent.PORT))
except Exception, ex:
print 'cannot connect to host:', ex
self.sd.close()
time.sleep(1)
err_counter += 1
if err_counter > 5:
self.parent.disconnect_slot()
self._stopEvent.set()
continue
while not self._stopEvent.isSet():
try:
data = self.sd.recv(1024 * 64)
if data:
self.buf += data
else:
print 'link down'
break
except Exception, ex:
print 'recv except: %s' %(ex)
break
self.process()
#print len(self.buf)
#print '======================\n'
class ConnWindow(QtGui.QWidget):
def __init__(self, parent):
QtGui.QWidget.__init__(self)
self.parent = parent
grid_layout = QtGui.QGridLayout()
if self.parent.HOST or self.parent.PORT:
self.host_edit = QtGui.QLineEdit(self.parent.HOST, self)
self.port_edit = QtGui.QLineEdit(str(self.parent.PORT), self)
self.conn_btn = QtGui.QPushButton('done', self)
else:
self.host_edit = QtGui.QLineEdit('set host name here', self)
self.port_edit = QtGui.QLineEdit('set port number here', self)
self.conn_btn = QtGui.QPushButton('done', self)
self.connect(self.conn_btn, QtCore.SIGNAL('clicked()'), self.done_slot)
grid_layout.addWidget(self.host_edit, 0, 0)
grid_layout.addWidget(self.port_edit, 1, 0)
grid_layout.addWidget(self.conn_btn, 3, 3)
self.setLayout(grid_layout)
self.resize(320, 320)
def done_slot(self):
host = self.host_edit.text()
port = self.port_edit.text().toInt()
if port[1]:
port = port[0]
if (port > 65535 or port <= 0):
QtGui.QMessageBox.warning(self, 'ERROR', 'Port should be a integer between 0 and 65535')
return
else:
QtGui.QMessageBox.warning(self, 'ERROR', 'Port should be a integer')
return
sd = socket(AF_INET, SOCK_STREAM)
try:
sd.connect((host, port))
sd.close()
except Exception, ex:
QtGui.QMessageBox.warning(self,"CANNOT ACCESS",'Cannot connect to service you given! Please input host name and port name again.\ndetail: %s' %(ex))
return
self.parent.HOST = host
self.parent.PORT = port
self.parent.connect_slot()
self.close()
class Gui(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.pic_label_frame = QtGui.QFrame(self)
self.pic_label_frame.setGeometry(0, 0, 720, 540)
self.pic_label = QtGui.QLabel(self.pic_label_frame)
self.pic_label.setScaledContents(True)
self.scroll = QtGui.QScrollArea(self.pic_label_frame)
self.scroll.setWidget(self.pic_label)
self.scroll.resize(720, 540)
self.scroll.setWidgetResizable(True)
self.cmd_frame = QtGui.QFrame(self)
self.cmd_frame.setGeometry(0, 540, 720, 40)
self.conn_btn = QtGui.QPushButton(self.cmd_frame)
self.conn_btn.setGeometry(0, 10, 100, 30)
self.conn_btn.setText('CONNECT')
self.pic_label.setText('click "CONNECT" to setup host and port')
self.connect(self.conn_btn, QtCore.SIGNAL('clicked()'), self.show_conn_window)
self.img_queue = Queue.Queue()
#self.connect(self.conn, QtCore.SIGNAL('show_picture(QString)'), self.show_picture_sig)
self.timer = QtCore.QTimer(self)
self.connect(self.timer,QtCore.SIGNAL("timeout()"),self.show_picture_timer)
self.HOST = ''
self.PORT = 0
self.resize(720, 580)
def connect_slot(self):
self.disconnect_slot()
self.conn = Conn(self)
self.conn.start()
self.timer.start()
def disconnect_slot(self):
self.timer.stop()
if hasattr(self, 'conn'):
self.conn.join()
self.pic_label.setText('disconnected')
#def show_picture_sig(self, data):
#img = QtGui.QPixmap()
#if img.loadFromData(self.img_queue.get_nowait()):
#print 'showing'
#self.pic_label.setPixmap(img)
#else:
#print 'not a image, cannot show'
def show_conn_window(self):
self.conn_win = ConnWindow(self)
self.conn_win.show()
def show_picture_timer(self):
try:
data = self.img_queue.get_nowait()
except:
return
img = QtGui.QPixmap()
if img.loadFromData(data):
self.pic_label.setPixmap(img)
else:
print 'not a image, cannot show'
####
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
run = Gui()
run.show()
app.exec_()