李莎 2 years ago
commit
aa057a2ab1
3 changed files with 88 additions and 0 deletions
  1. 49 0
      MyUtils/ConfigUtils.py
  2. 6 0
      config.xml
  3. 33 0
      start.py

+ 49 - 0
MyUtils/ConfigUtils.py

@@ -0,0 +1,49 @@
+import xml.etree.ElementTree as ET
+
+
+#读取xml文件
+class ConfigUtils():
+    def __init__(self,file):
+        self.url = ""
+        self.tree = ET.parse(file)
+        self.root = self.tree.getroot()
+
+    def readTop(self, key, child):
+        datas = []
+        data = self.root.find(key)
+        for key in child:
+            datas.append(data.get(key))
+        return datas
+
+    def readTopDict(self, key, child):
+        datas = {}
+        data = self.root.find(key)
+        for key in child:
+            datas[key] = data.get(key)
+        return datas
+
+    def readConfig(self,parent,child):
+        datas=[]
+        for childLine in self.root.find(parent):
+            data=[]
+            for key in child:
+                data.append(childLine.get(key))
+            datas.append(data)
+        return datas
+
+    def readConfigDict(self,parent,child):
+        datas=[]
+        for childLine in self.root.find(parent):
+            data={}
+            for key in child:
+                data[key]=childLine.get(key)
+            datas.append(data)
+        return datas
+
+    def readConfigSingle(self,key):
+        data = self.root.find(key).text
+        return data
+
+
+if __name__ == '__main__':
+    config = ConfigUtils("logger.conf")

+ 6 - 0
config.xml

@@ -0,0 +1,6 @@
+<root>
+
+    <!-- database:数据源位置  -->
+    <mysql database="zjsys_saas-org-person" host="10.100.28.84" passwd="gWK5o9WmCBF5LiW" port="9934" user="root" />
+    <config url="http://10.100.28.79/duoduo-service/setup-service/synUserInfo/synAllUser"/>
+</root>

+ 33 - 0
start.py

@@ -0,0 +1,33 @@
+import json
+
+from MyUtils.ConfigUtils import ConfigUtils
+import datetime,pymysql
+import requests
+SELECT_SQL="select * from `%s`.saas_employee"
+
+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
+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))))
+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()