From d58d080cbaaa1806c42565c18ada067571304e9d Mon Sep 17 00:00:00 2001 From: RTAkland Date: Sun, 14 Nov 2021 13:10:10 +0800 Subject: [PATCH] add: added a new way to get ip and location --- tests/get_location.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/tests/get_location.py b/tests/get_location.py index 861707e..6cca818 100644 --- a/tests/get_location.py +++ b/tests/get_location.py @@ -7,10 +7,31 @@ import requests +import json + + +class Request(object): + def __init__(self): + self.url1 = 'https://ip.tool.lu' + self.url2 = 'https://acg.toubiec.cn/random.php?ret=json' + self.session = requests.Session() + self.session.trust_env = False + + def request_way_1(self): + """ + https://ip.tool.lu + :return: {'IP:': r.split(':')[1].split()[0], 'Location': r.split(':')[2].split()} + """ + r = self.session.get(self.url1, timeout=5).text + return {'IP:': r.split(':')[1].split()[0], 'Location': r.split(':')[2].split()} # return IP & Cities + + def request_way_2(self): + """ + https://acg.toubiec.cn/random.php?ret=json + :return: {'IP': data['client_ip'], 'Location': data['client_lsp']} + """ + r = self.session.get(self.url2, timeout=5).text + data = json.loads(r)[0] + return {'IP': data['client_ip'], 'Location': data['client_lsp']} -def request(): - session = requests.Session() - session.trust_env = False - r = session.get('https://ip.tool.lu/', timeout=5).text - return {'IP:': r.split(':')[1].split()[0], 'Location': r.split(':')[2].split()} # return IP & Cities