我構(gòu)建了一個 NodeJS 服務(wù)器,其中包含一個用于用戶及其密碼的小型數(shù)據(jù)庫。我為此創(chuàng)建了我的小網(wǎng)站,您可以在其中輸入您的用戶名和密碼,它會為您創(chuàng)建一個會話以便您登錄。它工作正?!,F(xiàn)在我想用Python寫一些東西來自動登錄一個小樹莓派。經(jīng)過大量的研究,我想出了一個這樣的python程序: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.textr = requests.get('http://192.168.7.1:3000/home')print r.text我在 NodeJS 應(yīng)用程序中的函數(shù)如下所示: 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(); }});當(dāng)我嘗試像我在瀏覽器中使用我的程序那樣對您進行身份驗證時,我最終會得到: response.send('請輸入用戶名和密碼!'); 我的發(fā)布請求有什么問題,我需要更改什么才能找到我的密碼和用戶名?對我來說另一個重要的問題是如何在 Python 中保持 Session 處于喚醒狀態(tài)以執(zhí)行其他一些請求。對于文件等?
來自 Python 的 Node JS Post 請求
慕運維8079593
2021-11-02 17:14:47