-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.py
More file actions
executable file
·50 lines (42 loc) · 1.23 KB
/
client.py
File metadata and controls
executable file
·50 lines (42 loc) · 1.23 KB
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
49
50
#!/usr/bin/env python2
from __future__ import print_function
import requests
import logging
import sys
def debug_mode():
try: # for Python 3
from http.client import HTTPConnection
except ImportError:
from httplib import HTTPConnection
HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
# debug_mode()
if len(sys.argv) > 1:
url = sys.argv[1]
else:
url = "http://localhost:5000/"
# first request
first_hit = requests.get(url)
first_json = first_hit.json()
token = first_json.pop('token')
title, next_url = first_json.popitem()
print("My ID is {}".format(token))
# subsequent requests
done = False
link_titles = [title]
while not done:
print("Accessing {}".format(next_url))
hit = requests.post(next_url, json={'token': token})
response = hit.json()
if 'answer' in response:
print(response)
print('Link titles: {}'.format(', '.join(link_titles)))
done = True
else:
token = response.pop('token')
title, next_url = response.popitem()
link_titles.append(title)