|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import
urllib.request
import
http.cookiejar
import
urllib.parse
# 登录的主页面
hosturl
=
'http://xxxxx'
# 自己填写
# post数据接收和处理的页面(我们要向这个页面发送我们构造的Post数据)
posturl
=
'http://xxxxxxxxxxxxxxx/index.php'
# 从数据包中分析出,处理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(hosturl)
# 构造header,一般header至少要包含一下两项。这两项是从抓到的包里分析得出的。
headers
=
{
'User-Agent'
:
'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0'
,
'Referer'
:
'http://xxxxxxxxxxxx/index.php'
}
# 构造Post数据,他也是从抓大的包里分析得出的。
postData
=
{
"name"
:
'Admin'
,
"password"
:
'xxxxxxx'
,
"autologin"
:
1
,
"enter"
:
"Sign in"
}
# 需要给Post数据编码
postData
=
urllib.parse.urlencode(postData).encode(
'utf-8'
)
# 通过urllib2提供的request方法来向指定Url发送我们构造的数据,并完成登录过程
request
=
urllib.request.Request(posturl, postData, headers)
response
=
urllib.request.urlopen(request)
text
=
response.read().decode()
print
(text)
|
本文转自 295631788 51CTO博客,原文链接:http://blog.51cto.com/hequan/1907543,如需转载请自行联系原作者