IPQueryObject.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # @Author : Rocky
  2. # @File : IPQueryObject.py
  3. # @Time : 2023/8/16 8:56
  4. import ssl
  5. import urllib
  6. import requests
  7. class IPQuery:
  8. """
  9. 阿里云IP地址查询
  10. https://market.aliyun.com/products/57002003/cmapi021970.html?spm=5176.2020520132.recommend-card.dbottom-suggestion.33e17218GYjWDt#sku=yuncode15970000020
  11. """
  12. def __init__(self, ip):
  13. self.appcode = 'd7d63b34b1d54214be446608a57ff0a2'
  14. self.host = 'https://c2ba.api.huachen.cn'
  15. self.path = '/ip'
  16. self.district = None
  17. self.city = None
  18. param = 'ip=' + ip
  19. url = self.host + self.path + '?' + param
  20. # 获取ip的区级和城市信息
  21. headers = {'Authorization': 'APPCODE ' + self.appcode}
  22. res = requests.get(url=url, headers=headers)
  23. if res.status_code == 200:
  24. # 反序列化响应数据
  25. res_data = eval(res.content.decode('utf-8'))
  26. if res_data['ret'] == 200:
  27. self.district, self.city = res_data['data']['district'], res_data['data']['city']
  28. class WeatherInfo:
  29. def __init__(self, city_id):
  30. self.appcode = 'd7d63b34b1d54214be446608a57ff0a2'
  31. self.headers = {'Authorization': 'APPCODE ' + self.appcode,
  32. 'Content-Type': 'application/x-www-form-urlencoded'}
  33. self.city_id = city_id
  34. def get_city_weather(self):
  35. url = "https://aliv18.data.moji.com/whapi/json/alicityweather/condition" # 获取实时天气
  36. data = {'cityId': self.city_id}
  37. response = requests.post(url, headers=self.headers, data=data, verify=False)
  38. if response.status_code == 200:
  39. result = response.json()
  40. if result['code'] == 0:
  41. # 返回温湿度
  42. return result['data']['condition']['temp'], result['data']['condition']['humidity']
  43. return False