Python socket编程之四:模拟分时图
建立 socket,先运行服务器,再运行客户端,建立连接后服务器从本地数据库调数据一截一截地发送给客户端,客户端接受数据绘图模拟分时图
- 1.socket
# -*- coding: utf-8 -*- """socket 模块""" import socket import struct import sqlalchemy import pandas ######################################################################## class sckt: #---------------------------------------------------------------------- def __init__(self, host = '192.168.1.3', port = 12345): self.host = host self.port = port #---------------------------------------------------------------------- def run_server(self): sckt = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sckt.bind((self.host, self.port)) sckt.listen(5) """连接库""" engine = sqlalchemy.create_engine('mssql+pyodbc://sa:123456@XiTongDSN') """取开盘价""" Open = (pandas.read_sql('sh', engine))['open'] i = 0 while True: connection, address = sckt.accept() if connection.recv(1024) == b'Link' and i < (len(Open) - 1): """数据打包""" connection.send(struct.pack('f', Open[i])) i += 1 else: connection.send(b'Cut off the connection!') connection.close() #---------------------------------------------------------------------- def run_client(self, message = b'Link'): sckt = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sckt.connect((self.host, self.port)) sckt.send(message) """数据解包,注意 unpack 要求四字节,其用法为后接 [:4]""" Open = struct.unpack('f', sckt.recv(1024)[:4]) sckt.close() """返回开盘价,去格式的用法后接 [0]""" return Open[0]
- 2.server
# -*- coding: utf-8 -*- """服务器端单独一个文件""" import sys sys.path.append('C:\WinPython-32bit-3.5.1.3\myWorkSpace1') import f1 F1 = f1.sckt() F1.run_server()
- 3.client
# -*- coding: utf-8 -*- """客户端单独一个文件""" import f1 F1 = f1.sckt() import matplotlib.pylab as plt opn = [] for i in range(0, 1000): opn.append(F1.run_client()) plt.plot(opn) plt.grid(True, axis = 'both') """必须有 pause,不然会卡住""" plt.pause(0.1) """先运行服务器,在运行客户端"""
- 4.runtimeplot