123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import json
- import pytz,time
- import datetime,pymysql
- import requests
- SELECT_SQL="select * from `%s`.saas_employee"
- def datetime_now():
- # datetime_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
- #容器时间
- # tz = pytz.timezone('Asia/Shanghai') # 东八区
- datetime_now = datetime.datetime.fromtimestamp(int(time.time()),
- pytz.timezone('Asia/Shanghai')).strftime('%Y-%m-%d %H:%M')
- return datetime_now
- # datetimenow = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
- # config = ConfigUtils("config.xml")
- # mysql = config.readTopDict("mysql", ["host", "port", "user", "passwd", "database"])
- # mysql["port"] = int(mysql["port"])
- # database = mysql["database"]
- # url = config.readTop("config",["url"])[0]
- mysql = {'host': '10.100.28.84', 'port': 9934, 'user': 'root', 'passwd': 'gWK5o9WmCBF5LiW', 'database': 'zjsys_saas-org-person'}
- database = "zjsys_saas-org-person"
- url = "http://10.100.28.79/duoduo-service/setup-service/synUserInfo/synAllUser"
- while True:
- time_now = datetime.datetime.fromtimestamp(int(time.time()),
- pytz.timezone('Asia/Shanghai')).strftime('%H:%M:%S')
- am_set_time = "05:00:00"
- if time_now == am_set_time: # 此处设置每天定时的时间
- #连接mysql
- conn = pymysql.connect(**mysql)
- mysql_cur = conn.cursor()
- mysql_cur.execute(SELECT_SQL % (database))
- #获取列名
- column_list = []
- column = mysql_cur.description
- for i in column:
- column_list.append(i[0])
- datas = mysql_cur.fetchall()
- lst = []
- for line in datas:
- lst.append(dict(zip(column_list,list(line))))
- print("%s 总共需要同步%s条数据"%(datetime_now(),len(lst)))
- for i in range(0,len(lst),100):
- dataranges = lst[i:i + 100]
- data_json = json.dumps(dataranges)
- r = requests.post(url=url,headers={'Content-Type': 'application/json;charset=UTF-8'},data=data_json)
- print(r.text)
- mysql_cur.close()
- conn.close()
- print("%s 同步完成"%(datetime_now()))
- time.sleep(2)
|