0

I try to log in this website using python (and after automate some actions) : https://www.rika-firenet.com/web/login

As you see on the page, the html code is this one :

<form id="login" method="POST" action="/action_page.php" data-ajax="false">
  <input type="text" data-theme="b" name="email" value="" placeholder="email@example.com">
  <input type="password" data-theme="b" name="password" value="" placeholder="password">
  <button type="submit" data-theme="a" data-icon="fa-sign-in" data-iconpos="right">
    Connect
  </button>
</form>

So I tried that in python :

import requests
import urllib.parse

url = 'https://www.rika-firenet.com/'
url_login = url+'web/login'

client = requests.session()
payload = urllib.parse.urlencode({
    'email':'myemail@mail.com',
    'password':'mypwd'
})

print("+-[url get] : {}".format(url_login))
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
r = client.get(url, headers=headers, allow_redirects=True)
print(r.cookies)

print("+-[url post] : {}".format(url_login))
p = client.post(url_login, data=payload, headers=headers, cookies=r.cookies)
print(p.content)

I get the cookie in the get, but I have the original page in return of post request.

+-[url get] : https://www.rika-firenet.com/web/login
<RequestsCookieJar[<Cookie connect.sid=s%3Auzv2S7zjhW6Hs2S7hOKyx6icXhbSSSTx.t%2Fg32GT2s2zIbvGI3kq%2Fht%2FR3BDa8aPUwTmWl%2BYktKU for www.rika-firenet.com/>]>
+-[url post] : https://www.rika-firenet.com/web/login

Somebody succeeded with this php code :

function login ()
{
  global $login_url,$username_rika,$password_rika,$path_cookie;
  $postinfo = "email=".$username_rika."&password=".$password_rika;
  $status = false;
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_HEADER, false);
  curl_setopt($ch, CURLOPT_NOBODY, false);
  curl_setopt($ch, CURLOPT_URL, $login_url);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_FAILONERROR, true);

  curl_setopt($ch, CURLOPT_COOKIEJAR, $path_cookie);
  curl_setopt($ch, CURLOPT_COOKIEFILE, $path_cookie); // file to read cookies in
  //set the cookie the site has for certain features, this is optional
  curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0");
  curl_setopt($ch, CURLOPT_USERAGENT,
      "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
  $return = curl_exec($ch);

  // Retourne le numéro d'erreur de la dernière opération cURL.
  $curl_errno = curl_errno($ch);
  $curl_error = curl_error($ch);

  if ($curl_errno > 0) {
        echo "cURL Error ($curl_errno): $curl_error\n";
        $status['connected'] = false;
        $status['curl_errno'] = curl_errno($ch);
        $status['curl_error'] = curl_error($ch);
        exit; // mettre en veille en mode développement
     }
  else {
        //echo "Data received phase 1 : $return\n";
        $status['connected'] = true;
     }
  curl_close($ch);
  return $status;
}

But as I'm not a php expert, I don't really understand why it works.

Any clue ?

Thanks

Greg

ps : After resolution, complete code for controlling Rika stoves can be found in https://github.com/iero/Rika-Stove

iero
  • 401
  • 3
  • 14
  • Try specifying a user agent? `headers={'User-Agent' : "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7"}` – cs95 Nov 24 '17 at 08:21
  • The PHP curl code doesn't set a referrer, but does set the `User-Agent` header. – Martijn Pieters Nov 24 '17 at 08:26
  • Thanks ! I tried (updated code) but It's the same output. – iero Nov 24 '17 at 08:29
  • 1
    Possible duplicate of [How to "log in" to a website using Python's Requests module?](https://stackoverflow.com/questions/11892729/how-to-log-in-to-a-website-using-pythons-requests-module) – Mark Nov 24 '17 at 08:34
  • Thanks Mark, but I'm not sure that answer my question. They just pass "payload" in post request and I already do the same thing. I think I need to do something else. – iero Nov 24 '17 at 09:07

1 Answers1

1

Some notes:

  • Don't urlencode your post data with urllib, let requests handle the encoding.
  • requests.session persists cookies across requests by default, so you don't have to use the cookies parameter.
  • requests follows redirects by default.
  • You don't have to spoof User-Agent for this site. If you want to add some headers you could do that in the session object so you won't have to use the headers parameter for every request.

Python code:

import requests

url = 'https://www.rika-firenet.com/'
url_login = url+'web/login'
s = requests.session()
#s.headers['User-Agent'] = 'Mozilla/5.0'
data = {'email':'myemail@mail.com', 'password':'mypwd'}
r = s.post(url_login, data)

print(r.url)
print('Log out' in r.text)
t.m.adam
  • 15,106
  • 3
  • 32
  • 52
  • Thank you very much t.m.adam, it works ! And with all details provided, I learned several things in this discussion for my future requests use. Thanks again !! – iero Nov 24 '17 at 12:30
  • 1
    Thanks to this help, complete code for controlling the stove can be found on https://github.com/iero/Rika-Stove – iero Nov 26 '17 at 23:33