Mysql 与 Python socket
py1.py
# -*- coding: utf-8 -*-
import sqlalchemy
import tushare
import pandas
import socket
import struct class Mysql0: def __init__(self, user0='test', password0='123456', dsn0='odbcdsn', host0='192.168.1.3', port0=12345):
self.user0 = user0
self.password0 = password0
self.dsn0 = dsn0
self.engine0 = sqlalchemy.create_engine('mysql+pyodbc://'+self.user0+':'+self.password0+'@'+self.dsn0)
self.host0 = host0
self.port0 = port0 def get_url_data0(self):
codelist0 = pandas.DataFrame((tushare.get_today_all())['code'])
codelist0 = codelist0.sort(columns='code', ascending=True)
codelist0['index'] = pandas.DataFrame([i for i in range(0, len(codelist0))], codelist0.index)
codelist0.reindex(index=codelist0['code'])
codelist0.to_sql('codelist', self.engine0, if_exists='replace', index=True, index_label='index')
self.engine0.connect().execute('alter table testdb.codelist add primary key(`index`)')
for i in range(0, len(codelist0)):
stockdata0 = tushare.get_h_data(codelist0['code'][i])
stockdata0['date'] = pandas.Series(stockdata0.index, stockdata0.index)
stockdata0 = stockdata0.sort_values(by='date', ascending=True)
stockdata0['index'] = pandas.DataFrame([i for i in range(0, len(stockdata0))], stockdata0.index)
stockdata0.to_sql(codelist0['code'][i], self.engine0, if_exists='replace', index=True, index_label='index')
self.engine0.connect().execute('alter table testdb.'+codelist0['code'][i]+' add primary key(`index`)') def get_sql_data0(self, code0='000410'):
stockdata1 = pandas.read_sql_table(code0, self.engine0)
return code0, stockdata1 def run_socket_server0(self):
socket0 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket0.bind((self.host0, self.port0))
socket0.listen(5)
code1, stockdata2 = self.get_sql_data0()
i0 = stockdata2['index']
o0 = stockdata2['open']
h0 = stockdata2['high']
l0 = stockdata2['low']
c0 = stockdata2['close']
v0 = stockdata2['volume']
i = 0
while True:
connection0, address0 = socket0.accept()
if connection0.recv(1024) == b'connect' and i < len(i0):
connection0.send(struct.pack('6si5f', code1.encode('utf-8'), i0[i], o0[i], h0[i], l0[i], c0[i], v0[i]))
i += 1
else:
connection0.send(struct.pack('6si5f', '888888'.encode('utf-8'), 8, 8, 8, 8, 8, 8))
connection0.close()
socket0.close() def run_socket_client0(self, message0=b'connect'):
socket1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket1.connect((self.host0, self.port0))
socket1.send(message0)
code2, i1, o1, h1, l1, c1, v1 = struct.unpack('6si5f', socket1.recv(1024)[:32])
socket1.close()
return code2.decode('utf-8'), i1, o1, h1, l1, c1, v1
py2.py
# coding=utf-8
import sys
import py1
sys.path.append('D:\\360data\\MyWorkSpace1')
Py1 = py1.Mysql0()
Py1.run_socket_server0()
py3.py
# coding=utf-8
import sys
import time
import py1
import pandas
sys.path.append('D:\\360data\\MyWorkSpace1')
Py1 = py1.Mysql0()
f0 = open('c:\\1.txt', 'a')
f0.write('code'+','+'index'+','+'open'+','+'high'+','+'low'+','+'close'+','+'volume'+'\n')
while True:
code1, i1, o1, h1, l1, c1, v1 = Py1.run_socket_client0()
if code1 == '888888':
break
f0.write(code1+','+str(i1)+','+str(o1)+','+str(h1)+','+str(l1)+','+str(c1)+','+str(v1)+'\n')
time.sleep(0.01)
f0.close()
f1 = pandas.read_csv('c:\\1.txt')
print(f1)
Mysql 与 Python socket的更多相关文章
- 连接Mysql提示Can’t connect to local MySQL server through socket的解决方法
mysql,mysqldump,Mysqladmin,php连接mysql服务常会提示下面错误: ERROR 2002 (HY000): Can't connect to local MySQL se ...
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
有时候,当我们使用"mysql"."mysqladmin"."mysqldump"等命令管理数据库时,服务器抛出类似如下错误: 一.错误现场 ...
- 启动Mysql服务提示Can’t connect to local MySQL server through socket的解决方法
启动Mysql服务常会提示下面错误: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/ ...
- Can 't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock '(2) ;
Can 't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock '(2) ; 如果你在网上search这个 ...
- OperationalError: (2002, “Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)”)
OperationalError: (2002, “Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld ...
- PHP连接MySQL报错:SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket 'MySQL' (2)
如下所示,PHP连接MySQL报错: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket 'MySQL' ...
- Ubuntu+Django+Nginx+uWSGI+Mysql搭建Python Web服务器
Ubuntu+Django+Nginx+uWSGI+Mysql搭建Python Web服务器 闲着无聊的时候部署了一个Django项目玩,用vm虚拟机部署的. 准备工作 我使用的系统是Ubuntu16 ...
- Can’t connect to local MySQL server through socket的解决方法
http://www.aiezu.com/db/mysql_cant_connect_through_socket.html mysql,mysqldump,php连接mysql服务常会提示下面错误: ...
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket
原文链接:https://blog.csdn.net/u011262253/article/details/82802157 一.错误现场还原: 下面我们通过三种方式来连接,然后观察提示的错误信息: ...
随机推荐
- 【POJ 3241】Object Clustering 曼哈顿距离最小生成树
http://poj.org/problem?id=3241 曼哈顿距离最小生成树模板题. 核心思想是把坐标系转3次,以及以横坐标为第一关键字,纵坐标为第二关键字排序后,从后往前扫.扫完一个点就把它插 ...
- 你所不知道的Android Studio调试技巧
转载:http://www.jianshu.com/p/011eb88f4e0d Android Studio目前已经成为开发Android的主要工具,用熟了可谓相当顺手.作为开发者,调试并发现bug ...
- [bzoj1787][Ahoi2008]紧急集合
Description 给定一棵大小为的树,有组询问,每组询问给三个点,求到这三个点距离和最小的点及最小距离和. Input 第一行两个数. 接下来行,每行两个数表示到有一条边. 最后行,每行个数,为 ...
- bzoj2683简单题
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> ...
- hdu 4609 FFT
题意:给出一堆数,问从这些数中取3个能组成三角形的概率? sol:其实就是问从这些数里取3个组成三角形有多少种取法 脑洞大开的解法:用FFT 设一开始的数是1 3 3 4 作一个向量x,其中x[i]= ...
- 洛谷P1410 子序列
题目描述 给定一个长度为N(N为偶数)的序列,问能否将其划分为两个长度为N/2的严格递增子序列, 输入输出格式 输入格式: 若干行,每行表示一组数据.对于每组数据,首先输入一个整数N,表示序列的长度. ...
- lua中的table、stack和registery
ok,前面准备给一个dll写wrapper,写了篇日志,看似写的比较明白了,但是其实有很多米有弄明白的.比如PIL中使用的element,key,tname,field这些,还是比较容易混淆的.今天正 ...
- subprocess实现管道
# shell last | cut -d ' ' -f 1 | sort -u #python from subprocess import Popen,PIPE p1 = Popen('last' ...
- GCD XOR, ACM/ICPC Dhaka 2013, UVa12716
不同的枚举方法,效率完全不同.值得记录一下! #include <cstdio> #include <cstring> , count = ; ]; void pre() { ...
- Unity 插件制作笔记(持续更新)
示例: [MenuItem(Menu1/Menu2)] static void TestMenu1() { Debug.log("点击了菜单"); } 导航菜单添加 [MenuIt ...