Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- from bs4 import BeautifulSoup
- from requests import Request, Session
- import random
- def genera_numero_casuale_28_cifre():
- # Generiamo un numero intero casuale a 28 cifre
- numero_casuale = random.randint(10**27, 10**28 - 1)
- # Convertiamo l'intero in una stringa
- numero_casuale_str = str(numero_casuale)
- return numero_casuale_str
- #-----------------------------2965631837716582234261587076
- # url del filo
- url = "https://vecchiochan.com/t/res/1533.html"
- # headers della richiesta
- contentType = "-----------------------------" + genera_numero_casuale_28_cifre()
- headers = {
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0',
- 'Accept': '*/*',
- 'Accept-Language': 'it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3',
- 'Accept-Encoding': 'gzip, deflate, br, zstd',
- 'X-Requested-With': 'XMLHttpRequest',
- 'Content-Length': '2187',
- 'Origin': 'https://vecchiochan.com',
- 'Alt-Used': 'vecchiochan.com',
- 'Connection': 'keep-alive',
- 'Referer': url,
- 'Cookie': 'serv={}',
- 'Sec-Fetch-Dest': 'empty',
- 'Sec-Fetch-Mode': 'cors',
- 'Sec-Fetch-Site': 'same-origin',
- 'Priority': 'u=0',
- 'TE': 'trailers'
- }
- # ottengo la risposta dal server
- h_response = requests.get(url)
- # ne prelevo il file HTML
- data = h_response.text
- # analizzo l'HTML
- soup = BeautifulSoup(data, 'html.parser')
- # array di dizionari contenenti nome-valore
- arr:dict = {}
- # ottengo tutti il form che mi consente di postare
- form = soup.find("form", attrs={"name": "post"})
- # trovo tutti gli input interni al form compresi quelli nascosti
- inputs = form.find_all("input")
- # per ogni elemento input
- for e in inputs:
- # inizializzo il valore vuoto
- value = ""
- # se l'elelemnto ha l'attributo value
- if e.has_attr("value"):
- # imposto il valore
- value = e["value"]
- if e["name"] == "password":
- value = "ricchione"
- # creo un dizionario nome-valore
- dictx = {str(e["name"]) : str(value)}
- # lo salvo nell'array
- arr.update(dictx)
- # trovo anche tutte le textarea interne al form
- ta = form.find_all("textarea")
- # per ogni textarea trovata
- for tas in ta:
- # salvo il nome della textarea e il suo contenuto
- if tas["name"] == "body":
- # qui inserisco il corpo del post
- arr.update({str(tas["name"]): "Ciao da Python! Odio i negri, viva il papa!"})
- else:
- arr.update({str(tas["name"]): str((tas.text))})
- # aggiungo chiave mancante
- arr.update({"json_response":"1"})
- # costruisco corpo della richiesta custom affinchè il boundary abbia il giusto numero dei trattini se questi incidono
- body = ""
- body += contentType + "\r\n"
- for key, value in arr.items():
- body += "Content-Disposition: form-data; name=\"" + str(key) + "\"\r\n\r\n" + str(value) + "\r\n"
- body += contentType + "\r\n"
- body = body[:-2]
- body += "--"
- #inoltro la richiesta
- s = Session()
- req = Request('POST', url, data={}, headers=headers)
- prepped = req.prepare()
- prepped.body = body.encode('utf-8')
- prepped.headers.update({'Content-Type': 'multipart/form-data; boundary='+ contentType})
- response = s.send(prepped)
- print("Corpo richiesta")
- print(response.request.body.decode('utf-8'))
- print("Header richiesta")
- print(response.headers)
- print(response.status_code)
Advertisement
Add Comment
Please, Sign In to add comment