Python---请求最简单PSOT表单登陆页面

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# 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐为波

看着给就好了,学习写作有点累!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值