Skip to content

Commit 787d1a2

Browse files
committed
add cloudflareDDNS and getYourIP
1 parent 7300caf commit 787d1a2

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed

extendingPython/__init__.py

Whitespace-only changes.

extendingPython/cloudflareDDNS/__init__.py

Whitespace-only changes.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import json
2+
3+
import pyhttpx
4+
import requests
5+
6+
7+
def getCloudflareDomainNameContent(Token, DomainID, RecordID):
8+
"""
9+
:param Token:
10+
:param DomainID:
11+
:param RecordID:
12+
:return: result from Cloudflare dns_records
13+
"""
14+
headers = {
15+
"Authorization": "Bearer " + Token,
16+
"Content-Type": "application/json"
17+
}
18+
session = pyhttpx.HttpSession()
19+
res = session.get(
20+
url='https://api.cloudflare.com/client/v4/zones/' + DomainID + '/dns_records/' + RecordID,
21+
headers=headers)
22+
res = json.loads(res.text)
23+
return res
24+
25+
26+
def verifyCloudflareToken(Token: str):
27+
"""
28+
:param Token:
29+
:return: True or False
30+
"""
31+
headers = {
32+
"Authorization": "Bearer " + Token,
33+
"Content-Type": "application/json"
34+
}
35+
session = pyhttpx.HttpSession()
36+
res = session.get(url='https://api.cloudflare.com/client/v4/user/tokens/verify', headers=headers)
37+
res = json.loads(res.text)
38+
if res["result"]["status"] == "active":
39+
return True
40+
else:
41+
return False
42+
43+
44+
def updateCloudflareContent(Token: str, DDNSType: str, DDNSHostName: str, DDNSContent: str, DDNS_TTL: int,
45+
isProxied: bool, DomainID: str,
46+
RecordID: str):
47+
"""
48+
:param Token: str
49+
:param DDNSType: str
50+
:param DDNSHostName: str
51+
:param DDNSContent: str
52+
:param DDNS_TTL: int
53+
:param isProxied: bool
54+
:param DomainID: str
55+
:param RecordID: str
56+
:return: status_code
57+
"""
58+
headers = {
59+
"Authorization": "Bearer " + Token,
60+
"Content-Type": "application/json"
61+
}
62+
data = {
63+
"type": DDNSType,
64+
"name": DDNSHostName,
65+
"content": DDNSContent,
66+
"ttl": DDNS_TTL,
67+
"proxied": isProxied
68+
}
69+
res = requests.put(
70+
url='https://api.cloudflare.com/client/v4/zones/' + DomainID + '/dns_records/' + RecordID,
71+
headers=headers, json=data)
72+
return res.status_code
73+
74+
75+
def compareDDNSContent(NowContent: str, LastContent: str):
76+
"""
77+
:param NowContent:
78+
:param LastContent: you can use getCloudflareDomainNameContent()["result"]["content"]
79+
:return: True or False
80+
"""
81+
if NowContent == LastContent:
82+
return True
83+
else:
84+
return False

extendingPython/getYourIP/__init__.py

Whitespace-only changes.

extendingPython/getYourIP/getIP.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pyhttpx
2+
3+
4+
def getYourPublicIPv4():
5+
headers = {
6+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36",
7+
}
8+
session = pyhttpx.HttpSession()
9+
res = session.get(url='https://ipv4.icanhazip.com', headers=headers)
10+
return res.text

0 commit comments

Comments
 (0)