Send Text from HTML to Python - 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

Related

Python POST request with the Request module

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.

Invalid Request Origin after Python post request

I've been trying to write a program which gets my school timetable which is locked behind a login screen, however when I run my python script I am greeted by:
Sorry, we are unable to complete this request
An error occured while submitting the form:
Invalid Request Origin
This is the code I've written so far:
import requests
login_url = 'https://WEBSITE/login?page=%2Fhomepage'
timetable_url = 'https://WEBSITE/timetable'
payload = {
'username': 'USERNAME',
'password': 'PASSWORD'
}
with requests.Session() as session:
post = session.post(login_url, data=payload, verify = True)
r = session.get(timetable_url)
print (post.text)
And this is the html from the login page:
<div id="content">
<form id="login" method="post" action="/login?page=%2Flogin%2F">
<input type="hidden" name="page" value="/login/" />
<div class="row collapse">
<div class="small-12 column">
<div class="logo">
<img src="/images/logo.php?logo=skin_logo_login&size=normal" alt="Schoolbox" />
</div>
</div>
<div class="small-12 column">
<p id="error-msg" class="alert-box alert"></p>
</div>
<div class="small-12 column">
<input type="text" name="username" id="username" placeholder="Username" />
<input type="password" name="password" id="password" placeholder="Password" />
</div>
</div>
<div class="row collapse">
<div class="small-12 column">
<p>Forgotten your password?</p>
</div>
<div class="small-12 column">
<input id="rememberme" type="checkbox" name="rememberme" value="1" />
<label for="rememberme">Remember me</label>
</div>
<div class="small-12 column login-links">
<input type="submit" name="Submit" value="Login" alt="Login" />
</div>
</div>
</form>
</div>
How exactly would I be able to solve this error, or would there be any other more viable solutions? Thank you.
This error Invalid Request Origin means that your origin does not match what is restricted by the server. You could try adding the Origin: header specifying your timetable_url, but it is likely that the page can only be called by another page on the server.

Python send mail on button click Google App Engine

Frustration: I have searched most of the websites and still was not able to find any answer.
Info: I don't know anything about Python.
What I want to code: I want to send a mail using Python on button click of an HTML web page. It should not open the user's email client, like 'mailto' does in html, but directly send me the mail. The web application is hosted using Google App Engine (url is like www.example.appspot.com). I want to also send the values in text box and radio group to be sent along to the recipients email address.
HTML form:
<form method="post" id="rsvpForm" action="">
<div class="row half">
<div class="6u">
<input type="text" name="name" id="name" placeholder="Name" />
</div>
<div class="6u">
<input type="text" name="email" id="email" placeholder="Email" />
</div>
</div>
<br/>
<div>
<div class="12u">
<span>Radio grp 1</span>
</div>
</div>
<div class="row half">
<div class="12u">
<input type="radio" name="attendance" value="Options">Options<br>
</div>
</div>
<br/>
<div class="row half">
<div class="12u">
<textarea name="message" id="message" placeholder="Message or wishes" rows="3">
</textarea>
</div>
</div>
<div class="row half">
<div class="12u">
<ul class="actions">
<li><input type="submit" class="style1" value="Send" id="sendMessage"/>
</li>
<li><input type="reset" class="style2" value="Reset" /></li>
</ul>
</div>
</div>
Reference Used: I used this site to host the website
http://www.labnol.org/internet/host-website-on-google-app-engine/18801/
Please help me. I don't want to use PHP
Send an email from GAE Python is very easy, but you post only the HTML code, have you a python handler in place to do that? If not I suggest to learn and test all these tutorial provided by google: https://developers.google.com/appengine/docs/python/gettingstartedpython27/introduction
This is a sample to send an email form GAE python: https://developers.google.com/appengine/docs/python/mail/?hl=fr#Python_Sending_mail_in_Python

How do I use Python to Login to a website using urllib2 and then open a browser page to the logged in site?

I saw a similar question asking for help logging into reddit here.
The site I want to login seems more complex. The form from the HTML looks like this:
<div class="bottom-wrapper">
<div class="sidebar-container">
<div id="logged-in-user">
<div class="ajax-loading"></div>
<div class="panel-pane pane-type1 anonymous-content" id="pane-login-block">
<h2 class="pane-title">Login</h2>
<div id="login-section" class="pane-content">
<form method="post" action="https://www.fancywebsite.com/php/login.php">
<input type="hidden" name="destination" value="">
<input type="hidden" value="user_login" name="form_id">
<input type="hidden" value="2800" name="affid">
<input type="hidden" value="0" name="blocklogin">
<input type="hidden" value="1" name="wager">
<input id="edit-redirect" type="hidden" value="http://www.fancywebsite.com/main" name="redirect">
<ul class="field-set">
<li>
<label for="username">Username:</label>
<input type="text" name="acct" id="username" class="text-box" maxlength="100" size="20">
</li>
<li>
<label for="password">Password:</label>
<input type="password" name="pin" id="password" class="text-box" maxlength="16" size="20">
</li>
<li>
<span id="reset-login-link">forgot your login information?</span>
<input type="submit" class="button" value="Login" id="Login" name="Login">
</li>
</ul>
</form>
I want Python to go to that login page and enter my credentials and then open a browser to the logged in page. Any help would be greatly appreciated. I tried to adapt the method from the reddit example work, without luck. I don't want to use mechanize. Thanks in advance.
You can use either requests and urllib2 to login or you can use mechanize to simulate the browser, which is a better way I think.
Here is link you can refer to:
requests or mechanize

How to send login request in python?

i am trying this python script:
import httplib
import urllib
conn = httplib.HTTPConnection("moodle.tau.ac.il")
conn.request("POST" , "/hu11/?lang=en_utf8&username=bugs&password=bunny" )
response = conn.getresponse()
print response.status, response.reason
print response.read()
conn.close()
but it doesn't log in the site as i want to. i want to build some script to help a friend to download all the files from his courses.
what do i do wrong in setting a post request to the server?
i have this form tag in site i would like to login to with python script:
<form action="" method="post" id="login">
<div class="loginform">
<div class="form-label"><label for="username">Username</label></div>
<div class="form-input">
<input type="text" name="username" id="username" size="8" maxlength=8 value="" />
</div>
<div class="clearer"><!-- --></div>
<div class="form-label"><label for="password">Password</label></div>
<div class="form-input">
<input type="password" name="password" id="password" size="5" value="" />
<br /><br />
<input type="submit" value="Login" />
<input type="hidden" name="testcookies" value="0" />
</div>
<div class="clearer"><!-- --></div>
</div>
</form>
Try BeautifulSoup or Mechanize.

Categories

Resources