1. f1.py
#-*-coding:utf-8-*-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. f2.py
# -*-coding:utf-8-*- Import syssys.path.append ('d:/360data/Important data/desktop ') Import == [] for in range (0): open.append (f1.run_client ()) print(Open)
Python socket programming Three: Simulating database cyclic publishing data