test_proxy_location.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import requests
  4. import json
  5. PYTHON_API = "http://localhost:5005"
  6. def test():
  7. print("=" * 60)
  8. print("Test Proxy IP and Location")
  9. print("=" * 60)
  10. # Test proxy
  11. print("\n[1] Getting proxy IP...")
  12. proxy_data = {
  13. "provider": "shenlong",
  14. "productKey": "o2ihwv3e",
  15. "signature": "ccff437b0c211d7a758b0926cbdcf958",
  16. "regionCode": "310000",
  17. "city": "Shanghai",
  18. "platform": "weixin"
  19. }
  20. try:
  21. resp = requests.post(f"{PYTHON_API}/proxy/test", json=proxy_data, timeout=30)
  22. result = resp.json()
  23. print(f"Response: {json.dumps(result, indent=2, ensure_ascii=False)}")
  24. if result.get("success"):
  25. proxy = result.get("proxy", "")
  26. print(f"\n[OK] Proxy: {proxy}")
  27. # Extract IP
  28. proxy_ip = proxy.replace("http://", "").split(":")[0]
  29. print(f"Proxy IP: {proxy_ip}")
  30. # Query IP location
  31. print(f"\n[2] Querying IP location...")
  32. try:
  33. ip_info = requests.get(f"http://ip-api.com/json/{proxy_ip}?lang=zh-CN", timeout=5).json()
  34. print(f"Country: {ip_info.get('country', 'N/A')}")
  35. print(f"Region: {ip_info.get('regionName', 'N/A')}")
  36. print(f"City: {ip_info.get('city', 'N/A')}")
  37. print(f"ISP: {ip_info.get('isp', 'N/A')}")
  38. city = ip_info.get('city', '')
  39. if 'shanghai' in city.lower() or 'shang hai' in city.lower() or city == '上海':
  40. print(f"\n[OK] Proxy location is Shanghai")
  41. else:
  42. print(f"\n[WARNING] Proxy location is {city}, NOT Shanghai!")
  43. print("This is why WeChat shows wrong location")
  44. except Exception as e:
  45. print(f"[ERROR] IP query failed: {e}")
  46. else:
  47. print(f"[ERROR] Proxy failed: {result.get('error', 'Unknown')}")
  48. except Exception as e:
  49. print(f"[ERROR] Request failed: {e}")
  50. print("\n" + "=" * 60)
  51. if __name__ == "__main__":
  52. test()