码迷,mamicode.com
首页 > 数据库 > 详细

Python socket编程之三:模拟数据库循环发布数据

时间:2016-05-29 06:26:18      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:

1. f1.py

# -*- coding: utf-8 -*-
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) == bLink and i < (len(Open) - 1):               
                """数据打包"""                
                connection.send(struct.pack(f, Open[i]))
                i += 1
            else:
                connection.send(bCut off the connection!)
                connection.close()
    #----------------------------------------------------------------------
    def run_client(self, message = bLink):
        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 sys
sys.path.append(D:/360data/重要数据/桌面)
import f1
F1 = f1.sckt()
F1.run_server()
Open = []
for i in range(0, 99):
    Open.append(F1.run_client())
    print(Open)
技术分享

Python socket编程之三:模拟数据库循环发布数据

标签:

原文地址:http://www.cnblogs.com/blog-3123958139/p/5538821.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!