Python socket編程之三:類比資料庫迴圈發布資料

來源:互聯網
上載者:User

標籤:

1. f1.py

# -*- coding: utf-8 -*-import socketimport structimport sqlalchemyimport 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:[email protected]‘)        """取開盤價"""        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. f2.py

# -*- coding: utf-8 -*-import syssys.path.append(‘D:/360data/重要資料/案頭‘)import f1F1 = f1.sckt()F1.run_server()Open = []for i in range(0, 99):    Open.append(F1.run_client())    print(Open)

Python socket編程之三:類比資料庫迴圈發布資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.