禅道API接口,非REST接口
调试版本:11.3
百度出来的100%都用不了(本篇除外...)。于是自己修改了网上代码,先get session,再post登录,最后调用产品列表验证,实测通过。
1 import requests
2 import json
3
4 class zentao_cli(object):
5
6 session = None #用于实现单例类,避免多次申请sessionID
7 sid = None
8
9 def __init__(self, url, account, password, override = False):
10 self.url = url
11 self.account = account
12 self.password = password
13 self.session_override = override
14 self.pages = {
15 "sid": "/api-getSessionID.json", #获取sid的接口
16 "login": "/user-login.json?zentaosid={0}", #登录的接口
17 "get_product_list": "/product-index-no.json",
18 }
19 self.s = None
20 self.sid = None
21
22 def req_get(self, url):
23 #请求并返回结果
24 web = requests.get(url)
25 if web.status_code == 200:
26 resp = json.loads(web.content)
27 if resp.get("status") == "success":
28 return True, resp
29 else:
30 return False, resp
31
32 def req_post(self, url, body):
33 #请求并返回结果
34 res = requests.post(url=url, data=body)
35 if res.status_code == 200:
36 resp = json.loads(res.content)
37 if resp.get("status") == "success":
38 return True, resp
39 else:
40 return False, resp
41
42 def login(self):
43 if self.s is None:
44 if not self.session_override and zentao_cli.session is not None:
45 self.s = zentao_cli.session
46 self.sid = zentao_cli.sid
47 else:
48 #新建会话
49 self.s = requests.session()
50 res, resp = self.req_get(self.url.rstrip("/") + self.pages["sid"])
51 if res:
52 print("获取sessionID成功")
53 self.sid = json.loads(resp["data"])["sessionID"]
54 zentao_cli.sid = self.sid
55 body = {'account': self.account, 'password': self.password, 'keepLogin[]': 'on', 'referer':self.url.rstrip("/") + '/my/'}
56 login_res, login_resp = self.req_post(self.url.rstrip("/") + self.pages["login"].format(self.sid), body)
57 if login_res:
58 print("登录成功")
59 zentao_cli.session = self.s
60
61
62 def get_product_list(self):
63 req_url = self.url.rstrip("/") + self.pages["get_product_list"]
64 res, resp = self.req_get(req_url + "?zentaosid=" + self.sid)
65 if res:
66 data = resp['data']
67 products = json.loads(data)['products']
68 return products.keys(), products.values()
69
70
71 if __name__ == "__main__":
72 cli = zentao_cli("http://xxx/zentao", "xxx", "xxx")
73 cli.login()
74 print(cli.get_product_list())
以下是我收集到的比较好的学习教程资源,虽然不是什么很值钱的东西,如果你刚好需要,可以评论区,留言【777】直接拿走就好了
各位想获取资料的朋友请点赞 + 评论 + 收藏,三连!
三连之后我会在评论区挨个私信发给你们~