Arilnilhaq12

Untitled

Nov 2nd, 2025
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. import requests
  2. import json
  3. import textwrap
  4.  
  5. def analyze_network_request(url_awal: str):
  6. """
  7. Melakukan request HTTP ke URL, melacak seluruh rantai redirect (history),
  8. dan menampilkan detail Request/Response Headers serta Payload (JSON/Text)
  9. untuk setiap langkah, menyerupai Developer Tools.
  10. """
  11.  
  12. print("=========================================================================")
  13. print(f"šŸš€ Memulai Analisis Jaringan GET ke: {url_awal}")
  14. print("=========================================================================")
  15.  
  16. try:
  17. # Lakukan request, biarkan requests mengikuti redirect secara otomatis
  18. response = requests.get(url_awal, timeout=15)
  19.  
  20. # Gabungkan objek riwayat dengan objek respons akhir untuk iterasi penuh
  21. all_responses = response.history + [response]
  22.  
  23. for i, resp in enumerate(all_responses):
  24. is_final = (i == len(all_responses) - 1)
  25.  
  26. # --- Judul Langkah ---
  27. if is_final:
  28. print("\n\n#########################################################################")
  29. print(f"šŸ RESPON AKHIR (Final URL)")
  30. print("#########################################################################")
  31. else:
  32. print("\n-------------------------------------------------------------------------")
  33. print(f"āž”ļø LANGKAH {i+1} - REDIRECT ({resp.status_code})")
  34. print("-------------------------------------------------------------------------")
  35.  
  36. # 1. TAMPILAN REQUEST HEADERS
  37. print(f"1. REQUEST HEADERS ({resp.request.method}):")
  38. print(f" URL: {resp.url}")
  39. for header, value in resp.request.headers.items():
  40. print(f" > {header}: {value}")
  41.  
  42. # Request Body (Payload) - Biasanya N/A untuk GET
  43. request_body = resp.request.body if resp.request.body else 'N/A (No Body)'
  44. if request_body != 'N/A (No Body)':
  45. print(f" Body: {request_body}")
  46.  
  47.  
  48. # 2. TAMPILAN RESPONSE HEADERS
  49. print(f"\n2. RESPONSE HEADERS (Status: {resp.status_code}):")
  50. for header, value in resp.headers.items():
  51. if not is_final and header.lower() == 'location':
  52. # Highlight Location header untuk langkah redirect
  53. print(f" >> **{header}: {value}**")
  54. else:
  55. print(f" > {header}: {value}")
  56.  
  57. # 3. TAMPILAN RESPONSE PAYLOAD (Hanya di langkah akhir)
  58. if is_final:
  59. print("\n3. RESPONSE PAYLOAD (Body):")
  60.  
  61. # Cek Content-Type untuk petunjuk
  62. content_type = resp.headers.get('Content-Type', '').lower()
  63.  
  64. try:
  65. # šŸ’„ Prioritas #1: Coba parsing sebagai JSON (untuk Ajax)
  66. data_json = resp.json()
  67.  
  68. # Konfirmasi Tipe
  69. if 'application/json' in content_type:
  70. print(" [TIPE: JSON (Ajax) - Confirmed by Header]")
  71. else:
  72. print(" [TIPE: JSON (Ajax) - Parsing Berhasil]")
  73.  
  74. # Format JSON yang rapi
  75. formatted_json = json.dumps(data_json, indent=2)
  76. # Tampilkan hingga 1000 karakter pertama
  77. print(textwrap.indent(formatted_json[:1000] + ('...' if len(formatted_json) > 1000 else ''), ' '))
  78.  
  79. except (json.JSONDecodeError, UnicodeDecodeError):
  80. # šŸ’„ Prioritas #2: Jika gagal, tampilkan sebagai Teks/HTML
  81. print(" [TIPE: Text/HTML/Other - Gagal Parsing JSON]")
  82. text_snippet = resp.text
  83. # Tampilkan hingga 500 karakter pertama
  84. print(textwrap.indent(text_snippet[:500] + ('...' if len(text_snippet) > 500 else ''), ' '))
  85.  
  86.  
  87. except requests.exceptions.RequestException as e:
  88. print(f"\nāŒ Terjadi kesalahan saat melakukan request: {e}")
  89. finally:
  90. print("\n=========================================================================")
  91.  
  92. # --- CARA MENGGUNAKAN (WORKING EXAMPLE) ---
  93. # Gunakan URL API (JSON) untuk melihat Payload JSON
  94. # url_to_test = "https://jsonplaceholder.typicode.com/todos/1"
  95.  
  96. # Gunakan URL web (HTML + Redirect Chain) untuk melihat Redirect History
  97. url_to_test = "http://www.cisco.com"
  98.  
  99. # Jalankan fungsi
  100. analyze_network_request(url_to_test)
Advertisement
Add Comment
Please, Sign In to add comment