Click button with Python and requests - python

I need their help with a requests call for click a button with python and request.
The HTML code the form is this:
<form action="someurl/changedata" enctype="multipart/form-data" method="POST">
<p class="message">
Description
</p>
<div class="data-field-grouping" data-field-grouping-name="Passwords">
<div class="data-field" data-field-name="OldPassword">
<label data-required="data-required" for="OldPassword">Old Password</label><input id="OldPassword" maxLength="128" name="OldPassword" required="required" type="password" /></div>
<div class="data-field" data-field-name="Password">
<label data-required="data-required" for="Password">New Password</label><input id="Password" maxLength="128" name="Password" required="required" type="password" /></div>
<div class="data-field" data-field-name="PasswordConfirmation">
<label data-required="data-required" for="PasswordConfirmation">Confirm Password</label><input id="PasswordConfirmation" maxLength="128" name="PasswordConfirmation" required="required" type="password" /></div>
</div>
<input name="__RequestVerificationToken" type="hidden" value="xwvDA9Y-bzAY4Z9F3UVSnuFlYEuVfD2F8kYY4aD__wKzjQct7y6JZ4Jd6_YpxhQIXq0zcRJ-RfLxleHgT49P-lMecIB55LEyXCMylaTxK1KzI_HqbWM101FGmaK33Y2z0" /><button type="submit">Change Password</button></form>
I’ve been trying with this code and the login works successfully but the next part not, well. It doesn’t gives me error, but just gives me the html of the body.
import requests
from requests_ntlm import HttpNtlmAuth
info = {'OldPassword':'firstpass',
'Password':newpass,
'PasswordConfirmation': newpass,
'__RequestVerificationToken':'0'
}
s = requests.session()
with requests.session() as s:
responsee = s.get("url",auth=HttpNtlmAuth('username', 'firstpass'))
print (responsee.text)
if responsee.status_code == 200:
print("Login successfully")
response = s.post("url2",params=info)
print(response.text)

Related

Fastapi Upload a Image

i try to upload a image to a fastapi from a Cordova Webapp and get the following error:
{"detail":[{"loc":["body","terms"],"msg":"field required","type":"value_error.missing"},{"loc":["body","privacy"],"msg":"field required","type":"value_error.missing"}]}
INFO: 192.168.1.129:51915 - "POST /upload/ HTTP/1.1" 422 Unprocessable Entity
My FastApi Code is:
#app.post("/upload/", dependencies=[Depends(valid_content_length)])
async def create_upload_file(file: bytes = File(...), terms: str = Form(...), privacy: str = Form(...)):
allowedFiles = {"image/jpeg", "image/png", "image/gif", "image/tiff", "image/bmp", "video/webm"}
if file.content_type in allowedFiles:
filename = str(uuid.uuid4())
with open("uploaded_images" + filename + file.filename, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
return {"filename": file.filename}
else:
return "miau"
Client Code:
<form method="post" action="http://192.168.1.129:8080/upload">
<div class="form_row">
<label for="myfile">Choose your image:</label>
<input type="file" id="myfile" name="file">
</div>
<div class="form_row">
<label>Accept:</label>
<label class="label-checkbox item-content">
<input type="checkbox" name="my-checkbox" value="privacy" required>
<div class="item-media">
<i class="icon icon-form-checkbox"></i>
</div>
<div class="item-inner">
<div class="item-title"><a src="" target="_blank">Privacy Policy</a></div>
</div>
</label>
<label class="label-checkbox item-content">
<input type="checkbox" name="my-checkbox" value="terms" required>
<div class="item-media">
<i class="icon icon-form-checkbox"></i>
</div>
<div class="item-inner">
<div class="item-title">Terms of Use</div>
</div>
</label>
<label class="label-checkbox item-content">
<input type="submit" name="submit" class="form_submit" id="submit" value="Send"/>
</label>
</div>
</form>
How to solve the problem? According to error the body is empty, but I don't know what the problem is.
Thanks :)
The error says pretty much everything
"loc":["body","terms"],"msg":"field required"
and
"loc":["body","privacy"],"msg":"field required"
This means that your form is not submitting the terms and the privacy fields.
If you look at your HTML code, you can see that both of the inputs have the name my-checkbox, while fastapi route expects two parameters: privacy and terms.
Change the names of the inputs to match the two parameters

Python fill login form

I'm new with python and I'm trying to login to webpage for getting some data from it
this is my code:
import requests
from selenium import webdriver
driver = webdriver.Chrome()
url = "https://mano.eso.lt"
driver.get(url)
user = "xxxx#xxxx.com"
password = "yyyyyyy"
payload = {
"name[name]": user,
"user[pass]": password
}
s = requests.Session()
p = s.post(url, data = payload)
s.close
driver.close
So all what I'm getting just opening webpage, but not filling forms and not sending info to webpage
Could anybody help with this code?
Thank you
I think the URL which you're trying to hit the POST request on is incorrect. You'll have to hit the same request on the API endpoint URL and not in the webpage URL.
Ok, so I get some information:
<form class="user-login-form" data-drupal-selector="user-login-form" novalidate="novalidate" autocomplete="off" action="/" method="post" id="user-login-form" accept-charset="UTF-8">
<div class="input-type type-email">
<input autocorrect="none" autocapitalize="none" spellcheck="false" autofocus="autofocus" data-drupal-selector="edit-name" aria-describedby="edit-name--description" type="email" id="edit-name" name="name" value="" size="60" maxlength="254" class="form-email required" required="required" aria-required="true" />
</div>
<div id="edit-name--description" class="description">
Įveskite el.pašto adresą
</div>
<div class="input-password-wrapper">
<input autocomplete="off" data-drupal-selector="edit-pass" aria-describedby="edit-pass--description" type="password" id="edit-pass" name="pass" size="60" maxlength="128" class="form-text required" required="required" aria-required="true" />
<span class="password-view-toggle"><i class="icon-eye-closed"></i></span>
</div>
</div>
so I wrote a code like:
import time
from webbot import Browser
web = Browser()
web.go_to('https://mano.eso.lt')
time.sleep(5)
web.type('XXXXXX#xxxx.xxx', into = 'email')
time.sleep(1)
web.type('YYYYYYYYY', into = 'password')
result is that page is opening and email is writing to it. But for password it doesn't work. Any things what is wrong?

How to login to a website using python and upload a file to s3?

I have a script that uploads a file from a public URL to S3 which is working fine. i have a need to use this script to upload some files which are not Public, i need to log in to the site to get the file. i need help implementing this in my script; where the script would log in to the site and get the file. site i am trying to login to https://catalog.ldc.upenn.edu/login
Here is my Script
import threading
import requests
import boto3
from boto3.s3.transfer import TransferConfig
def multi_part_upload_with_s3():
config = TransferConfig(multipart_threshold=1024 * 25, max_concurrency=30,
multipart_chunksize=1024 * 25, use_threads=True)
url = "www.example-url.com"
r = requests.get(url, stream=True)
session = boto3.Session()
s3 = session.resource('s3')
key = 'examplePath/file'
bucket = s3.Bucket(bucket_name)
bucket.upload_fileobj(r.raw, key, Config=config, Callback=None)
Here is the html from the site i need to log in to
<form class="new_spree_user" id="new_spree_user" action="/login" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="xjxGqf8OYqJd6Ynt1GcK1Ms0XM6YcPz3kS69TFKCCRQwOgwh1kEMlpJSTKAoblXLtLpSLCIiwPWT+LLab0Altw==">
<div id="password-credentials">
<p>
<label for="spree_user_email">E-mail</label><br>
<input class="title" tabindex="1" type="text" name="spree_user[login]" id="spree_user_login">
</p>
<p>
<label for="spree_user_password">Password</label><br>
<input class="title" tabindex="2" type="password" name="spree_user[password]" id="spree_user_password">
</p>
</div>
<p>
<input name="spree_user[remember_me]" type="hidden" value="0"><input type="checkbox" value="1" name="spree_user[remember_me]" id="spree_user_remember_me">
<label for="spree_user_remember_me">Remember me</label>
</p>
<p><input type="submit" name="commit" value="Login" class="button primary" tabindex="3" id="submit_form" data-disable-with="Login"></p>
</form>
or
Create a new account |
Forgot password?

Login a website using python script redirects at same page

i am trying to login into a website using python script having the 'form' tag like this in the source code.
<form action="trylogin.php" method="post">
<input name="authenticity_token" type="hidden" value="dpsTlD8zutj35FVWJTIqtUZGX67qQ/vab33hpPyYuaU=" />
<input id="user_username" maxlength="20" name="username" placeholder="Username" size="20" type="text" />
<input id="user_password" name="password" placeholder="Password" size="20" type="password" />
<input class="submit themed_bg themed-dark-hover-background" name="commit" type="submit" value="Login" />
</form>
and i am trying the following python code
import requests
import lxml
import lxml.html
s = requests.session()
login = s.get('url')
login_html = lxml.html.fromstring(login.text)
hidden_inputs = login_html.xpath(r'//form//input[#type="hidden"]')
form = {x.attrib["name"]: x.attrib["value"] for x in hidden_inputs}
form['username'] ='user'
form['password'] ='pass'
form['commit'] = 'Login'
response = s.post('url', data=form)
response.ok
response.url
s.get() is working fine and response.ok also gives 'true'output but url of response is same as of previous page. it seems like it is redirecting the same page. i can't login from python. What should i do Should i use header arguement in s.post()? how to know it? and i have used
form['commit'] = 'Login'
since login is in form of input not button, is it correct?
<input class="submit themed_bg themed-dark-hover-background" name="commit" type="submit" value="Login" />

Python Mechanize change unnamed input value (known id)

as is written in topic i have to change value of some input field using mechanize but i dont have name of it only id :/ Let's stick to the point.
This is how form looks:
<form id="Login" name="Login">
<div id="login-inputs-div">
<input id="Username" type="hidden" name="username"></input>
<input id="Password" type="hidden" name="password"></input>
<input id="PopupUsername" class="input-text input-text-gray" type="text" disabled="disabled" value="admin" maxlength="32" style="width:100px;"></input>
<input id="PopupPassword" class="input-text input-text-gray " type="password" maxlength="32" value="" style="width:100px;" placeholder="hasło"></input>
<input id="bt_authenticate" class="input-btn input-btn-orange translation Translations.general.btn.authenticate translated" type="submit" value="zaloguj"></input>
</div>
<div id="logout-link-div" style="display: none;"></div>
</form>
What i have to do? Fill PopupPassword using some value and later submit it?
My approach looks like:
import mechanize
url = "http://192.168.1.1"
br = mechanize.Browser()
br.set_handle_robots(False)
br.open(url)
br.select_form(name="Login")
br.form().find_control(id="PopupPassword").__setattr__("value", "something")
res = br.submit()
content = res.read()
Im looking forward for some solution, thanks in advance.
find_control() is exactly what you need here. Just don't call br.form, it is not a function:
password_field = br.form.find_control(id="PopupPassword")
password_field.value = "something"

Categories