# coding=utf-8
import urllib
import hashlib
import http.client
import http.cookiejar
import http.cookies #post数据接收和处理的页面(我们要向这个页面发送我们构造的Post数据)
posturl = 'http://xxxxxxxxxxxx/xxxxxx.do' #从提交数据包中分析出,处理post请求的url #设置一个cookie处理器,它负责从服务器下载cookie到本地,并且在发送请求时带上本地的cookie
cj = http.cookiejar.LWPCookieJar()
cookie_support = urllib.request.HTTPCookieProcessor(cj)
opener = urllib.request.build_opener(cookie_support, urllib.request.HTTPHandler)
urllib.request.install_opener(opener)
#打开登录主页面(他的目的是从页面下载cookie,这样我们在再送post数据时就有cookie了,否则发送不成功,当然有的网站也不对此做要求,那就省去这一步)
h = urllib.request.urlopen('http://xxxxx.edu.cn/ntms/index.do') #构造header,一般header至少要包含一下两项。这两项是从抓到的包里分析得出的。 headers = {'User-Agent': ' Mozilla/5.0 (Windows NT 6.1; rv:32.0) Gecko/20100101 Firefox/32.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': ' zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3',
# 'Accept-Encoding': 'gzip, deflate',#大坑。加入后要手动对gzip解压后才会有可识别的内容
'Referer': 'http://xxxxx/userlogin.jsp?reason=login',
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded',
}
#构造Post数据,也是从抓大的包里分析得出的。 postData = {'j_username': 'xxxxxxxx',
'j_password': 'fc5700426c8d6a74b0cdf987b63922d3', #你的密码,密码可能是明文传输也可能是密文,如果是密文需要调用相应的加密算法加密,需查看js代码
#才知我就直接硬编码了
'save_cookie': 'none'
} #需要给Post数据编码
postData = urllib.parse.urlencode(postData).encode(encoding='UTF-8') #通过urllib.request提供的Request方法来向指定Url发送我们构造的数据,并完成登录过程
request = urllib.request.Request(posturl, postData, headers)
response = urllib.request.urlopen(request)
text = response.read() #测试是否成功登陆,这里是请求用户信息,如果成功登陆,那么cookie发到这个页面之后就会返回用户资料,否则提示错误,也知道自己登陆失败了
headers1 = {'User-Agent': ' Mozilla/5.0 (Windows NT 6.1; rv:32.0) Gecko/20100101 Firefox/32.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': ' zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3',
# 'Accept-Encoding': 'gzip, deflate',#大坑。加入后要手动对gzip解压后才会有可识别的内容
'Referer': 'http://xxxx.edu.cn/ntms/userLogin.jsp?reason=logout',
'Connection': 'keep-alive',
'Content-Length': '',
'Content-Type': ' application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest'
}
request = urllib.request.Request('http://xxxx.edu.cn/action/getCurrentUserInfo.do', None, headers1)
response = urllib.request.urlopen(request)
text = response.read()
#打印回应的内容
print(str(text, encoding='utf-8', errors='strict'))

post登录资料备份的更多相关文章

  1. 苹果手机 iTunes 资料备份到另一手机

    百度教程 https://jingyan.baidu.com/article/d621e8da332e602865913f8e.html 直接使用iTunes将老手机的资料备份, (可能需要关闭手机定 ...

  2. swift使用查阅资料备份3

    自主学习之RxSwift(二) -----flatMap https://blog.csdn.net/chelongfei/article/details/50995603 RxSwift 系列(九) ...

  3. swift使用查阅资料备份2

    Swift3.0朝圣之路-Then协议库-绝妙的初始化方式 https://www.jianshu.com/p/6cc1e21df6ac DisposeBag http://southpeak.git ...

  4. find tar 压缩第一层目录,用于资料备份。

    find *.tar.gz -exec tar zxvf '{}' \;//查找当前目录下的.tar.gz 的文件的 并发送给后面的命令执行find . -maxdepth 1 //查找当前目录下的文 ...

  5. mysql的登录和备份

    mysql -h xxx.xxx.xxx.xxx -u root -p -h 指出服务器名 若是localhost则是指本机 -u 指出用户名 root是默认的最高权限用户名 -p指出密码 不填就指密 ...

  6. eclipse报错资料备份

    一. eclipse中初始化控件出错 在添加 TextView myTextView=(TextView)this.findViewById(R.id.myTextView); Button myBu ...

  7. 【React 资料备份】React Hook

    Hooks是React16.8一个新增项,是我们可以不用创建class组件就能使用状态和其他React特性 准备工作 升级react.react-dom npm i react react-dom - ...

  8. 【React 资料备份】React v16.3之后的生命周期

    React v16.4 的生命周期图 React v16.4 的生命周期 变更缘由 原来(React v16.0前)的生命周期在React v16推出的Fiber之后就不合适了,因为如果要开启asyn ...

  9. spring 学习资料备份

    易百教程  https://www.yiibai.com/spring/spring-autowiring-by-name.html

随机推荐

  1. Maven公共仓库/镜像站收集及使用技巧

    查询: 1.http://search.maven.org/ 2.https://mvnrepository.com/ 3.https://maven-repository.com/ 4.(阿里云镜像 ...

  2. java读取大文本文件

    原文:http://blog.csdn.net/k21325/article/details/53886160 小文件当然可以直接读取所有,然后放到内存中,但是当文件很大的时候,这个方法就行不通了,内 ...

  3. 咏南 DELPHI DATASNAP LINUX中间件

    咏南 DATASNAP LINUX中间件   咏南 DATASNAP LINUX中间件,一套源码,同时支持WINDOWS和LINUX操作系统. 基于DELPHI 10.2 TOKYO开发 使用FIRE ...

  4. [TypeScript] Use TypeScript’s never Type for Exhaustiveness Checking

    TypeScript 2.0 introduced a new primitive type called never, the type of values that never occur. It ...

  5. C - The C Answer (2nd Edition) - Exercise 1-8

    /* Write a program to count blanks, tabs, and newlines. */ #include <stdio.h> /* count blanks, ...

  6. JS 获取form表单的所有数据

    在HTML中用js获取通过GET.POST方法(就是在网址后加?a=b&c=d之类)传过来的表单值. 针对大家常用的获取表单方式,很多时候都是在重复的写一些代码,今天给大家贴出来的代码可以作为 ...

  7. IOS-Storyboard控制器切换之TabBar(3)

    TabBar与Push相反,他以底部为导航 以头部为内容区域,如图: 打开storyboard文件,创建一个UITabBarController文件到画板中,默认带了2个 UIViewControll ...

  8. ZOJ Design the city LCA转RMQ

    Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...

  9. linux进程间通信——netlink【转】

    本文转载自:http://blog.csdn.net/wangyuling1234567890/article/details/21561457 今天在看用户态与内核态通信相关东西时,发现了关于net ...

  10. android 反编译 for mac

    android反编译现在来说的话很方便. windows上有不好好用的工具,当然我比较喜欢dex2jar 这个是比较好用的,打开他的文件目录会发现,里面有很多.sh .bat文件 那也就是说在wind ...