Guest User

Untitled

a guest
Jul 25th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. def auth():
  2. global url # url = 'http://path_to_server'
  3. global aParams # aParams = {'login': 'user', 'pass': 'pass'}
  4. with open('token.txt', 'r') as data:
  5. load = json.load(data)
  6. loaded = load['tstmp']
  7. print('__', loaded)
  8. if int(datetime.datetime.now().strftime('%s')) - int(loaded) >= 900:
  9. print('old token:', load['key'])
  10. return load['key']
  11. else:
  12. with open('token.txt', 'w') as data:
  13. get_token = requests.get(url + '/api/auth', params=aParams).text
  14. timestamp = int(datetime.datetime.now().strftime('%s'))
  15. load = json.dump({'key': get_token, 'tstmp': timestamp}, data)
  16. print('new token', load['key'])
  17. return load['key']
  18.  
  19. "/Volumes/My Files/Python/slack/slackenv/bin/python" "/Volumes/My Files/Python/slack/slackenv/lib/python3.5/site-packages/getreport.py"
  20. __ 1469428422
  21. Traceback (most recent call last):
  22. old token: 243c2e38-f329-aeda-4d03-4a631f247415
  23. File "/Volumes/My Files/Python/slack/slackenv/lib/python3.5/site-packages/getreport.py", line 85, in <module>
  24. <function auth at 0x101a7ad08>
  25. get_report(t=auth())
  26. __ 1469428422
  27. File "/Volumes/My Files/Python/slack/slackenv/lib/python3.5/site-packages/getreport.py", line 77, in get_report
  28. old token: 243c2e38-f329-aeda-4d03-4a631f247415
  29. r = requests.get(reportUrl, cookies=str(token)).json()
  30. File "/Volumes/My Files/Python/slack/slackenv/lib/python3.5/site-packages/requests/api.py", line 71, in get
  31. return request('get', url, params=params, **kwargs)
  32. File "/Volumes/My Files/Python/slack/slackenv/lib/python3.5/site-packages/requests/api.py", line 57, in request
  33. return session.request(method=method, url=url, **kwargs)
  34. File "/Volumes/My Files/Python/slack/slackenv/lib/python3.5/site-packages/requests/sessions.py", line 461, in request
  35. prep = self.prepare_request(req)
  36. File "/Volumes/My Files/Python/slack/slackenv/lib/python3.5/site-packages/requests/sessions.py", line 371, in prepare_request
  37. cookies = cookiejar_from_dict(cookies)
  38. File "/Volumes/My Files/Python/slack/slackenv/lib/python3.5/site-packages/requests/cookies.py", line 469, in cookiejar_from_dict
  39. cookiejar.set_cookie(create_cookie(name, cookie_dict[name]))
  40. TypeError: string indices must be integers
  41.  
  42. def get_report(t):
  43. # по логике функция получает токен, подставляет его в куки и получает отчет в json.
  44. global url
  45. global reportUrl
  46. global auth
  47. token = t
  48. # if df is None:
  49. # df = datetime.date.strftime(datetime.date.today(), '%Y-%m-%dT00:00:00')
  50. # if dt is None:
  51. # dt = datetime.date.strftime(datetime.date.today(), '%Y-%m-%dT23:59:59')
  52. r = requests.get(reportUrl, cookies=str(token)).json()
  53. report = json.dump(r, ensure_ascii=False, indent=4, sort_keys=True)
  54. requests.get(url + 'api/logout?key=' + str(token))
  55. print('logged out')
  56. print(report)
  57. return report
  58.  
  59.  
  60. get_report(t=auth())
  61.  
  62. url = 'http://httpbin.org/cookies'
  63. cookies = dict(cookies_are='working')
  64. r = requests.get(url, cookies=cookies)
Add Comment
Please, Sign In to add comment