check_ip_origin.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # -*- coding: utf-8 -*-
  2. import aiohttp
  3. import asyncio
  4. import json
  5. target_ip = '175.99.17.215' # 目标IP地址
  6. async def fetch(session, url, headers, payload):
  7. # 发送POST请求
  8. async with session.post(url, headers=headers, data=payload) as response:
  9. response_text = await response.text()
  10. print(response_text)
  11. # 解析JSON数据
  12. data = json.loads(response_text)
  13. # 打印获取到的数据
  14. print(data)
  15. # 将数据中的中文编码转换为中文字符
  16. text_data = data.get('text', {})
  17. for key, value in text_data.items():
  18. if isinstance(value, str):
  19. text_data[key] = value.encode('latin1').decode('utf-8')
  20. data['text'] = text_data
  21. return data
  22. async def main():
  23. # 爬虫目标URL
  24. url = 'https://tool.lu/ip/ajax.html'
  25. # 请求头
  26. headers = {
  27. 'accept': 'application/json, text/javascript, */*; q=0.01',
  28. 'accept-encoding': 'gzip, deflate, br, zstd',
  29. 'accept-language': 'zh-CN,zh;q=0.6',
  30. 'content-length': '16',
  31. 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
  32. 'origin': 'https://tool.lu',
  33. 'priority': 'u=1, i',
  34. 'referer': 'https://tool.lu/ip/',
  35. 'sec-ch-ua': '"Brave";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
  36. 'sec-ch-ua-mobile': '?0',
  37. 'sec-ch-ua-platform': '"macOS"',
  38. 'sec-fetch-dest': 'empty',
  39. 'sec-fetch-mode': 'cors',
  40. 'sec-fetch-site': 'same-origin',
  41. 'sec-gpc': '1',
  42. 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
  43. 'x-requested-with': 'XMLHttpRequest'
  44. }
  45. # 要发送的数据
  46. payload = f'{target_ip}'
  47. # 创建aiohttp会话
  48. async with aiohttp.ClientSession() as session:
  49. # 调用fetch函数
  50. result = await fetch(session, url, headers, payload)
  51. # 打印结果
  52. print(json.dumps(result, ensure_ascii=False, indent=2))
  53. # 运行异步主函数
  54. loop = asyncio.get_event_loop()
  55. loop.run_until_complete(main())