应同学要求,帮忙写了一个发帖的东西,原本以为很简单的,想让他自己写的。。。因为他也有在学python,这个可以很好地锻炼他,所以,一开始没答应。结果今天早上感觉特别无聊,就开始默默地帮他写,写了一半,结果发现这嘎达还是有点难度的 ,不是简简单单的urlopen就可以搞定的。。。
后来做到一半的时候和他聊天,最后敲定一顿牛排作为回报,然后就满心欢喜地开始码代码,但是,仔细一想,他有那么大方?!关键是答应的很快、、、然后就问了原因,结果原来是他妈在他旁边坐着,答应也是他妈答应的。。。瞬间捂脸- -|| 表示好丢脸。。。
然后一个下午的垒代码,终于搞定
不过交付过程中bug乱入。。。
但,还是解决了。。。
_________________________________________________________________________________
后来表示要验证码神马的,然后发现在自己电脑上根本不需要验证码,果断觉得是那边特判了之类的。
于是乎,就决定搞一个代理,捣鼓了半个下午,终于搞定。。。
然后,交付使用的时候,居然不用代理也发成功了- -|| 这是有多疼啊。。。。
只贴源代码,至于应用程序就不给了- -
Main.py
#_*_encoding:utf-8_*_
__author__ = 'glcsnz123'
import wx, thread, threading
import time
import os
import ReplyDeal, CardDeal, random
def FindFileList(name='card'):
filelist = os.listdir(r".\data")
anslist = []
for fname in filelist:
if fname.startswith(name):
fname = '.\\data\\' + fname
anslist.append(fname)
return anslist
def FindAccount():
try:
f = open(".\\data\\account.in", "r")
acc = f.readlines()
except Exception, e:
print "open error" + str(e)
return []
return acc
class GUIFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent=None, id=-1, title="AutoSendCard", size=(450, 300))
self.panel = wx.Panel(self, -1)
#create the menubar
self.CreateMenuBar()
#static args
self.flag = True
self.rwtime = 1800
self.cwtime = 1800
#create the button
self.but1 = wx.Button(self.panel, -1, u"自动回复", pos=(100, 70), size=(100, 50))
self.but2 = wx.Button(self.panel, -1, u"自动发帖", pos=(250, 70), size=(100, 50))
self.but_can = wx.Button(self.panel, -1, u"取消", pos=(170, 140), size=(100, 50))
self.but_can.Show(False)
#create the static text
self.leftext = wx.StaticText(self.panel, -1, "", pos=(200, 200))
#Bind the action
self.Bind(wx.EVT_BUTTON, self.AutoReply, self.but1)
self.Bind(wx.EVT_BUTTON, self.AutoCard, self.but2)
self.Bind(wx.EVT_BUTTON, self.Cancel, self.but_can)
def Cancel(self, event):
self.flag = False
self.but1.Show(True)
self.but2.Show(True)
self.but_can.Show(False)
self.leftext.SetLabel("")
def CreateMenuBar(self):
menubar = wx.MenuBar()
menu1 = wx.Menu()
menu2 = wx.Menu()
menubar.Append(menu1, u"选项")
menubar.Append(menu2, u"参数设定")
menu1_it1 = menu1.Append(-1, u"自动回复", u"自动回复指定的帖子")
menu1_it2 = menu1.Append(-1, u"自动发帖", u"随即选取账号,自动发帖")
menu2_it1 = menu2.Append(-1, u"【回复】时间间隔", u"自动【回复】的时间间隔设定,单位为秒")
menu2_it2 = menu2.Append(-1, u"【发帖】时间间隔", u"自动【发帖】的时间间隔设定,单位为秒")
self.mn2_pro = menu2.Append(-1, u"【启用】代理", u"【是否】启用代理,对代理进行设置")
self.SetMenuBar(menubar)
self.Bind(wx.EVT_MENU, self.AutoReply, menu1_it1)
self.Bind(wx.EVT_MENU, self.AutoCard, menu1_it2)
self.Bind(wx.EVT_MENU, self.SetReplyArgs, menu2_it1)
self.Bind(wx.EVT_MENU, self.SetCardArgs, menu2_it2)
self.Bind(wx.EVT_MENU, self.SetProxy, self.mn2_pro)
#create toolbar
self.CreateStatusBar()
def SetProxy(self, event):
#print dir(self.mn2_pro)
if self.mn2_pro.GetLabel() == u"【启用】代理":
self.mn2_pro.SetItemLabel(u"【关闭】代理")
ReplyDeal.bool_proxy = True
CardDeal.bool_proxy = True
else:
self.mn2_pro.SetItemLabel(u"【启用】代理")
CardDeal.bool_proxy = False
ReplyDeal.bool_proxy = False
def SetReplyArgs(self, event):
ted = wx.TextEntryDialog(self, u"请输入回复时间间隔(单位:秒)", caption=u"设定参数",
style=wx.OK | wx.CANCEL)
if ted.ShowModal() == wx.ID_OK:
try:
self.rwtime = int(ted.GetValue())
except Exception, e:
self.rwtime = 1800
else:
self.rwtime = 1800
def SetCardArgs(self, event):
ted = wx.TextEntryDialog(self, u"请输入发帖时间间隔(单位:秒)", caption=u"设定参数",
style=wx.OK | wx.CANCEL)
if ted.ShowModal() == wx.ID_OK:
try:
self.cwtime = int(ted.GetValue())
except Exception, e:
self.cwtime = 1800
else:
self.cwtime = 1800
def GetTopicid(self):
ted = wx.TextEntryDialog(self, u"请输入您要自动回复的帖子的topicid的值:\n(ps.topicid的意思请参见readme.txt)", caption=u"设定topicid",
style=wx.OK | wx.CANCEL)
if ted.ShowModal() == wx.ID_OK:
return str(ted.GetValue()).split(",")
else:
return []
def RandomAccount(self):
t = random.randint(0, len(self.acc) - 2)
#print t
g = self.acc[t]
del self.acc[t]
self.acc.append(g)
return g.split(' ')
def Waiting(self, waitime=1800):
i = waitime#set the time
while i > 0:
time.sleep(1)
i -= 1
self.leftext.SetLabel(str(i / 60) + " : " + str(i % 60))
self.Refresh()
if self.flag == False:
print "exit the thread"
self.leftext.SetLabel("")
thread.exit()
def AutoSendReply(self):
t = random.randrange(len(self.flist))
t %= len(self.flist)
while True:
for topicid in self.topid:
if topicid == "":
if len(self.topid) == 1:
thread.exit()
else:
break
content = open(self.flist[t]).read()
acnt, passwd = self.RandomAccount()
ReplyDeal.Login(content, "RE:", topicid, acnt, passwd)
t += 1
t %= len(self.flist)
self.Waiting(self.rwtime)
def AutoReply(self, event):
self.flist = FindFileList("reply")
self.acc = FindAccount()
self.flag = True
self.topid = self.GetTopicid()
#print topid
self.but1.Show(False)
self.but2.Show(False)
self.but_can.Show(True)
thread.start_new_thread(self.AutoSendReply, ())
def AutoSendCard(self):
t = random.randrange(len(self.flist))
t %= len(self.flist)
while True:
acnt, passwd = self.RandomAccount()
f = open(self.flist[t])
content = f.read().split('\n')
titl = content[0]
mesg = '\n'.join(content[1:])
#print titl.decode("utf-8")
#print mesg.decode("utf-8")
#print acnt, passwd
CardDeal.Login(mesg, titl, acnt, passwd)
self.Waiting(self.cwtime)
t += 1
t %= len(self.flist)
def AutoCard(self, event):
self.flist = FindFileList("card")
self.acc = FindAccount()
self.flag = True
#print self.acc
self.but1.Show(False)
self.but2.Show(False)
self.but_can.Show(True)
thread.start_new_thread(self.AutoSendCard, ())
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = GUIFrame()
frame.Show(True)
app.MainLoop()
CardDeal.py
#_*_encoding:utf-8_*_
__author__ = 'glcsnz123'
import urllib, urllib2
import cookielib
import time
import ProxyDeal
bool_proxy = False
def Login(mesg=None, titl=None, name='glcsnz1234', passwd='a123456'):
print "I am try to login..."
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
if bool_proxy == True:
print "I am setting proxy..."
hostss, ptype = ProxyDeal.RandomProxy()
print "args are: ", hostss, ptype
proxy_handler = urllib2.ProxyHandler({ptype: hostss})
opener.add_handler(proxy_handler)
opener.addheaders = [('User-agent', 'Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.1)')]
urllib2.install_opener(opener)
path = "http://bbs.cnool.net/login.aspx"
post_data = urllib.urlencode({"username": name, "password": passwd})
conn = urllib2.Request(path, post_data)
urllib2.urlopen(conn).read()
print "end of login..."
#send the reply
SendCard(mesg, titl)
#SendCard()
def SendCard(mesg='this is a test reply 2', titl='RE'):
print "I am create the post data..."
try:
print titl.decode("gbk")
except Exception, e:
print titl
post_data = urllib.urlencode(
{"message": mesg, "title": titl, "activityId": "0", "anonymous": "false",
'attachments': '[]', 'fid': '43', 'grabHouse': "0", 'iconid': '0',
'isOriginal': '1', 'multiple': 'on', 'htmlon': '0', 'noticePerm': '0', 'notifyPolice': '0',
'postbytopictype': '1',\
'posttime': time.strftime("%Y-%m-%d") + " " + time.strftime("%H:%M:%S"), "topicsubmit": "发新主题", 'wysiwyg': '1',\
'hidename': '0', "typeid": "0", "submitType": "1", "threadcategory": "0", "tid": "0", "typeid": "0",
"sessionId": "AAfKz8r20kNXwqL/Rmav+CFSN9QlQs+of548LS1eh5K2gCsUjQ+HhfX+scGzMULDjfNivKLk9JD3Z2VD1PyKqRF2YNvORiVq5n8AQuhRgIc="
});
#print post_data
print "end of the post..."
headers = {"Accept": "application/json, text/javascript, */*; q=0.01", "Accept-Encoding": "gzip, deflate",
"Accept-Language": " zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Host": "bbs.cnool.net", "Referer": "http://bbs.cnool.net/cposttopic.aspx?forumid=43&forumpage=1",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0",
"X-Requested-With": "XMLHttpRequest", "Host": "bbs.cnool.net"};
print "begin request..."
pst = urllib2.Request("http://bbs.cnool.net/util/post.aspx?forumid=43&forumpage=1&type=", headers=headers);
print "begin urlopen..."
pset = urllib2.urlopen(pst, post_data)
#print pst, post_data;
html = pset.read()
print "read end."
print html.decode("utf-8")
if __name__ == '__main__':
Login()
#http://bbs.cnool.net/util/post.aspx?forumid=43&forumpage=1&type=
ReplyDeal.py
#-*- coding: UTF-8 -*-
__author__ = 'glcsnz123'
import urllib, urllib2
import cookielib
import time
import ProxyDeal
bool_proxy = False
def Login(mesg=None, titl=None, topicid=None, name='glcsnz1234', passwd='a123456'):
print "I am try to login..."
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
if bool_proxy == True:
print "I am setting proxy..."
hostss, ptype = ProxyDeal.RandomProxy()
print "args are: ", hostss, ptype
proxy_handler = urllib2.ProxyHandler({ptype: hostss})
opener.add_handler(proxy_handler)
opener.addheaders = [('User-agent', 'Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.1)')]
urllib2.install_opener(opener)
path = "http://bbs.cnool.net/login.aspx"
post_data = urllib.urlencode({"username": name, "password": passwd})
conn = urllib2.Request(path, post_data)
urllib2.urlopen(conn).read()
print "end of login..."
#send the reply
SendReply(mesg, titl, topicid)
#SendReply()
def SendReply(mesg='this is a test reply 2', titl='Re', topicid='104121830'):
print "I am create the post data..."
try:
print titl.decode("gbk")
except Exception, e:
print titl
post_data = urllib.urlencode(
{"message": mesg, "title": titl, "emailnotify": "on", "postreplynotice": "true",
'submitType': '1', 'threadcategory': '0', 'fid': '43', 'tid': topicid, 'anonymous': 'false',
'noticePerm': '0', 'attachments': '[]', 'htmlon': '0', 'iconid': '0', 'postbytopictype': '1',\
'posttime': time.strftime("%Y-%m-%d") + " " + time.strftime("%H:%M:%S"), "topicsubmit": "回复主题", 'wysiwyg': '1',\
'hidename': '0',
"sessionId": "AAfKz8r20kNXwqL/Rmav+CFSN9QlQs+of548LS1eh5K2gCsUjQ+HhfX+scGzMULDjfNivKLk9JD3Z2VD1PyKqRF2YNvORiVq5n8AQuhRgIc="
});
#print post_data
print "end of the post..."
headers = {"Accept": "application/json, text/javascript, */*; q=0.01", "Accept-Encoding": " gzip, deflate",
"Accept-Language": "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Host": "bbs.cnool.net",
"Referer": "http://bbs.cnool.net/cpostreply.aspx?topicid=" + topicid + "&forumpage=1",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0",
"X-Requested-With": "XMLHttpRequest"};
print "begin request..."
pst = urllib2.Request("http://bbs.cnool.net/util/reply.aspx?topicid=" + topicid + "&forumpage=1&t=quickreply",
headers=headers);
print "begin urlopen..."
pset = urllib2.urlopen(pst, post_data)
#print pst, post_data;
html = pset.read()
print "read end."
print html.decode("utf-8")
if __name__ == '__main__':
Login()
#"Cookie": "cnool_
# "Content-Length": "603",
ProxyDeal.py
#_*_encoding:utf-8_*_
__author__ = 'lenovo'
import random
import cookielib, urllib, urllib2
def RandomProxy():
try:
f = open(r".\data\proxy.in")
ant = (f.read()).split("\n")
#print random.randrange(len(ant))
ant = ant[random.randrange(len(ant))]
except Exception, e:
ant = ""
finally:
return ant.split(" ")
def Login(mesg=None, titl=None, name='glcsnz1234', passwd='a123456'):
print "I am try to login..."
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('User-agent', 'Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.1)')]
urllib2.install_opener(opener)
path = "http://user.qzone.qq.com/425797155"
conn = urllib2.Request(path)
conn.set_proxy("111.161.30.224:80", "http")
html = urllib2.urlopen(conn).read()
print html.decode("utf-8")
print "end of login..."
#send the reply
#SendCard(mesg, titl)
#SendCard()
if __name__ == '__main__':
f=open(r".\data\proxy.in",'w')
f.write(r"http://111.161.30.224:80 http")
f.close()
#print RandomProxy()