python实现自动登录后台管理系统

yipeiwu_com5年前Python基础

本文实例为大家分享了python实现自动登录后台管理系统的具体代码,供大家参考,具体内容如下

首先感谢下网络上的各位大神和博主,通过学习各位大神的文章,才实现了该脚本

①首先浏览器运行真是系统通过fiddler抓包,抓取到登录地址(后面的地址和头部信息等都是通过fiddler抓取的)

并获取头信息,header信息里面Accept-Encoding: gzip, deflate去掉吧,免得后面提取页面的url时无法解码,通过代码实现后,这个时候回返回html文本,从文本里面获取下一步要进入的系统的地址,这个地址已经附上cookie即token了,只有经过了这一步请求,才能进行下一步的对系统里面的内容进行操作(之前没有经过这一步,就进行了具体操作的url请求,总是返回重定位到登录界面)。

②然后再进行这个带token的url的请求

③请求完成后即可进行想要的操作了

下面直接上代码,有一些信息是公司的信息,我直接屏蔽换成通用字符串

# coding=utf-8
import urllib.request
import urllib
import http.cookiejar
from database_functions import *
import re
 
 
# 正则表达式匹配规则
regx = re.compile(r'(.*)(href=\")(.*)(\"\starget=.*bms.*)')
 
 
# 定义一个方法用于生成请求头信息,处理cookie
def getopener(head):
 
  cj = http.cookiejar.CookieJar()
  pro = urllib.request.HTTPCookieProcessor(cj)
  opener = urllib.request.build_opener(pro)
  header = []
  for key, value in head.items():
    elem = (key, value)
    header.append(elem)
  opener.addheaders = header
  return opener
 
 
def modify_appeal_status_pass(phone):
 
  index, status = get_appeal_by_phone(phone)
  # 封装头信息,伪装成浏览器
  header = {
    'Connection': 'Keep-Alive',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Accept': 'application/json, text/javascript,text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest',
    'Host': 'xxx.xxx.xxx',
  }
 
  # 登录地址
  login_url = 'http://xxx.xxx.xxx/xx-xxx/login.do'
 
  # request封装cookie和头信息
  opener = getopener(header)
  urllib.request.install_opener(opener)
 
  username = '***' # 你的用户名
  password = '***' # 你的密码
  postdict = {
    'username': username,
    'password': password
  }
 
  # 登录请求
  postdata = urllib.parse.urlencode(postdict).encode('utf-8')
  login_response = urllib.request.Request(login_url, data=postdata, headers=header) #登录系统
  login_webpage = urllib.request.urlopen(login_response)
  # 返回的html页面
  login_data = login_webpage.read().decode()
 
  # 获取系统地址
  bms_url = regx.findall(login_data)[0][2]
  # 请求xxxx
  bms_response = urllib.request.Request(bms_url, headers=header)
  bms_webpage = urllib.request.urlopen(bms_response)
  # 返回的系统后台页面
  bms_data = bms_webpage.read().decode()
 
  # 对xx状态进行修改
  update_url = "http://xxx.xxx.xxx/xxx/xxx/xxx/xxx/{index}".format(index=index)
  update_response = urllib.request.Request(update_url, headers=header)
  update_webpage = urllib.request.urlopen(update_response)
  update_data = update_webpage.read().decode()
  print(update_data) 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

对python 中re.sub,replace(),strip()的区别详解

对python 中re.sub,replace(),strip()的区别详解

1.strip(): str.strip([chars]);去除字符串前面和后面的所有设置的字符串,默认为空格 chars -- 移除字符串头尾指定的字符序列。 st = " he...

win7下python3.6安装配置方法图文教程

win7下python3.6安装配置方法图文教程

win7 python3.6安装教程及环境配置,具体内容如下 由于刚刚重装系统,发现安装得win7专业版存在漏洞,导致Python3不行安装,提示:Python setup failed...

3个用于数据科学的顶级Python库

3个用于数据科学的顶级Python库

Python有许多吸引力,如效率,代码可读性和速度,使其成为数据科学爱好者的首选编程语言。Python通常是希望升级其应用程序功能的数据科学家和机器学习专家的首选。 由于其广泛的用途,P...

浅谈dataframe中更改列属性的方法

在读取文件时将整数变量读成了字符串, 或者需要转换列属性时,通过方法astype Python中 举例: dataframe.numbers=dataframe.numbers.as...

Python中模块(Module)和包(Package)的区别详解

1. 模块(Module) 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护。 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文...