Reputation: 35
[Disclaimer: I am an absolute Beginner in Web-Engineering!!:)]
I build a NodeJS Server with a small database for User and their passwords. I created I little Website for this where you can give in your username and password it creates a session for you so that you are logged in. It works fine.
Now I want to write something in Python to log in a small raspberry automatically. After ton's of research I came up with a python programme like this:
import requests
def authentification():
s = Session();
req = Request('POST', "http://192.168.7.1:3000/auth")
prepped = req.prepare()
prepped.body = 'username=test&password=test'
#del prepped.headers['Content-Type']
resp = s.send(prepped)
print resp.text
r = requests.get('http://192.168.7.1:3000/home')
print r.text
my function in the NodeJS application looks like this:
app.post('/auth', function(request, response) {
var username = request.body.username;
var password = request.body.password;
console.log("Somebody tries to acces");
console.log(username)
if (username && password) {
connection.query('SELECT * FROM accounts WHERE username = ? AND password = ?', [username, password], function(error, results, fields) {
if (results.length > 0) {
request.session.loggedin = true;
request.session.cookie.maxAge = 3000;
request.session.username = username;
response.redirect('/home');
} else {
response.send('Incorrect Username and/or Password!');
}
response.end();
});
} else {
response.send('Please enter Username and Password!');
response.end();
}
});
When i try to authentificate you like i do it in the browser with my programm i will end up with: response.send('Please enter Username and Password!'); What is wrong with my Post Request and what do I need to change that it will find my password and username?
Another important Question for me is how i can keep the Session awake in Python to do some other requests. For files etc?
Thank you very much for your help! If there is any Problem with my question or the Input given by me please tell me an i will give the Informations you need!
Kind Regards Carl
Upvotes: 3
Views: 5639
Reputation: 4116
With the requests module you can send a post requests using the data
attribute to which you provide a dictionary containing your payload. It will looks like this
import requests
url = 'http://localhost:3000/auth'
correct_payload = {'username': 'hello', 'password': 'p4$$w0rd!'}
wrong_payload = {'username': 'hello'}
# Output => OK
r = requests.post(url, data=correct_payload)
print(r.text)
# Output => You need to provide Username & password
r = requests.post(url, data=wrong_payload)
print(r.text)
To parse the data you're receiving from your client, use the body-parser package as an Express.js middleware. This will parse and put your data in req.body
. The code will look like this
const express = require('express')
const bodyParser = require('body-parser')
const port = 3000
const app = express()
app.use(bodyParser.urlencoded({ extended: false }))
app.post('/auth', (req, res) => {
const { username, password } = req.body;
if (username && password) {
res.send('OK'); // ALL GOOD
} else {
res.status(400).send('You need to provide Username & password'); // BAD REQUEST
}
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
Upvotes: 5