Python—请求最简单的页面登陆页面
正常使用工具获取数据包
1、正确的用户名与密码
2、错误的用户名与密码
=================================================================
python源码—用户名与密码都正确:
root@kali:~/python# cat login1.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
import urllib
import urllib2
url = 'http://192.168.40.239/xsser/index.php?do=login&act=submit'#请求的登陆的url地址
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'#浏览器模式
values = {'user' : 'xwb',
'pwd' : 'qwe123456',
}#请求的URL地址,post表单数据信息,用户名与密码
headers = { 'User-Agent' : user_agent }#请求的头部信息
data = urllib.urlencode(values)#请求post表单数据
req = urllib2.Request(url, data, headers)#请求数据
response = urllib2.urlopen(req)#打开请求的数据
the_page = response.read()#读取并缓存请求到的数据
print the_page#打印请求到的页面
print "你请求到页面数据包为%d字节" %len(the_page)#计算请求到的页面数据大小
if len(the_page) == 1048:#数据包大小等于1048就是成功
print "loing success!!!"
else:
print "login fail!!!!"
root@kali:~/python#
有注释#print the_page#打印请求到的页面
root@kali:~/python# python login1.py
你请求到页面数据包为1048字节
loing success!!!
root@kali:~/python#
无注释#print the_page#打印请求到的页面
root@kali:~/python# python login1.py
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>登录成功</title>
<link rel="stylesheet" href="http://192.168.40.239/xsser/themes/default/style/screen.css" type="text/css">
<link rel="stylesheet" href="http://192.168.40.239/xsser/themes/default/style/print.css" type="text/css">
<!--[if lt IE 8]><link rel="stylesheet" href="http://192.168.40.239/xsser/themes/default/style/ie.css" type="text/css"><![endif]-->
<link rel="stylesheet" href="http://192.168.40.239/xsser/themes/default/style/style.css" type="text/css">
<script type="text/javascript">
setTimeout("location.href='http://192.168.40.239/xsser/index.php'",500);
</script>
</head>
<body>
<div class="container">
<div style="margin:50px auto 0 auto" class="success">登录成功 <a href="http://192.168.40.239/xsser/index.php">返回</a></div>
</div>
</body>
</html>
你请求到页面数据包为1048字节
loing success!!!
root@kali:~/python#
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
python源码—用户名或密码不正确:
root@kali:~/python# cat login1.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
import urllib
import urllib2
url = 'http://192.168.40.239/xsser/index.php?do=login&act=submit'#请求的登陆的url地址
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'#浏览器模式
values = {'user' : 'xwb',
'pwd' : 'xxxxxqwe123456',
}#请求的URL地址,post表单数据信息,用户名与密码
headers = { 'User-Agent' : user_agent }#请求的头部信息
data = urllib.urlencode(values)#请求post表单数据
req = urllib2.Request(url, data, headers)#请求数据
response = urllib2.urlopen(req)#打开请求的数据
the_page = response.read()#读取并缓存请求到的数据
print the_page#打印请求到的页面
print "你请求到页面数据包为%d字节" %len(the_page)#计算请求到的页面数据大小
if len(the_page) == 1048:#数据包大小等于1048就是成功
print "loing success!!!"
else:
print "login fail!!!!"
root@kali:~/python#
请求页面
root@kali:~/python# python login1.py
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>登录失败,请检查用户/邮箱或密码</title>
<link rel="stylesheet" href="http://192.168.40.239/xsser/themes/default/style/screen.css" type="text/css">
<link rel="stylesheet" href="http://192.168.40.239/xsser/themes/default/style/print.css" type="text/css">
<!--[if lt IE 8]><link rel="stylesheet" href="http://192.168.40.239/xsser/themes/default/style/ie.css" type="text/css"><![endif]-->
<link rel="stylesheet" href="http://192.168.40.239/xsser/themes/default/style/style.css" type="text/css">
<script type="text/javascript">
setTimeout("location.href='http://192.168.40.239/xsser/index.php?do=login'",500);
</script>
</head>
<body>
<div class="container">
<div style="margin:50px auto 0 auto" class="error">登录失败,请检查用户/邮箱或密码 <a href="http://192.168.40.239/xsser/index.php?do=login">返回</a></div>
</div>
</body>
</html>
你请求到页面数据包为1128字节
login fail!!!!
root@kali:~/python#