Repository navigation
Expand file tree
/
Copy pathmodules.py
More file actions
160 lines (124 loc) · 4.57 KB
/
Copy pathmodules.py
File metadata and controls
160 lines (124 loc) · 4.57 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
###########################datetime###############################
from datetime import datetime
now = datetime.now()
print(now)
#datetime转换为timestamp
dt = datetime(2015, 4, 19, 12, 20)
print(dt.timestamp())
#timestamp转换为datetime
t = 1429417200.0
print(datetime.fromtimestamp(t))
#timestamp也可以直接被转换到UTC标准时区的时间:与北京时间差8小时
print(datetime.utcfromtimestamp(t))
#str转换为datetime
cday = datetime.strptime('2015-6-1 18:19:59', '%Y-%m-%d %H:%M:%S')
print(cday)
#datetime转换为str
now = datetime.now()
print(now.strftime('%Y-%m-%d %H:%M:%S'))
print(now.strftime('%A, %B %d %H:%M'))
#关于时间格式的转化 https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior
###########################collections###############################
teststr = "dsklsdjflks aspdfjpqwef jhywednbskdl;e frnoodf kdkja lkjfgg;rkfg jkqrlk"
result = {}
for ch in teststr:
if ch == ' ':
continue
result[ch] = result.get(ch, 0) + 1;
print(result)
print(result.items())
print(sorted(result.items()))
print(sorted(result.items(),key=lambda x:x[1]))
print(sorted(result.items(),key=lambda x:x[1],reverse=True))
print(sorted(result.items(),key=lambda x:x[1],reverse=True)[:3])
from collections import Counter
res = Counter()
for ch in teststr:
if ch == ' ':
continue
res[ch] = res[ch] + 1
print(res)
print(sorted(res.items(),key=lambda x:x[1],reverse=True)[:3])
###########################hashlib###############################
import hashlib
md5 = hashlib.md5()
md5.update('how to use md5 in python hasnlib'.encode('utf-8'))
print(md5.hexdigest())
md5 = hashlib.md5()
md5.update('how to use md5 in python hasnlib?'.encode('utf-8'))
print(md5.hexdigest())
#对md5加盐,增加破解难度md5(message + salt)
slat = 'md5salt'
str= 'how to use md5 in python hasnlib?'
md5 = hashlib.md5()
md5.update(str.encode('utf-8'))
print(md5.hexdigest())
md5 = hashlib.md5()
md5.update(str.encode('utf-8'))
print(md5.hexdigest())
md5 = hashlib.md5()
md5.update('how to use md5 in python hasnlib?md5salt'.encode('utf-8'))
print(md5.hexdigest())
md5 = hashlib.md5()
md5.update((str+slat).encode('utf-8'))
print(md5.hexdigest())
#使用随机验证码,(随机盐)
import hmac
message = b'how to use md5 in python hasnlib?'
key = b'md5salt'
h = hmac.new(message, key, digestmod = 'MD5')
print(h.hexdigest())
h = hmac.new(str.encode('utf-8'), slat.encode('utf-8'), digestmod = 'MD5')
print(h.hexdigest())
###########################urllib###############################
from urllib import request
with request.urlopen("http://apilocate.amap.com/position?accesstype=0&cdma=0&key=8eca96227a6489db9af57d01edfe27cc&bts=460,04,6257,27781411,-95") as f:
data = f.read()
print("Status",f.status, f.reason)
for k,v in f.getheaders():
print('%s:%s' % (k,v))
print('Data:',data.decode('utf-8'))
from urllib import request,parse
print("Login to weibo.cn")
# email = input('Email:')
# passwd = input('Password:')
email = 'test'
passwd = 'test'
login_data = parse.urlencode(
[
('username',email),
('password',passwd),
('entry','mweibo'),
('client_id',''),
('savestate','1'),
('ec',''),
('pagerefer', 'https://passport.weibo.cn/signin/welcome?entry=mweibo&r=http%3A%2F%2Fm.weibo.cn%2F')
]
)
req = request.Request('https://passport.weibo.cn/sso/login')
req.add_header('Origin', 'https://passport.weibo.cn')
req.add_header('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25')
req.add_header('Referer', 'https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F')
with request.urlopen(req, data=login_data.encode('utf-8')) as f:
print('Status:',f.status, f.reason)
for k,v in f.getheaders():
print('%s,%s' % (k,v))
print('Data:',f.read().decode('utf-8'))
###########################requests###############################
import requests
r = requests.get('https://www.douban.com/')
print('Status:',r.status_code, r.reason)
# print(r.text)
print(r.url)
print(r.encoding)
r = requests.get('https://www.douban.com/serrch', params={'q':'python'})
print(r.url)
print(r.content)
#requests的方便之处还在于,对于特定类型的响应,例如JSON,可以直接获取:
r = requests.get("http://apilocate.amap.com/position?accesstype=0&cdma=0&key=8eca96227a6489db9af57d01edfe27cc&bts=460,04,6257,27781411,-95")
print('text:',r.text)
print('json:',r.json())
h = r.headers
print(h)
for k, v in h.items():
print(k,': ', v)