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 一.错误现场还原: 下面我们通过三种方式来连接,然后观察提示的错误信息: ...
随机推荐
- 对Ip地址进行验证
依据正则表达式 boolean voliate(String ipString) { // 匹配ip正则表达式 String ip = "([1-9]|[1-9] ...
- 89c51中断入口地址表
中断源 中断入口地址 外部中断0 0003H 定时器/计数器T0 000BH 外部中断1 0013H 定时器/计数器T1 001BH 串行口中断 0023H
- 不停止MySQL服务的情况下修改root的密码
首先我们得知道一个MySQL普通用户的密码 这里我来记录一下我的操作过程 这里我刚刚到一家公司上面装的是cacti,但是之前的运维不记得MySQL的root密码了 但是他知道cacti的密码, 用户: ...
- 【BZOJ 2005】【NOI 2010】能量采集 数论+容斥原理
这题设$f(i)$为$gcd(i,j)=x$的个数,根据容斥原理,我们只需减掉$f(i×2),f(i×3)\cdots$即可 那么这道题:$$ans=\sum_{i=1}^n(f(i)×((i-1)× ...
- 在ORACLE触发器里调用JAVA程序
因为项目需要,有一个已经写好的Java程序,想要在Oracle某个表的触发器中调用,以使得每次数据更新时,调用这个JAVA程序,来修改后台某个数据. 现将过程记录如下: 1.编写JAVA程序 publ ...
- Hive 中的分号问题
1. hive表中有一列值,是以 分号 ; 为分隔符连接存储的 1470047164;1470047628;1470049068;1470048978;1470048922;1470047658;1 ...
- python-学习笔记1-面向对象编程
1.面向对象最重要的概念就是类(Class)和实例(Instance),类是抽象的模板,而实例是根据类创建出来的一个个具体的"对象",每个对象都拥有相同的方法,但各自的数据可能不同 ...
- 【BZOJ-3786】星系探索 Splay + DFS序
3786: 星系探索 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 647 Solved: 212[Submit][Status][Discuss] ...
- SSH无密码登陆Agent admitted failure to sign using the key
A :CentOS_Master B:Slave_1 C:Slave_2 普通用户hxsyl 1.现在A 上 ssh-keygen -t rsa 一路回车,不需要输入密码 执行该操作将在/home/h ...
- springMVC-配置Bean
配置Bean - 配置形式:基于xml文件方式,基于注解的方式- bean的配置方式:通过全类名(反射),通过工厂方法(静态工厂方法和实例工厂方法),FactoryBean依赖注入的方方式,属性注入和 ...