Skip to content
This repository was archived by the owner on Mar 2, 2020. It is now read-only.

Commit 14e08df

Browse files
committed
Initial commit
0 parents  commit 14e08df

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

Diff for: SimpleClient.py

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from __future__ import unicode_literals
5+
import sys
6+
import xlrd
7+
import socket
8+
from PyQt4 import QtGui, QtCore
9+
10+
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
11+
from matplotlib.figure import Figure
12+
13+
14+
class MyMplCanvas(FigureCanvas):
15+
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
16+
17+
def __init__(self, parent=None, width=5, height=4, dpi=100):
18+
fig = Figure(figsize=(width, height), dpi=dpi)
19+
self.axes = fig.add_subplot(111)
20+
# We want the axes cleared every time plot() is called
21+
self.axes.hold(False)
22+
23+
self.compute_initial_figure()
24+
25+
#
26+
FigureCanvas.__init__(self, fig)
27+
self.setParent(parent)
28+
29+
FigureCanvas.setSizePolicy(self,
30+
QtGui.QSizePolicy.Expanding,
31+
QtGui.QSizePolicy.Expanding)
32+
FigureCanvas.updateGeometry(self)
33+
34+
def compute_initial_figure(self):
35+
pass
36+
37+
38+
class MyDynamicMplCanvas(MyMplCanvas):
39+
"""A canvas that updates itself every second with a new plot."""
40+
41+
def update_figure(self):
42+
sock = socket.socket()
43+
sock.connect(('localhost', 45454))
44+
sock.send('RCV'.encode('utf-8'))
45+
data = [axis.split(',') for axis in sock.recv(1024).decode('utf-8').split(';')[:2]]
46+
points = [[float(data[i][j]) for j in range(10)] for i in range(2)]
47+
self.axes.plot(points[0], points[1], 'k.')
48+
self.draw()
49+
aw.statusBar().showMessage("Data was successfully received!", 2000)
50+
sock.send('DSC'.encode('utf-8'))
51+
sock.close()
52+
53+
class ApplicationWindow(QtGui.QMainWindow):
54+
def __init__(self):
55+
QtGui.QMainWindow.__init__(self)
56+
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
57+
self.setWindowTitle("Simple Client")
58+
59+
self.main_widget = QtGui.QWidget(self)
60+
61+
l = QtGui.QVBoxLayout(self.main_widget)
62+
sc = MyDynamicMplCanvas(self.main_widget, width=5, height=4, dpi=100)
63+
sendfile = QtGui.QPushButton('Send File', self.main_widget)
64+
sendfile.clicked.connect(self.sendfileEvent)
65+
getdata = QtGui.QPushButton('Get Data', self.main_widget)
66+
getdata.clicked.connect(sc.update_figure)
67+
l.addWidget(sc)
68+
l.addWidget(sendfile)
69+
l.addWidget(getdata)
70+
71+
self.main_widget.setFocus()
72+
self.setCentralWidget(self.main_widget)
73+
74+
self.statusBar().showMessage("Welcome!", 2000)
75+
76+
def sendfileEvent(self):
77+
sock = socket.socket()
78+
sock.connect(('localhost', 45454))
79+
xlsfilename = QtGui.QFileDialog.getOpenFileName(self, 'Open file', './')
80+
if xlsfilename:
81+
workbook = xlrd.open_workbook(xlsfilename)
82+
else:
83+
sock.send('DSC'.encode('utf-8'))
84+
sock.close()
85+
return
86+
worksheet = workbook.sheet_by_index(0)
87+
#data = array([[worksheet.cell(i, j).value for i in range(10)] for j in range(2)])
88+
data = 'SND'+''.join(','.join(str(worksheet.cell(i, j).value) for i in range(10))+';' for j in range(2))
89+
90+
sock.send(data.encode('utf-8'))
91+
msg = sock.recv(1024).decode('utf-8')
92+
93+
if msg == 'OK':
94+
sock.send('DSC'.encode('utf-8'))
95+
sock.close()
96+
self.statusBar().showMessage("Data was successfully uploaded!", 2000)
97+
98+
qApp = QtGui.QApplication(sys.argv)
99+
100+
aw = ApplicationWindow()
101+
aw.show()
102+
sys.exit(qApp.exec_())

Diff for: SimpleServer.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import socket
5+
6+
sock = socket.socket()
7+
sock.bind(('', 45454))
8+
sock.listen(1)
9+
while True:
10+
conn, addr = sock.accept()
11+
print('Successfully connected:', addr)
12+
points = ''
13+
while True:
14+
data = conn.recv(1024).decode('utf-8')
15+
print(data)
16+
if data[:3] == 'SND':
17+
print('Got Data')
18+
points = data[3:]
19+
with open('data.txt', 'w') as f:
20+
f.write(points)
21+
conn.send('OK'.encode('utf-8'))
22+
if data[:3] == 'RCV':
23+
if points:
24+
conn.send(points.encode('utf-8'))
25+
else:
26+
with open('data.txt', 'r') as f:
27+
conn.send(f.read().encode('utf-8'))
28+
if data[:3] == 'DSC':
29+
break
30+
print('Successfully disconnected')
31+
conn.close()

Diff for: data.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0;1.0,4.0,9.0,16.0,25.0,36.0,49.0,64.0,81.0,100.0;

Diff for: data.xls

6 KB
Binary file not shown.

0 commit comments

Comments
 (0)