Python 3 - Script to Send Credentials to PHP page
up vote
0
down vote
favorite
I wanted to try sending login details from a Python script to a web page which requires a username and password to access the main site.
I have set up a test environment by installing Apache server with PHP onto my Raspberry Pi.
I searched Stack Overflow and found the following script from user called Genetics:
login.html
<form action="login.php" method="post">
<input type="text" id="username" name="username"/>
<input type="password" id="password" name="password"/>
<input type="submit" name="Login" value="Login">
</form>
login.php
<html>
<head>
<title>Login</title>
</head>
<body>
<?php
//If Submit Button Is Clicked Do the Following
if ($_POST['Login']){
$myFile = "log.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $_POST['username'] . ":";
fwrite($fh, $stringData);
$stringData = $_POST['password'] . "n";
fwrite($fh, $stringData);
fclose($fh);
} ?>
//goes here after
<script>location.href='https://YOURWEBSITE.com';</script>
</body>
</html>
I have created these files on the Pi in the /var/www/html directory along with a log.txt file. When details are entered into the login.html page they are saved to the log.txt file and all works as expected.
What I wanted to do is run a Python 3 script to enter these details in without actually having to access the page through a browser. After a bit more digging, I found the following script and changed it to access the php page on the Pi:
import requests
url = 'http://192.168.0.23/login.php'
username = 'admin'
password = 'letmein'
r = requests.post(url, allow_redirects=False, data={
'username': username,
'password': password
})
I run the script and it does not show any errors but the login credentials in the script do not get written in the log.txt file.
This is the header file for the php page:
General:
Request URL: http://192.168.0.23/login.php
Request Method: POST
Status Code: 200 OK
Remote Address: 192.168.0.23:80
Referrer Policy: no-referrer-when-downgrade
Response Headers:
Connection: Keep-Alive
Content-Encoding: gzip
Content-Length: 135
Content-Type: text/html; charset=UTF-8
Date: Sat, 10 Nov 2018 22:01:13 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.4.10 (Raspbian)
Vary: Accept-Encoding
Request Headers:
POST /login.php HTTP/1.1
Host: 192.168.0.23
Connection: keep-alive
Content-Length: 41
Cache-Control: max-age=0
Origin: http://192.168.0.23
Upgrade-Insecure-Requests: 1
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,
image/webp,image/apng,*/*;q=0.8
Referer: http://192.168.0.23/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Form Data:
username: admin
password: letmein
Login: Login
Can anyone help and show me how to get this to work as I expect it to?
Any help much appreciatted.
php python-3.x
add a comment |
up vote
0
down vote
favorite
I wanted to try sending login details from a Python script to a web page which requires a username and password to access the main site.
I have set up a test environment by installing Apache server with PHP onto my Raspberry Pi.
I searched Stack Overflow and found the following script from user called Genetics:
login.html
<form action="login.php" method="post">
<input type="text" id="username" name="username"/>
<input type="password" id="password" name="password"/>
<input type="submit" name="Login" value="Login">
</form>
login.php
<html>
<head>
<title>Login</title>
</head>
<body>
<?php
//If Submit Button Is Clicked Do the Following
if ($_POST['Login']){
$myFile = "log.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $_POST['username'] . ":";
fwrite($fh, $stringData);
$stringData = $_POST['password'] . "n";
fwrite($fh, $stringData);
fclose($fh);
} ?>
//goes here after
<script>location.href='https://YOURWEBSITE.com';</script>
</body>
</html>
I have created these files on the Pi in the /var/www/html directory along with a log.txt file. When details are entered into the login.html page they are saved to the log.txt file and all works as expected.
What I wanted to do is run a Python 3 script to enter these details in without actually having to access the page through a browser. After a bit more digging, I found the following script and changed it to access the php page on the Pi:
import requests
url = 'http://192.168.0.23/login.php'
username = 'admin'
password = 'letmein'
r = requests.post(url, allow_redirects=False, data={
'username': username,
'password': password
})
I run the script and it does not show any errors but the login credentials in the script do not get written in the log.txt file.
This is the header file for the php page:
General:
Request URL: http://192.168.0.23/login.php
Request Method: POST
Status Code: 200 OK
Remote Address: 192.168.0.23:80
Referrer Policy: no-referrer-when-downgrade
Response Headers:
Connection: Keep-Alive
Content-Encoding: gzip
Content-Length: 135
Content-Type: text/html; charset=UTF-8
Date: Sat, 10 Nov 2018 22:01:13 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.4.10 (Raspbian)
Vary: Accept-Encoding
Request Headers:
POST /login.php HTTP/1.1
Host: 192.168.0.23
Connection: keep-alive
Content-Length: 41
Cache-Control: max-age=0
Origin: http://192.168.0.23
Upgrade-Insecure-Requests: 1
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,
image/webp,image/apng,*/*;q=0.8
Referer: http://192.168.0.23/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Form Data:
username: admin
password: letmein
Login: Login
Can anyone help and show me how to get this to work as I expect it to?
Any help much appreciatted.
php python-3.x
Hi, maybe change the URL tourl = 'http://127.0.0.1/login.php'
?
– ankabout
Nov 10 at 22:46
127.0.0.1 just gives me a big bunch of errors. I am not running the python script on the Pi. I am running it from a seperate Win7 laptop. 192.168.0.23 is the correct address for my Pi on my network.
– zeeman
Nov 10 at 22:57
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I wanted to try sending login details from a Python script to a web page which requires a username and password to access the main site.
I have set up a test environment by installing Apache server with PHP onto my Raspberry Pi.
I searched Stack Overflow and found the following script from user called Genetics:
login.html
<form action="login.php" method="post">
<input type="text" id="username" name="username"/>
<input type="password" id="password" name="password"/>
<input type="submit" name="Login" value="Login">
</form>
login.php
<html>
<head>
<title>Login</title>
</head>
<body>
<?php
//If Submit Button Is Clicked Do the Following
if ($_POST['Login']){
$myFile = "log.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $_POST['username'] . ":";
fwrite($fh, $stringData);
$stringData = $_POST['password'] . "n";
fwrite($fh, $stringData);
fclose($fh);
} ?>
//goes here after
<script>location.href='https://YOURWEBSITE.com';</script>
</body>
</html>
I have created these files on the Pi in the /var/www/html directory along with a log.txt file. When details are entered into the login.html page they are saved to the log.txt file and all works as expected.
What I wanted to do is run a Python 3 script to enter these details in without actually having to access the page through a browser. After a bit more digging, I found the following script and changed it to access the php page on the Pi:
import requests
url = 'http://192.168.0.23/login.php'
username = 'admin'
password = 'letmein'
r = requests.post(url, allow_redirects=False, data={
'username': username,
'password': password
})
I run the script and it does not show any errors but the login credentials in the script do not get written in the log.txt file.
This is the header file for the php page:
General:
Request URL: http://192.168.0.23/login.php
Request Method: POST
Status Code: 200 OK
Remote Address: 192.168.0.23:80
Referrer Policy: no-referrer-when-downgrade
Response Headers:
Connection: Keep-Alive
Content-Encoding: gzip
Content-Length: 135
Content-Type: text/html; charset=UTF-8
Date: Sat, 10 Nov 2018 22:01:13 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.4.10 (Raspbian)
Vary: Accept-Encoding
Request Headers:
POST /login.php HTTP/1.1
Host: 192.168.0.23
Connection: keep-alive
Content-Length: 41
Cache-Control: max-age=0
Origin: http://192.168.0.23
Upgrade-Insecure-Requests: 1
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,
image/webp,image/apng,*/*;q=0.8
Referer: http://192.168.0.23/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Form Data:
username: admin
password: letmein
Login: Login
Can anyone help and show me how to get this to work as I expect it to?
Any help much appreciatted.
php python-3.x
I wanted to try sending login details from a Python script to a web page which requires a username and password to access the main site.
I have set up a test environment by installing Apache server with PHP onto my Raspberry Pi.
I searched Stack Overflow and found the following script from user called Genetics:
login.html
<form action="login.php" method="post">
<input type="text" id="username" name="username"/>
<input type="password" id="password" name="password"/>
<input type="submit" name="Login" value="Login">
</form>
login.php
<html>
<head>
<title>Login</title>
</head>
<body>
<?php
//If Submit Button Is Clicked Do the Following
if ($_POST['Login']){
$myFile = "log.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $_POST['username'] . ":";
fwrite($fh, $stringData);
$stringData = $_POST['password'] . "n";
fwrite($fh, $stringData);
fclose($fh);
} ?>
//goes here after
<script>location.href='https://YOURWEBSITE.com';</script>
</body>
</html>
I have created these files on the Pi in the /var/www/html directory along with a log.txt file. When details are entered into the login.html page they are saved to the log.txt file and all works as expected.
What I wanted to do is run a Python 3 script to enter these details in without actually having to access the page through a browser. After a bit more digging, I found the following script and changed it to access the php page on the Pi:
import requests
url = 'http://192.168.0.23/login.php'
username = 'admin'
password = 'letmein'
r = requests.post(url, allow_redirects=False, data={
'username': username,
'password': password
})
I run the script and it does not show any errors but the login credentials in the script do not get written in the log.txt file.
This is the header file for the php page:
General:
Request URL: http://192.168.0.23/login.php
Request Method: POST
Status Code: 200 OK
Remote Address: 192.168.0.23:80
Referrer Policy: no-referrer-when-downgrade
Response Headers:
Connection: Keep-Alive
Content-Encoding: gzip
Content-Length: 135
Content-Type: text/html; charset=UTF-8
Date: Sat, 10 Nov 2018 22:01:13 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.4.10 (Raspbian)
Vary: Accept-Encoding
Request Headers:
POST /login.php HTTP/1.1
Host: 192.168.0.23
Connection: keep-alive
Content-Length: 41
Cache-Control: max-age=0
Origin: http://192.168.0.23
Upgrade-Insecure-Requests: 1
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,
image/webp,image/apng,*/*;q=0.8
Referer: http://192.168.0.23/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Form Data:
username: admin
password: letmein
Login: Login
Can anyone help and show me how to get this to work as I expect it to?
Any help much appreciatted.
php python-3.x
php python-3.x
asked Nov 10 at 22:19
zeeman
608
608
Hi, maybe change the URL tourl = 'http://127.0.0.1/login.php'
?
– ankabout
Nov 10 at 22:46
127.0.0.1 just gives me a big bunch of errors. I am not running the python script on the Pi. I am running it from a seperate Win7 laptop. 192.168.0.23 is the correct address for my Pi on my network.
– zeeman
Nov 10 at 22:57
add a comment |
Hi, maybe change the URL tourl = 'http://127.0.0.1/login.php'
?
– ankabout
Nov 10 at 22:46
127.0.0.1 just gives me a big bunch of errors. I am not running the python script on the Pi. I am running it from a seperate Win7 laptop. 192.168.0.23 is the correct address for my Pi on my network.
– zeeman
Nov 10 at 22:57
Hi, maybe change the URL to
url = 'http://127.0.0.1/login.php'
?– ankabout
Nov 10 at 22:46
Hi, maybe change the URL to
url = 'http://127.0.0.1/login.php'
?– ankabout
Nov 10 at 22:46
127.0.0.1 just gives me a big bunch of errors. I am not running the python script on the Pi. I am running it from a seperate Win7 laptop. 192.168.0.23 is the correct address for my Pi on my network.
– zeeman
Nov 10 at 22:57
127.0.0.1 just gives me a big bunch of errors. I am not running the python script on the Pi. I am running it from a seperate Win7 laptop. 192.168.0.23 is the correct address for my Pi on my network.
– zeeman
Nov 10 at 22:57
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
As you're expecting $_POST['Login']
to be set for triggering your code, you'll have to provide a value to make it works. Add 'Login': true
into your requests.post
data
and ti will work great.
Thanks, the penny just dropped on how to trigger the code :)
– zeeman
Nov 12 at 20:01
I posted my solution before you provided your answer
– zeeman
Nov 12 at 20:16
lol, ok you helped me and solved the issue
– zeeman
Nov 13 at 0:09
huge thanks btw
– zeeman
Nov 13 at 0:11
My pleasure ;) Next time I would try to be faster hehehe
– Jonathan Gagne
Nov 13 at 2:53
add a comment |
up vote
0
down vote
After much trying and failing, it turns out the issue was I was sending a username and password but no parameter for the login button.
The correct code is:
import requests
url = 'http://192.168.0.23/login.php'
username = 'admin'
password = 'letmein'
Login = 'Login'
r = requests.post(url, allow_redirects=False, data={
'username': username,
'password': password,
'Login': Login
})
And a second solution:
import requests
headers = {'User-Agent': 'Mozilla/5.0'}
payload = {'username':'Admin','password':'Letmein','Login':'Login'}
session = requests.Session()
session.post('http://192.168.0.23/login.php',headers=headers,data=payload)
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
As you're expecting $_POST['Login']
to be set for triggering your code, you'll have to provide a value to make it works. Add 'Login': true
into your requests.post
data
and ti will work great.
Thanks, the penny just dropped on how to trigger the code :)
– zeeman
Nov 12 at 20:01
I posted my solution before you provided your answer
– zeeman
Nov 12 at 20:16
lol, ok you helped me and solved the issue
– zeeman
Nov 13 at 0:09
huge thanks btw
– zeeman
Nov 13 at 0:11
My pleasure ;) Next time I would try to be faster hehehe
– Jonathan Gagne
Nov 13 at 2:53
add a comment |
up vote
0
down vote
accepted
As you're expecting $_POST['Login']
to be set for triggering your code, you'll have to provide a value to make it works. Add 'Login': true
into your requests.post
data
and ti will work great.
Thanks, the penny just dropped on how to trigger the code :)
– zeeman
Nov 12 at 20:01
I posted my solution before you provided your answer
– zeeman
Nov 12 at 20:16
lol, ok you helped me and solved the issue
– zeeman
Nov 13 at 0:09
huge thanks btw
– zeeman
Nov 13 at 0:11
My pleasure ;) Next time I would try to be faster hehehe
– Jonathan Gagne
Nov 13 at 2:53
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
As you're expecting $_POST['Login']
to be set for triggering your code, you'll have to provide a value to make it works. Add 'Login': true
into your requests.post
data
and ti will work great.
As you're expecting $_POST['Login']
to be set for triggering your code, you'll have to provide a value to make it works. Add 'Login': true
into your requests.post
data
and ti will work great.
answered Nov 12 at 19:58
Jonathan Gagne
1,8802720
1,8802720
Thanks, the penny just dropped on how to trigger the code :)
– zeeman
Nov 12 at 20:01
I posted my solution before you provided your answer
– zeeman
Nov 12 at 20:16
lol, ok you helped me and solved the issue
– zeeman
Nov 13 at 0:09
huge thanks btw
– zeeman
Nov 13 at 0:11
My pleasure ;) Next time I would try to be faster hehehe
– Jonathan Gagne
Nov 13 at 2:53
add a comment |
Thanks, the penny just dropped on how to trigger the code :)
– zeeman
Nov 12 at 20:01
I posted my solution before you provided your answer
– zeeman
Nov 12 at 20:16
lol, ok you helped me and solved the issue
– zeeman
Nov 13 at 0:09
huge thanks btw
– zeeman
Nov 13 at 0:11
My pleasure ;) Next time I would try to be faster hehehe
– Jonathan Gagne
Nov 13 at 2:53
Thanks, the penny just dropped on how to trigger the code :)
– zeeman
Nov 12 at 20:01
Thanks, the penny just dropped on how to trigger the code :)
– zeeman
Nov 12 at 20:01
I posted my solution before you provided your answer
– zeeman
Nov 12 at 20:16
I posted my solution before you provided your answer
– zeeman
Nov 12 at 20:16
lol, ok you helped me and solved the issue
– zeeman
Nov 13 at 0:09
lol, ok you helped me and solved the issue
– zeeman
Nov 13 at 0:09
huge thanks btw
– zeeman
Nov 13 at 0:11
huge thanks btw
– zeeman
Nov 13 at 0:11
My pleasure ;) Next time I would try to be faster hehehe
– Jonathan Gagne
Nov 13 at 2:53
My pleasure ;) Next time I would try to be faster hehehe
– Jonathan Gagne
Nov 13 at 2:53
add a comment |
up vote
0
down vote
After much trying and failing, it turns out the issue was I was sending a username and password but no parameter for the login button.
The correct code is:
import requests
url = 'http://192.168.0.23/login.php'
username = 'admin'
password = 'letmein'
Login = 'Login'
r = requests.post(url, allow_redirects=False, data={
'username': username,
'password': password,
'Login': Login
})
And a second solution:
import requests
headers = {'User-Agent': 'Mozilla/5.0'}
payload = {'username':'Admin','password':'Letmein','Login':'Login'}
session = requests.Session()
session.post('http://192.168.0.23/login.php',headers=headers,data=payload)
add a comment |
up vote
0
down vote
After much trying and failing, it turns out the issue was I was sending a username and password but no parameter for the login button.
The correct code is:
import requests
url = 'http://192.168.0.23/login.php'
username = 'admin'
password = 'letmein'
Login = 'Login'
r = requests.post(url, allow_redirects=False, data={
'username': username,
'password': password,
'Login': Login
})
And a second solution:
import requests
headers = {'User-Agent': 'Mozilla/5.0'}
payload = {'username':'Admin','password':'Letmein','Login':'Login'}
session = requests.Session()
session.post('http://192.168.0.23/login.php',headers=headers,data=payload)
add a comment |
up vote
0
down vote
up vote
0
down vote
After much trying and failing, it turns out the issue was I was sending a username and password but no parameter for the login button.
The correct code is:
import requests
url = 'http://192.168.0.23/login.php'
username = 'admin'
password = 'letmein'
Login = 'Login'
r = requests.post(url, allow_redirects=False, data={
'username': username,
'password': password,
'Login': Login
})
And a second solution:
import requests
headers = {'User-Agent': 'Mozilla/5.0'}
payload = {'username':'Admin','password':'Letmein','Login':'Login'}
session = requests.Session()
session.post('http://192.168.0.23/login.php',headers=headers,data=payload)
After much trying and failing, it turns out the issue was I was sending a username and password but no parameter for the login button.
The correct code is:
import requests
url = 'http://192.168.0.23/login.php'
username = 'admin'
password = 'letmein'
Login = 'Login'
r = requests.post(url, allow_redirects=False, data={
'username': username,
'password': password,
'Login': Login
})
And a second solution:
import requests
headers = {'User-Agent': 'Mozilla/5.0'}
payload = {'username':'Admin','password':'Letmein','Login':'Login'}
session = requests.Session()
session.post('http://192.168.0.23/login.php',headers=headers,data=payload)
answered Nov 12 at 19:51
zeeman
608
608
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243990%2fpython-3-script-to-send-credentials-to-php-page%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hi, maybe change the URL to
url = 'http://127.0.0.1/login.php'
?– ankabout
Nov 10 at 22:46
127.0.0.1 just gives me a big bunch of errors. I am not running the python script on the Pi. I am running it from a seperate Win7 laptop. 192.168.0.23 is the correct address for my Pi on my network.
– zeeman
Nov 10 at 22:57