MonaChanDev

Codice richiesta post

Dec 5th, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 KB | Source Code | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3. from requests import Request, Session
  4. import random
  5.  
  6. def genera_numero_casuale_28_cifre():
  7.     # Generiamo un numero intero casuale a 28 cifre
  8.     numero_casuale = random.randint(10**27, 10**28 - 1)
  9.  
  10.     # Convertiamo l'intero in una stringa
  11.     numero_casuale_str = str(numero_casuale)
  12.  
  13.     return numero_casuale_str
  14. #-----------------------------2965631837716582234261587076
  15. # url del filo
  16. url = "https://vecchiochan.com/t/res/1533.html"
  17. # headers della richiesta
  18. contentType = "-----------------------------" + genera_numero_casuale_28_cifre()
  19. headers = {
  20.     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0',
  21.     'Accept': '*/*',
  22.     'Accept-Language': 'it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3',
  23.     'Accept-Encoding': 'gzip, deflate, br, zstd',
  24.     'X-Requested-With': 'XMLHttpRequest',
  25.     'Content-Length': '2187',
  26.     'Origin': 'https://vecchiochan.com',
  27.     'Alt-Used': 'vecchiochan.com',
  28.     'Connection': 'keep-alive',
  29.     'Referer': url,
  30.     'Cookie': 'serv={}',
  31.     'Sec-Fetch-Dest': 'empty',
  32.     'Sec-Fetch-Mode': 'cors',
  33.     'Sec-Fetch-Site': 'same-origin',
  34.     'Priority': 'u=0',
  35.     'TE': 'trailers'
  36. }
  37.  
  38.  
  39. # ottengo la risposta dal server
  40. h_response = requests.get(url)
  41. # ne prelevo il  file HTML
  42. data = h_response.text
  43. # analizzo l'HTML
  44. soup = BeautifulSoup(data, 'html.parser')
  45. # array di dizionari contenenti nome-valore
  46. arr:dict = {}
  47. # ottengo tutti il form che mi consente di postare
  48. form = soup.find("form", attrs={"name": "post"})
  49. # trovo tutti gli input interni al form  compresi quelli nascosti
  50. inputs = form.find_all("input")
  51. # per ogni elemento input
  52. for e in inputs:
  53.     # inizializzo il valore vuoto
  54.     value = ""
  55.     # se l'elelemnto ha l'attributo value
  56.     if e.has_attr("value"):
  57.         # imposto il valore
  58.         value = e["value"]
  59.         if e["name"] == "password":
  60.             value = "ricchione"
  61.         # creo un dizionario nome-valore
  62.         dictx = {str(e["name"]) : str(value)}
  63.         # lo salvo nell'array
  64.         arr.update(dictx)
  65. # trovo anche tutte le textarea interne al form
  66. ta = form.find_all("textarea")
  67. # per ogni textarea trovata
  68. for tas in ta:
  69.     # salvo il nome della textarea e il suo contenuto
  70.     if tas["name"] == "body":
  71.         # qui inserisco il corpo del post
  72.        arr.update({str(tas["name"]): "Ciao da Python! Odio i negri, viva il papa!"})
  73.     else:
  74.         arr.update({str(tas["name"]): str((tas.text))})
  75. # aggiungo chiave mancante
  76. arr.update({"json_response":"1"})
  77. # costruisco corpo della richiesta custom affinchè il boundary abbia il giusto numero dei trattini se questi incidono
  78. body = ""
  79. body += contentType + "\r\n"
  80. for key, value in arr.items():
  81.     body += "Content-Disposition: form-data; name=\"" + str(key) + "\"\r\n\r\n" + str(value) + "\r\n"
  82.     body += contentType + "\r\n"
  83. body = body[:-2]
  84. body += "--"
  85. #inoltro la richiesta
  86. s = Session()
  87. req = Request('POST', url, data={}, headers=headers)
  88. prepped = req.prepare()
  89. prepped.body = body.encode('utf-8')
  90. prepped.headers.update({'Content-Type': 'multipart/form-data; boundary='+ contentType})
  91. response = s.send(prepped)
  92. print("Corpo richiesta")
  93.  
  94. print(response.request.body.decode('utf-8'))    
  95. print("Header richiesta")
  96.  
  97. print(response.headers)    
  98. print(response.status_code)
Tags: python
Advertisement
Add Comment
Please, Sign In to add comment