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

yipeiwu_com7年前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语言中with as的用法使用详解

With语句是什么? 有一些任务,可能事先需要设置,事后做清理工作。对于这种场景,Python的with语句提供了一种非常方便的处理方式。一个很好的例子是文件处理,你需要获取一个文件句...

python文本数据处理学习笔记详解

python文本数据处理学习笔记详解

最近越发感觉到限制我对Python运用、以及读懂别人代码的地方,大多是在于对数据的处理能力。 其实编程本质上就是数据处理,怎么把文本数据、图像数据,通过python读入、切分等,变成一个...

在python 中实现运行多条shell命令

使用py时可能需要连续运行多条shell 命令 1. # coding: UTF-8 import sys reload(sys) sys.setdefaultencoding('u...

Python求两个圆的交点坐标或三个圆的交点坐标方法

计算两个圆的交点 代码如下: # -*- coding: utf-8 -*- import math import numpy as np def insec(p1,r1,p2,r2...

django 邮件发送模块smtp使用详解

前言 在Python中已经内置了一个smtp邮件发送模块,Django在此基础上进行了简单地封装,让我们在Django环境中可以更方便更灵活的发送邮件。 所有的功能都在django.co...