Set up a socket, run the server first, then run the client, set up the connection after the server from the local database to send a truncated data to the client, the client accepts the data drawing simulation ticks
#-*-coding:utf-8-*-"""Socket Module"""ImportSocketImportstructImportSQLAlchemyImportPandas########################################################################classSckt:#---------------------------------------------------------------------- def __init__(Self, host ='192.168.1.3', Port = 12345): Self.host=host Self.port=Port#---------------------------------------------------------------------- defRun_server (self): Sckt=Socket.socket (socket.af_inet, socket. Sock_stream) Sckt.bind ((Self.host, Self.port)) Sckt.listen (5) """Connection Library"""engine= Sqlalchemy.create_engine ('Mssql+pyodbc://sa:[email protected]') """Take open Price"""Open= (Pandas.read_sql ('SH', engine)) ['Open'] I=0 whiletrue:connection, Address=sckt.accept ()ifCONNECTION.RECV (1024x768) = = B'Link' andI < (len (Open)-1): """Data Packaging"""connection.send (Struct.pack ('F', Open[i])) I+ = 1Else: connection.send (b'Cut off the connection!') Connection.close ()#---------------------------------------------------------------------- defRun_client (self, message = b'Link'): Sckt=Socket.socket (socket.af_inet, socket. Sock_stream) Sckt.connect ((Self.host, Self.port)) sckt.send (message)"""data Unpacking, note that unpack requires four bytes, which is used after the following [: 4]"""Open= Struct.unpack ('F', SCKT.RECV (1024) [: 4]) sckt.close ()"""return open price, go to format usage followed by [0]""" returnOPEN[0]
# -*-coding:utf-8-*- """ server-side single file """ Import syssys.path.append ('C:\WinPython-32bit-3.5.1.3\myWorkSpace1') Import= f1.sckt () f1.run_server ()
#-*-coding:utf-8-*-"""client alone one file"""Importf1f1=f1.sckt ()ImportMatplotlib.pylab as Pltopn= [] forIinchRange (0, 1000): Opn.append (F1.run_client ()) Plt.plot (OPN) Plt.grid (True, Axis='both') """There must be pause, or it will get stuck."""Plt.pause (0.1)"""run the server first, run the client"""
Python socket Programming Four: analog tick chart