Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit 423120c

Browse files
committed
Python 3.x porting
1 parent adf1e5b commit 423120c

11 files changed

Lines changed: 775 additions & 639 deletions

examples/example_are_zones_ipv6.py

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,64 @@
11
#!/usr/bin/env python
2+
"""CloudFlare API code - example"""
23

34
import os
45
import sys
6+
57
sys.path.insert(0, os.path.abspath('..'))
68
import CloudFlare
79

810
def main():
11+
"""CloudFlare API code - example"""
12+
13+
# Check for update flag
14+
update_ipv6 = False
15+
try:
16+
if sys.argv[1] == '--update':
17+
update_ipv6 = True
18+
sys.argv.pop(1)
19+
except IndexError:
20+
pass
21+
22+
# Grab the first argument, if there is one
23+
try:
24+
zone_name = sys.argv[1]
25+
params = {'name':zone_name, 'per_page':1}
26+
except IndexError:
27+
params = {'per_page':50}
28+
29+
cf = CloudFlare.CloudFlare()
30+
31+
# grab the zone identifier
32+
try:
33+
zones = cf.zones.get(params=params)
34+
except CloudFlare.CloudFlareAPIError as e:
35+
exit('/zones.get %d %s - api call failed' % (e, e))
36+
except Exception as e:
37+
exit('/zones - %s - api call failed' % (e))
38+
39+
for zone in sorted(zones, key=lambda v: v['name']):
40+
zone_name = zone['name']
41+
zone_id = zone['id']
42+
try:
43+
ipv6 = cf.zones.settings.ipv6.get(zone_id)
44+
except CloudFlare.CloudFlareAPIError as e:
45+
exit('/zones.settings.ipv6.get %d %s - api call failed' % (e, e))
46+
47+
ipv6_value = ipv6['value']
48+
if update_ipv6 and ipv6_value == 'off':
49+
print zone_id, ipv6_value, zone_name, '(now updating... off -> on)'
50+
try:
51+
ipv6 = cf.zones.settings.ipv6.patch(zone_id, data={'value':'on'})
52+
except CloudFlare.CloudFlareAPIError as e:
53+
exit('/zones.settings.ipv6.patch %d %s - api call failed' % (e, e))
54+
ipv6_value = ipv6['value']
55+
if ipv6_value == 'on':
56+
print '\t', '... updated!'
57+
else:
58+
print zone_id, ipv6_value, zone_name
959

10-
# Check for update flag
11-
update_ipv6 = False
12-
try:
13-
if sys.argv[1] == '--update':
14-
update_ipv6 = True
15-
sys.argv.pop(1)
16-
except:
17-
pass
18-
19-
# Grab the first argument, if there is one
20-
try:
21-
zone_name = sys.argv[1]
22-
params = {'name':zone_name,'per_page':1}
23-
except:
24-
params = {'per_page':50}
25-
26-
cf = CloudFlare.CloudFlare()
27-
28-
# grab the zone identifier
29-
try:
30-
zones = cf.zones.get(params=params)
31-
except CloudFlare.CloudFlareAPIError as e:
32-
exit('/zones.get %d %s - api call failed' % (e, e))
33-
except Exception as e:
34-
exit('/zones - %s - api call failed' % (e))
35-
36-
for zone in sorted(zones, key=lambda v: v['name']):
37-
zone_name = zone['name']
38-
zone_id = zone['id']
39-
try:
40-
ipv6 = cf.zones.settings.ipv6.get(zone_id)
41-
except CloudFlare.CloudFlareAPIError as e:
42-
exit('/zones.settings.ipv6.get %d %s - api call failed' % (e, e))
43-
44-
ipv6_value = ipv6['value']
45-
if update_ipv6 and ipv6_value == 'off':
46-
print zone_id, ipv6_value, zone_name, '(now updating... off -> on)'
47-
try:
48-
ipv6 = cf.zones.settings.ipv6.patch(zone_id, data={'value':'on'})
49-
except CloudFlare.CloudFlareAPIError as e:
50-
exit('/zones.settings.ipv6.patch %d %s - api call failed' % (e, e))
51-
ipv6_value = ipv6['value']
52-
if ipv6_value == 'on':
53-
print '\t', '... updated!'
54-
else:
55-
print zone_id, ipv6_value, zone_name
56-
57-
exit(0)
60+
exit(0)
5861

5962
if __name__ == '__main__':
60-
main()
63+
main()
6164

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
#!/usr/bin/env python
2+
"""CloudFlare API code - example"""
23

34
import os
45
import sys
6+
57
sys.path.insert(0, os.path.abspath('..'))
68
import CloudFlare
79

810
def main():
9-
cf = CloudFlare.CloudFlare()
10-
zones = cf.zones.get(params={'per_page':50})
11-
for zone in zones:
12-
zone_name = zone['name']
13-
zone_id = zone['id']
14-
settings_ipv6 = cf.zones.settings.ipv6.get(zone_id)
15-
ipv6_on = settings_ipv6['value']
16-
print zone_id, ipv6_on, zone_name
17-
exit(0)
11+
"""CloudFlare API code - example"""
12+
13+
cf = CloudFlare.CloudFlare()
14+
zones = cf.zones.get(params={'per_page':50})
15+
for zone in zones:
16+
zone_name = zone['name']
17+
zone_id = zone['id']
18+
settings_ipv6 = cf.zones.settings.ipv6.get(zone_id)
19+
ipv6_on = settings_ipv6['value']
20+
print zone_id, ipv6_on, zone_name
21+
exit(0)
1822

1923
if __name__ == '__main__':
20-
main()
24+
main()
2125

examples/example_certificates.py

Lines changed: 71 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,82 @@
11
#!/usr/bin/env python
2+
"""CloudFlare API code - example"""
23

34
import os
45
import sys
6+
import json
7+
58
sys.path.insert(0, os.path.abspath('..'))
69
import CloudFlare
710

8-
import json
9-
1011
def main():
11-
# Grab the first argument, if there is one
12-
try:
13-
zone_name = sys.argv[1]
14-
params = {'name':zone_name,'per_page':1}
15-
except:
16-
params = {'per_page':50}
17-
18-
cf = CloudFlare.CloudFlare()
19-
20-
# grab the zone identifier
21-
try:
22-
zones = cf.zones.get(params=params)
23-
except CloudFlare.CloudFlareAPIError as e:
24-
exit('/zones %d %s - api call failed' % (e, e))
25-
except Exception as e:
26-
exit('/zones - %s - api call failed' % (e))
27-
28-
# there should only be one zone
29-
for zone in sorted(zones, key=lambda v: v['name']):
30-
zone_name = zone['name']
31-
zone_id = zone['id']
32-
try:
33-
certificates = cf.zones.ssl.certificate_packs.get(zone_id)
34-
except CloudFlare.CloudFlareAPIError as e:
35-
exit('/zones.ssl.certificate_packs %d %s - api call failed' % (e, e))
36-
37-
for certificate in certificates:
38-
certificate_type = certificate['type']
39-
primary_certificate = certificate['primary_certificate']
40-
certificate_hosts = certificate['hosts']
41-
certificate_sig = certificate['certificates'][0]['signature']
42-
certificate_sig_count = len(certificate['certificates'])
43-
if certificate_sig_count > 1:
44-
c = certificate['certificates'][0]
45-
print '%-40s %-10s %-32s %-15s [ %s ]' %(zone_name, certificate_type, primary_certificate, c['signature'], ','.join(certificate_hosts))
46-
nn = 0
47-
for c in certificate['certificates']:
48-
nn += 1
49-
if nn == 1:
50-
next
51-
print '%-40s %-10s %-32s %2d:%-15s [ %s ]' %('', '', '', nn, c['signature'], '')
52-
else:
53-
for c in certificate['certificates']:
54-
print '%-40s %-10s %-32s %-15s [ %s ]' %(zone_name, certificate_type, primary_certificate, c['signature'], ','.join(certificate_hosts))
55-
56-
exit(0)
12+
"""CloudFlare API code - example"""
13+
14+
# Grab the first argument, if there is one
15+
try:
16+
zone_name = sys.argv[1]
17+
params = {'name':zone_name, 'per_page':1}
18+
except IndexError:
19+
params = {'per_page':50}
20+
21+
cf = CloudFlare.CloudFlare()
22+
23+
# grab the zone identifier
24+
try:
25+
zones = cf.zones.get(params=params)
26+
except CloudFlare.CloudFlareAPIError as e:
27+
exit('/zones %d %s - api call failed' % (e, e))
28+
except Exception as e:
29+
exit('/zones - %s - api call failed' % (e))
30+
31+
# there should only be one zone
32+
for zone in sorted(zones, key=lambda v: v['name']):
33+
zone_name = zone['name']
34+
zone_id = zone['id']
35+
try:
36+
certificates = cf.zones.ssl.certificate_packs.get(zone_id)
37+
except CloudFlare.CloudFlareAPIError as e:
38+
exit('/zones.ssl.certificate_packs %d %s - api call failed' % (e, e))
39+
40+
for certificate in certificates:
41+
certificate_type = certificate['type']
42+
primary_certificate = certificate['primary_certificate']
43+
certificate_hosts = certificate['hosts']
44+
certificate_sig = certificate['certificates'][0]['signature']
45+
certificate_sig_count = len(certificate['certificates'])
46+
if certificate_sig_count > 1:
47+
c = certificate['certificates'][0]
48+
print '%-40s %-10s %-32s %-15s [ %s ]' % (
49+
zone_name,
50+
certificate_type,
51+
primary_certificate,
52+
c['signature'],
53+
','.join(certificate_hosts)
54+
)
55+
nn = 0
56+
for c in certificate['certificates']:
57+
nn += 1
58+
if nn == 1:
59+
next
60+
print '%-40s %-10s %-32s %2d:%-15s [ %s ]' % (
61+
'',
62+
'',
63+
'',
64+
nn,
65+
c['signature'],
66+
''
67+
)
68+
else:
69+
for c in certificate['certificates']:
70+
print '%-40s %-10s %-32s %-15s [ %s ]' % (
71+
zone_name,
72+
certificate_type,
73+
primary_certificate,
74+
c['signature'],
75+
','.join(certificate_hosts)
76+
)
77+
78+
exit(0)
5779

5880
if __name__ == '__main__':
59-
main()
81+
main()
6082

0 commit comments

Comments
 (0)