python模拟Post请求

前期准备

利用chrome开发者工具截取post请求。

具体代码

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
# -*- coding: utf-8 -*-
import httplib
import re
import time
if __name__=='__main__':
content=open('content.txt','r').read()
num=int(open('num.txt','r').read())
dingnum=0
l=content.split('endendend')
UserAgent="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36"
ContentType="application/x-www-form-urlencoded"
Host="bbs.ichunqiu.com"
ContentLength="151"
Accept="text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
Origin="http://bbs.ichunqiu.com"
matchpath="path:([^~]*)"
matchcookie="cookie:([^~]*)"
matchdata="data:([^~]*)"
starttime=time.time()
print("共顶%d个帖子" % len(l))
# l 帖子组
# c 单个帖子信息
for n in range(num):
n=n+1
print("正在进行第%d次" % n)
print "帖子组信息",l
for c in l:
try:
print ("具体信息:")
path=re.findall(matchpath,c)[0]
cookie=re.findall(matchcookie,c)[0]
data=re.findall(matchdata,c)[0]
print path
print cookie
print data
except:
print(c)
continue
conn=httplib.HTTPConnection('bbs.ichunqiu.com')
conn.request("POST",path,data,{"Host":Host,"Accept":Accept,"Origin":Origin,"User-Agent":UserAgent,"Content-Type":ContentType,"Cookie":cookie,
"Content-Length":ContentLength})
print "连接成功,等待服务器响应"
response=conn.getresponse()
content=response.read()
conn.close()
print "服务器响应如下:"
print(content.decode("utf-8"))
dingnum+=1
time.sleep(15.1)
endtime=time.time()
print("顶贴完成,共顶%d次 耗时:%d秒" % (dingnum,(endtime-starttime)) )

其他配置文件

num.txt

1
5

content.txt

1
2
3
4
5
6
7
path:/forum.php?mod=post&action=reply&fid=42&tid=4406&extra=page%3D1&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1~
cookie:aUZ1_2132_saltkey=J3TSQ8QZ; aUZ1_2132_lastvisit=1458545593; chkphone=acWxNpxhQpDiAchhNuSnEqyiQuDIO0O0O;aUZ1_2132_connect_is_bind=0~
data:message=%7B%3A6_153%3A%7D%7B%3A6_153%3A%7D%7B%3A6_153%3A%7D%7B%3A6_153%3A%7D%7B%3A6_153%3A%7D&posttime=1459494513&formhash=6f4224d7&usesig=1&subject=++~
endendend