Python socket Programming Four: analog tick chart

Source: Internet
Author: User

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

  • 1.socket
#-*-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]
  • 2.server
# -*-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 ()
  • 3.client
#-*-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"""
  • 4.runtimeplot

Python socket Programming Four: analog tick chart

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.