-
Notifications
You must be signed in to change notification settings - Fork 0
/
Requests_cloud_session.py
48 lines (35 loc) · 1.64 KB
/
Requests_cloud_session.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# get data from cloud and save
import json
import requests
session = requests.Session()
servertoken="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
cloudHeaders={"Content-Type": "application/json","Authorization": "Bearer %s" %servertoken}
url="https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.com"
r = session.get(url, headers=cloudHeaders, timeout=(2,5))
file_path= file_path # file_path= fileFolder_save + file_name + '.json' # save json file
with open(file_path, "w+") as f:
json.dump(json.loads(r.text), f)
# streaming data
try:
print('start connecting!!!')
with self.stream_session.get(url, headers=None, stream=True, timeout=5) as resp:
for line in resp.iter_lines():
if line:
ts = int(json.loads(line)['result']['timestamp'])
print(f'{self.opla_job_number} Timestamp received:', ts, 'at', time.time())
if ts == self.last_timestamp:
print('******** timestamp issue !!! ********')
print(line)
if ts < self.last_timestamp:
print('!!!*********************** out of sequence ********************!!!')
print(line)
if ts > self.last_timestamp:
self.last_timestamp = ts
except requests.exceptions.ReadTimeout as errt:
print('streaming Error:', errt)
# smart connection
self.smart_connect()
except Exception as err:
print('streaming Error other than read time:', err)
time.sleep(1)
self.get_stream()