LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 03-02-2016, 02:26 AM   #1
624867243@qq.com
Member
 
Registered: Nov 2015
Location: ShenZhen
Posts: 33
Blog Entries: 1

Rep: Reputation: Disabled
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()

Last edited by 624867243@qq.com; 03-02-2016 at 04:31 AM.
 
Old 03-02-2016, 10:41 AM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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
 
Old 03-02-2016, 07:22 PM   #3
624867243@qq.com
Member
 
Registered: Nov 2015
Location: ShenZhen
Posts: 33

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by HMW View Post
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 thisbut 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"

Last edited by 624867243@qq.com; 03-02-2016 at 08:05 PM.
 
Old 03-03-2016, 11:38 AM   #4
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by 624867243@qq.com View Post
i try to modify it thisbut 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
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: How to simulate yes/No in Linux scripts/commands LXer Syndicated Linux News 0 02-23-2016 05:33 AM
How to simulate yes/No in Linux scripts/commands kumarat9pm Syndicated Linux News 0 02-16-2016 09:51 PM
Python for Administrative Scripts zokken Programming 4 02-27-2015 04:45 PM
LXer: Python Scripts as a Replacement for Bash Utility Scripts LXer Syndicated Linux News 1 01-17-2013 08:08 AM
Using shell scripts to simulate commands loOolita Programming 2 11-26-2010 03:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration