[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