外部更新环境变量.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # File : 外部更新环境变量.py
  4. # Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
  5. # Date : 2022/12/6
  6. import requests
  7. class Drpy:
  8. def __init__(self, url, username='admin', password='drpy'):
  9. s = requests.session()
  10. data = {
  11. 'username': username,
  12. 'password': password,
  13. }
  14. print(data)
  15. login_api = f'{url.rstrip("/")}/admin/login'
  16. try:
  17. r = s.post(login_api, data=data)
  18. print(r.cookies)
  19. print(r.text)
  20. self.env_api = f'{url.rstrip("/")}/admin/update_env'
  21. self.s = s
  22. print('drpy连接成功')
  23. except:
  24. self.s = None
  25. print('drpy连接失败')
  26. def update_env(self, key, value):
  27. if not self.s:
  28. exit('drpy未连接,无法进行操作')
  29. else:
  30. data = {
  31. 'key': key,
  32. 'value': value,
  33. }
  34. r = self.s.post(self.env_api, data=data)
  35. jsonData = r.json()
  36. if jsonData.get('code') == 200:
  37. print('修改成功')
  38. print(jsonData['data'])
  39. else:
  40. print('修改失败')
  41. if __name__ == '__main__':
  42. drpy = Drpy('http://localhost:5705/')
  43. drpy.update_env('test_env', '测试环境变量')