IPQueryObject.py 1.0 KB

123456789101112131415161718192021222324252627282930
  1. # @Author : Rocky
  2. # @File : IPQueryObject.py
  3. # @Time : 2023/8/16 8:56
  4. import requests
  5. class IPQuery:
  6. """
  7. 阿里云IP地址查询
  8. https://market.aliyun.com/products/57002003/cmapi021970.html?spm=5176.2020520132.recommend-card.dbottom-suggestion.33e17218GYjWDt#sku=yuncode15970000020
  9. """
  10. def __init__(self, ip):
  11. self.appcode = 'd7d63b34b1d54214be446608a57ff0a2'
  12. self.host = 'https://c2ba.api.huachen.cn'
  13. self.path = '/ip'
  14. self.district = None
  15. self.city = None
  16. param = 'ip=' + ip
  17. url = self.host + self.path + '?' + param
  18. # 获取ip的区级和城市信息
  19. headers = {'Authorization': 'APPCODE ' + self.appcode}
  20. res = requests.get(url=url, headers=headers)
  21. if res.status_code == 200:
  22. # 反序列化响应数据
  23. res_data = eval(res.content.decode('utf-8'))
  24. if res_data['ret'] == 200:
  25. self.district, self.city = res_data['data']['district'], res_data['data']['city']