start.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import json
  2. import pytz,time
  3. import datetime,pymysql
  4. import requests
  5. SELECT_SQL="select * from `%s`.saas_employee"
  6. def datetime_now():
  7. # datetime_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
  8. #容器时间
  9. # tz = pytz.timezone('Asia/Shanghai') # 东八区
  10. datetime_now = datetime.datetime.fromtimestamp(int(time.time()),
  11. pytz.timezone('Asia/Shanghai')).strftime('%Y-%m-%d %H:%M')
  12. return datetime_now
  13. # datetimenow = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
  14. # config = ConfigUtils("config.xml")
  15. # mysql = config.readTopDict("mysql", ["host", "port", "user", "passwd", "database"])
  16. # mysql["port"] = int(mysql["port"])
  17. # database = mysql["database"]
  18. # url = config.readTop("config",["url"])[0]
  19. mysql = {'host': '10.100.28.84', 'port': 9934, 'user': 'root', 'passwd': 'gWK5o9WmCBF5LiW', 'database': 'zjsys_saas-org-person'}
  20. database = "zjsys_saas-org-person"
  21. url = "http://10.100.28.79/duoduo-service/setup-service/synUserInfo/synAllUser"
  22. while True:
  23. time_now = datetime.datetime.fromtimestamp(int(time.time()),
  24. pytz.timezone('Asia/Shanghai')).strftime('%H:%M:%S')
  25. am_set_time = "05:00:00"
  26. if time_now == am_set_time: # 此处设置每天定时的时间
  27. #连接mysql
  28. conn = pymysql.connect(**mysql)
  29. mysql_cur = conn.cursor()
  30. mysql_cur.execute(SELECT_SQL % (database))
  31. #获取列名
  32. column_list = []
  33. column = mysql_cur.description
  34. for i in column:
  35. column_list.append(i[0])
  36. datas = mysql_cur.fetchall()
  37. lst = []
  38. for line in datas:
  39. lst.append(dict(zip(column_list,list(line))))
  40. print("%s 总共需要同步%s条数据"%(datetime_now(),len(lst)))
  41. for i in range(0,len(lst),100):
  42. dataranges = lst[i:i + 100]
  43. data_json = json.dumps(dataranges)
  44. r = requests.post(url=url,headers={'Content-Type': 'application/json;charset=UTF-8'},data=data_json)
  45. print(r.text)
  46. mysql_cur.close()
  47. conn.close()
  48. print("%s 同步完成"%(datetime_now()))
  49. time.sleep(2)