Python POST request with the Request module - python

I am trying to create a Python script that will fill some information in the database. For the server side I have used PHP and when I try to submit information using a browser, it works. But when I try to do it using the below Python script, it doesn't.
import requests
url_insert = 'http://192.168.1.100/index.php'
data_insert = {'fullname':'spiderman',
'ssn':'1234',
'dept':'Security',
'salary':10000,
'homeaddress':'New York',
'btn_save':'Save'}
req = requests.post(url_insert, data = data_insert)
print(req.text)
Response:
Connected Successfully.<br><html>
<head>
<title>RETRIEVE DATA</title>
</head>
<body>
<form action="data.php" method="POST">
<div class="form-group">
<label for="id">Full Name</label>
<input type="text" name="fullname" id="fullname" value="" placeholder="FullName">
<br>
<br>
<label for="id">Social Security Number</label>
<input type="text" name="ssn" id="ssn" value="" placeholder="Social Security Number">
<br>
<br>
<label for="id">Department</label>
<input type="text" name="dept" id="dept" value="" placeholder="Department">
<br>
<br>
<label for="id">Salary</label>
<input type="text" name="salary" id="salary" value="" placeholder="Salary">
<br>
<br>
<label for="id">Address</label>
<input type="text" name="homeaddress" id="homeaddress" value="" placeholder="Address">
<br>
<br>
<input type="submit" name="btn_save" value="Save">
</div>
</form>
</body>
</html>
HTML CODE:
<html>
<head>
<title>RETRIEVE DATA</title>
</head>
<body>
<form action="data.php" method="POST">
<div class="form-group">
<label for="id">Full Name</label>
<input type="text" name="fullname" id="fullname" value="" placeholder="FullName">
<br>
<br>
<label for="id">Social Security Number</label>
<input type="text" name="ssn" id="ssn" value="" placeholder="Social Security Number">
<br>
<br>
<label for="id">Department</label>
<input type="text" name="dept" id="dept" value="" placeholder="Department">
<br>
<br>
<label for="id">Salary</label>
<input type="text" name="salary" id="salary" value="" placeholder="Salary">
<br>
<br>
<label for="id">Address</label>
<input type="text" name="homeaddress" id="homeaddress" value="" placeholder="Address">
<br>
<br>
<input type="submit" name="btn_save" value="Save">
</div>
</form>
</body>
</html>
I am new to this, and any help would be much appreciated.

I am going to work on the assumption that when you have had success submitting the information using a browser that it was by using the form generated by script index.php.
That HTML form inputs data from a user into field names such as ssn and posts the data to script data.php. Your Python script should likewise be posting the data to data.php (it, however, seems to be missing data for fullname). But instead you are posting to index.php. I would then expect the response to be the HTML form with which you have previously had success submitting the information. That certainly seems to be the case.
Just change index.php to data.php and provide a value for fullname.

Related

How do I fill in a loginForm using BeautifulSoup

I want to fill in a loginForm using BeautifulSoup (I know it's not the easiest way) but I don't know how I can do that. Here under the loginForm.
<form id="loginForm" class="loginContainer" accept-charset="utf-8" action="/wipb2bweb/j_spring_security_check;jsessionid=C647FBABEB8C4ADC3DCCA3C6C875BECC.prod1" method="post"><fieldset>
<legend>Login</legend>
<img src="/wipb2bweb/_ui/themes/carhartt/images/loginTitle.png"
alt="Login" class="title" />
<form>
<div class="formRow">
<label for="User"><img
src="/wipb2bweb/_ui/themes/carhartt/images/loginLabelUser_de.png"
alt="Benutzername" />
</label>
<input id="User" name="j_username" type="text" value=""/></div>
<div class="formRow">
<label for="Password"><img
src="/wipb2bweb/_ui/themes/carhartt/images/loginLabelPass_de.png"
alt="Passwort" />
</label>
<input id="Password" name="j_password" type="password" value=""/></div>
<input type="submit" class="hiddenSubmit"/>
<a href="#" title="Login" class="btnGreyTextured">
<span>Login</span>
</a>
</form>

Login automatically using python

So I've seen that there are a couple of similar questions but none of them worked for me. This is the login page that I'm trying to log in to:
https://cas.lu.se/cas/login?service=http%3A%2F%2Fwww.ceq.lth.se%2Frapporter%2F%3Flasar_lp%3Dalla%26program%3Dsamtliga%26kurskod%3DEDAA45%26sort%3Dkurskod
The form has the following html:
<div class="box fl-panel" id="login">
<form action="/cas/login;jsessionid=C32FA3678B0093A4DB474BFD56858B6C?service=http%3A%2F%2Fwww.ceq.lth.se%2Frapporter%2F%3Flasar_lp%3Dalla%26program%3Dsamtliga%26kurskod%3DEDAA45%26sort%3Dkurskod" class="fm-v clearfix" id="fm1" method="post">
Please enter your userid without "#lu.se" at the end.<br/><br/>
<div class="row fl-controls-left">
<label class="fl-label" for="username"><span class="accesskey">U</span>sername:</label>
<input accesskey="u" autocomplete="false" class="required" id="username" name="username" onblur="makeLowercase();" size="25" tabindex="1" type="text" value=""/>
</div>
<div class="row fl-controls-left">
<label class="fl-label" for="password"><span class="accesskey">P</span>assword:</label>
<input accesskey="p" autocomplete="off" class="required" id="password" name="password" size="25" tabindex="2" type="password" value=""/>
</div>
<div class="row btn-row">
<input name="lt" type="hidden" value="LT-61720-irxrtm4JCzVNIJ2pkpidljNGOw5KYX"/>
<input name="execution" type="hidden" value="e1s1"/>
<input name="_eventId" type="hidden" value="submit"/>
<input accesskey="l" class="btn-submit" name="submit" tabindex="4" type="submit" value="LOGIN"/>
<input accesskey="c" class="btn-reset" name="reset" tabindex="5" type="reset" value="CLEAR"/>
</div>
</form>
</div>
So far I've tried using urllib in the following way:
from urllib import request, parse
auth = parse.urlencode({"username": "??",
"password": "??",
"lt": "LT-61720-irxrtm4JCzVNIJ2pkpidljNGOw5KYX",
"execution": "e1s1",
"_eventId": "submit",
"submit": "LOGIN", })
auth = auth.encode("ascii")
req = request.Request(url,
with request.urlopen(url) as f:
res = f.read()
The problem is that the only thing I get back is the html for the original page itself, and not the page that it is supposed to load after authentication. I'm also not sure what the action attribute contains in the form tag...

Python - Requests Upload File Error 500

I have written the code below, which is supposed to upload an image to a site called Roblox using Requests.
import requests
s = requests.session()
login_data = dict(username='USERNAMEHERE', password='PASSWORDHERE')
s.post('https://www.roblox.com/newlogin', data=login_data)
upload_data = dict(name="PythonUpload")
r = s.post('http://www.roblox.com/build/upload', files={'file': open('PythonDecalUploadTest.jpg', 'rb')}, data=upload_data)
print(r) #This returns a 500 error
Here is the form that this is posting to.
<form action="/build/upload" enctype="multipart/form-data" id="upload-form" method="post">
<input name="__RequestVerificationToken" type="hidden" value="H_69BCzd4bdyJ7EXalAOvaa-zeZZOvtQR-E9YHnCpmH9HVZKxHGrSVjAciLInCibyD8k432ZRzqX8d0MPPfCv-vOfAM1">
<input id="assetTypeId" name="assetTypeId" type="hidden" value="13">
<input id="isOggUploadEnabled" name="isOggUploadEnabled" type="hidden" value="True">
<input id="groupId" name="groupId" type="hidden" value="">
<input id="onVerificationPage" name="onVerificationPage" type="hidden" value="False">
<div id="container">
<div class="form-row">
<label for="file">Find your image:</label>
<input id="file" type="file" name="file" tabindex="1">
<span id="file-error" class="error"></span>
</div>
<div class="form-row">
<label for="name">Decal Name:</label>
<input id="name" type="text" class="text-box text-box-medium" name="name" maxlength="50" tabindex="2">
<span id="name-error" class="error"></span>
</div>
<div class="form-row submit-buttons">
<a id="upload-button" class="btn-medium btn-primary btn-level-element " tabindex="3">Upload</a>
<span id="loading-container">
<img src="http://images.rbxcdn.com/ec4e85b0c4396cf753a06fade0a8d8af.gif">
</span>
</div>
</div>
</form>
I'm not sure what the request verification token is, so I just changed a few letters/numbers just in case.
So why is this returning a 500 error? Why isn't it uploading my image?

recover the data from models to views in form html in django

please i tried with this How can I build multiple submit buttons django form?
but it doesnt work for me and im new to django programming
my aim is : when the user enter the identifiant when he clicks on the button recherche i want that all information come on the input tags here is my code
views.py:
def modifierEmploye(request):
if request.method=='POST':
emp2=EmployeForm(request.POST)
if emp2.is_valid():
if 'recherche' in request.POST:
return HttpResponse("recherche")
elif 'enregistrer' in request.POST:
return HttpResponse("enregistrer")
else:
emp2=EmployeForm()
return render(request,"GPI/modifierEmploye.html",locals())
html:
<form action="" method="POST">
{% csrf_token %}
<p> <input id="id" maxlength="200" name="id" type="text" placeholder="identifiant" /></p><br>
<p> <input id="id_nom" maxlength="200" name="nom" type="text" placeholder="Nom" /></p><br>
<p><input id="id_prenom" maxlength="200" name="prenom" type="text" placeholder="Prenom"/></p><br>
<p> <input id="id_departement" maxlength="200" name="departement" type="text" placeholder="Département" /></p><br>
<p> <input id="id_poste" maxlength="200" name="poste" type="text" placeholder="Poste" /></p><br>
<p> <input id="id_telephone" maxlength="200" name="telephone" type="text" placeholder="Téléphone" /></p><br>
<p><input id="id_email" maxlength="200" name="email" type="email" placeholder="Email" /></p>
<p> <input id="id" maxlength="200" name="id" type="text" placeholder="Nom" /></p><br>
<br>
<input type="submit" value="Rechercher" name="recherche" />
<input type="submit" value="Enregistrer" name="enregistrer" />
</form>
this program doesn't work, please qomeone to help me
change your template with this, it will work :
<form action="" method="POST">
{% csrf_token %}
{{emp2.as_p}}
<input type="submit" value="Rechercher" name="recherche" />
<input type="submit" value="Enregistrer" name="enregistrer" />
</form>

Send Text from HTML to Python

I am running an html page on apache2:
http://localhost/test.html
I want to send the username to a python script check.py, How can I do that?
html:
<div id="wrapper">
<form name="login-form" class="login-form" action="check.py" method="post" onsubmit="validate();return false;">
<div class="header">
<h1>Login Form</h1>
<span>Fill out the form below to login to my super awesome imaginary control panel.</span>
</div>
<div class="content">
<input id="Uname" name="username" type="text" class="input username" placeholder="Username" />
<div class="user-icon"></div>
<input id="Pname"name="password" type="password" class="input password" placeholder="Password" />
<div class="pass-icon"></div>
</div>
<div class="footer">
<input type="submit" name="submit" value="Login" class="button"/>
<input type="submit" name="submit" value="Register" class="register" />
</div>
</form>
</div>
<div class="gradient"></div>
</body>
</html>
Well you are running a static page, their is no way you can pass the page data to python file this way.
What you need is to learn server side programming.
In python Django and Flask are the popular framework for server side programming.
Below is a question related to flask. that will suits your need
Sending data from HTML form to a Python script in Flask

Categories

Resources