LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   how to use python scripts to simulate the login? (https://www.linuxquestions.org/questions/linux-server-73/how-to-use-python-scripts-to-simulate-the-login-4175573713/)

624867243@qq.com 03-02-2016 02:26 AM

how to use python scripts to simulate the login?
 
how to use python scripts to simulate the login? below is my php website code,
login.html:
Code:

<html>
<body>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<form name="login" action="login1.php" method="POST"> 
name<input type="text" name="name"/>
<p>password<input type="password" name="password"/> </p>
<input name="log" type="submit" value="login">
</form>
</body>
</html>

login1.php:
Code:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php


$mysqli=mysqli_connect("localhost","root","root","logindb");
$name=$_POST["name"];
$passowrd=$_POST["password"];
//print_r($_COOKIE);

if ($name && $passowrd){
$sql = "SELECT * FROM test1 WHERE name='$name' and password='$passowrd'";
$res = mysqli_query($mysqli,$sql);
$rows=mysqli_num_rows($res);
if($rows){
//header("location:login1.php");
//exit;
echo "successful";


}
 else{
echo "failed,please check username and password!";
}
mysqli_free_result($res);
mysqli_close($mysqli);
}
?>

I can visit http://127.0.0.1/gongji/login/login.html to the website code.

i want to simulate login and it can reponse me success or failure? but

i try to use below python scripts,but it did not response success or fialure to me.how to modify it?

Code:

#coding:utf-8
import urllib
import urllib2

aa = {'user':'knight','pasword':'knight'}
cc = urllib.urlencode(aa)
#print "this is %s" %(cc)

url = 'http://127.0.0.1/gongji/login/login.html'
#print url
print urllib2.urlopen(url,cc).read()


HMW 03-02-2016 10:41 AM

Hi!

Not really sure what you are trying to do here. First of all, you are not sending any POST requests with your Python script.

I suggest that you A) Use Python 3 in conjunction with requests, and B) Check this link for further information:
http://stackoverflow.com/questions/1...d-post-request

Furthermore, here's an example of how I would read a web page in Python 3 with urllib:
Code:

>>> import urllib.request
>>> data = urllib.request.urlopen("http://google.com")
>>> raw_Html = wp.read()
>>> print(raw_Html)

But, seriously, don't. Use requests instead, you will thank me later...

Best regards,
HMW

624867243@qq.com 03-02-2016 07:22 PM

Quote:

Originally Posted by HMW (Post 5509039)
Hi!

Not really sure what you are trying to do here. First of all, you are not sending any POST requests with your Python script.

I suggest that you A) Use Python 3 in conjunction with requests, and B) Check this link for further information:
http://stackoverflow.com/questions/1...d-post-request

Furthermore, here's an example of how I would read a web page in Python 3 with urllib:
Code:

>>> import urllib.request
>>> data = urllib.request.urlopen("http://google.com")
>>> raw_Html = wp.read()
>>> print(raw_Html)

But, seriously, don't. Use requests instead, you will thank me later...

Best regards,
HMW

i want to the website can respons me login access or login failure。
finaly i want to check the login is unnormal or normal.
i try to modify it this:(but no matter the username and password is right or wrong) it print sucess.
Code:

#coding:utf-8
import urllib
url = 'http://127.0.0.1/gongji/login/login.html'


data = {'name':'knight',
        'password':'knight'
        }


post_data = urllib.urlencode(data)


try :
    f  = urllib.urlopen(url, post_data)
except:
    print "login success"
else:
    print "failure"


HMW 03-03-2016 11:38 AM

Quote:

Originally Posted by 624867243@qq.com (Post 5509306)
i try to modify it this:(but no matter the username and password is right or wrong) it print sucess.
Code:

#coding:utf-8
import urllib
url = 'http://127.0.0.1/gongji/login/login.html'


data = {'name':'knight',
        'password':'knight'
        }


post_data = urllib.urlencode(data)


try :
    f  = urllib.urlopen(url, post_data)
except:
    print "login success"
else:
    print "failure"


That is because your logic behind Python's exceptions is plain wrong.
Read more here:
http://www.python-course.eu/python3_...n_handling.php

Basically, your code ALWAYS throws an exception, therefore you will ALWAYS get
login success
as a result.

If you need to use Python for this, you're gonna have to learn the language. There is (in my view) an excellent course (free) here:
http://www.pythonlearn.com/html-009/

Best regards,
HMW


All times are GMT -5. The time now is 04:56 PM.