You are not logged in.

#1 2016-03-04 12:19:12

Knight
Member
From: ShenZhen/Asia
Registered: 2015-11-19
Posts: 18

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:

<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:

<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?

import urllib
url = 'http://127.0.0.1/gongji/login/login.html'


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


post_data = urllib.urlencode(data)
print post_data

Offline

#2 2016-03-04 12:21:14

Knight
Member
From: ShenZhen/Asia
Registered: 2015-11-19
Posts: 18

Re: how to use python scripts to simulate the login?

below is sql:

/*
Navicat MySQL Data Transfer
 
Source Server         : localhost
Source Server Version : 50540
Source Host           : localhost:3306
Source Database       : logindb
 
Target Server Type    : MYSQL
Target Server Version : 50540
File Encoding         : 65001
 
Date: 2016-03-04 14:21:27
*/
 
SET FOREIGN_KEY_CHECKS=0;
 
-- ----------------------------
-- Table structure for test1
-- ----------------------------
DROP TABLE IF EXISTS `test1`;
CREATE TABLE `test1` (
  `name` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=gbk;
 
-- ----------------------------
-- Records of test1
-- ----------------------------
INSERT INTO `test1` VALUES ('knight', 'knight');

Offline

#3 2016-03-04 13:06:38

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,524
Website

Re: how to use python scripts to simulate the login?

Nowhere in your python code do you actually make the POST request - so of course there is no response.  I'm horrible with python and have never even heard of urllib, but from looking at the documentation for it it seems you'd need to use urlretrieve() to actually make the request to the url.

Also, if this is for any real use, you should not store passwords in plain text - but that is a separate topic.

EDIT: you're also using the wrong url in the python code.  The python code doesn't need to do anything with the login.html, it should POST to login1.html.  And I'm pretty sure you shouldn't urlencode POST data, that's for parameters passed in the url of a GET request.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2016-03-07 07:28:36

Knight
Member
From: ShenZhen/Asia
Registered: 2015-11-19
Posts: 18

Re: how to use python scripts to simulate the login?

Trilby wrote:

Nowhere in your python code do you actually make the POST request - so of course there is no response.  I'm horrible with python and have never even heard of urllib, but from looking at the documentation for it it seems you'd need to use urlretrieve() to actually make the request to the url.

Also, if this is for any real use, you should not store passwords in plain text - but that is a separate topic.

EDIT: you're also using the wrong url in the python code.  The python code doesn't need to do anything with the login.html, it should POST to login1.html.  And I'm pretty sure you shouldn't urlencode POST data, that's for parameters passed in the url of a GET request.

thank you  i have solveed it,i must post to login1.html

Last edited by Knight (2016-03-07 07:28:47)

Offline

#5 2016-03-07 11:40:16

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,524
Website

Re: how to use python scripts to simulate the login?

Please mark your thread as [SOLVED], and feel free to go back to all your other threads and mark them as [SOLVED] if they are too.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

Board footer

Powered by FluxBB