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) Index = list(Dataframe['date'].index) Close = Dataframe['close'] Volume = Dataframe['volume'] i = 0 while True: Connection, Address = Socket.accept() if Connection.recv(1024) == b'Link' and i < len(Close): '''修改2''' Connection.send(struct.pack('iff', Index[i], Close[i], Volume[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''' Index, Close, Volume = struct.unpack('iff', Socket.recv(1024)[:12]) Socket.close() return Index, Close, Volume
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 '''修改1''' Sub1 = Plot.subplot(211) Sub2 = Plot.subplot(212) index = [] close = [] volume = [] while True: Index, Close, Volume = F1.Run_client() index.append(Index) close.append(Close) volume.append(Volume) if Index > 10: Sub1.plot(index[Index-10:Index-1:1], close[Index-10:Index-1:1]) Sub2.bar(index[Index-10:Index-1:1], volume[Index-10:Index-1:1]) Plot.pause(0.01)
自设变量,头文字大写!
补充:
- 1.
import struct import sqlalchemy import pandas Engine = sqlalchemy.create_engine('mssql+pyodbc://sa:123456@XiTongDSN') Dataframe = pandas.read_sql('sh', Engine) Data, = struct.unpack('10s', struct.pack('10s', Dataframe['date'][0].encode('utf-8'))) print(Data.decode('utf-8'))
- 2.
import struct import sqlalchemy import pandas Engine = sqlalchemy.create_engine('mssql+pyodbc://sa:123456@XiTongDSN') Dataframe = pandas.read_sql('sh', Engine) A = Dataframe[:1] print(A, '\n') B = struct.pack('f10s', A['open'][0], A['date'][0].encode('utf-8')) print(B, '\n') C, D = struct.unpack('f10s', B) print(C, D.decode('utf-8'))