Python socket编程之七:多窗口的应用

时间:2023-03-09 17:58:52
Python socket编程之七:多窗口的应用

f1.py

# -*- coding: utf-8 -*-
import socket
import struct
import sqlalchemy
import pandas
########################################################################
class Socket:
#----------------------------------------------------------------------
def __init__(self, Host = '192.168.1.3', Port = 12345):
self.Host = Host
self.Port = Port
#----------------------------------------------------------------------
def Run_server(self):
Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Socket.bind((self.Host, self.Port))
Socket.listen(5)
Engine = sqlalchemy.create_engine('mssql+pyodbc://sa:123456@XiTongDSN')
'''修改1'''
Dataframe = pandas.read_sql('sh', Engine)
I = list(Dataframe['date'].index)
O = Dataframe['open']
H = Dataframe['high']
L = Dataframe['low']
C = Dataframe['close']
V = Dataframe['volume']
i = 0
while True:
Connection, Address = Socket.accept()
if Connection.recv(1024) == b'Link' and i < len(I):
'''修改2'''
Connection.send(struct.pack('i5f', I[i], O[i], H[i], L[i], C[i], V[i]))
i += 1
else:
Connection.close()
Socket.close()
#----------------------------------------------------------------------
def Run_client(self, Message = b'Link'):
Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Socket.connect((self.Host, self.Port))
Socket.send(Message)
'''修改3'''
I, O, H, L, C, V = struct.unpack('i5f', Socket.recv(1024)[:24])
Socket.close()
return I, O, H, L, C, V

f2.py

# -*- coding: utf-8 -*-
import sys
sys.path.append('C:\WinPython-32bit-3.5.1.3\myWorkSpace1')
import f1
F1 = f1.Socket()
F1.Run_server()

f3.py

# -*- coding: utf-8 -*-
import f1
F1 = f1.Socket()
import matplotlib.pylab as Plot
from matplotlib.finance import candlestick_ohlc as Drawk
'''修改1'''
Figure = Plot.figure('Made by DengChaohai')
f1 = Figure.add_subplot(3, 4, (1, 7), title = 'Index', xlim = [0, 100])
Plot.grid(True)
f2 = Figure.add_subplot(1, 4, 4, title = 'Trading price', ylim = [-6, 6])
Plot.grid(True)
f3 = Figure.add_subplot(3, 4, (9, 11), title = 'Volume', xlim = [0, 100])
Plot.grid(True)
Quotes = []
TRUE = True
while TRUE:
I, O, H, L, C, V = F1.Run_client()
Quotes.append((I, O, H, L, C, V))
Drawk(f1, Quotes, width = 0.5, colorup = 'g', colordown = 'r')
f2.axhline(y = 0)
f3.bar(I, V, width = 0.5, color = 'c', edgecolor = 'c')
Plot.pause(0.1)
if I > 100:
TRUE = False
F1.Run_client(b'disconnect')

Python socket编程之七:多窗口的应用