Skip to content

Instantly share code, notes, and snippets.

@hibiyasleep
Last active August 29, 2015 14:01
Show Gist options
  • Save hibiyasleep/5a6452543ddb1b4caf14 to your computer and use it in GitHub Desktop.
Save hibiyasleep/5a6452543ddb1b4caf14 to your computer and use it in GitHub Desktop.
typo
# FILE : naver.py
# -*- coding: utf-8 -*-
#
import urllib2, re, sys, thread
from time import time, sleep
from StringIO import StringIO
from gzip import GzipFile
VERSION = '0.2.1'
target = "http://m.search.naver.com/search.naver?sm=mtb_hty.top&where=m&query=선린인터넷고등학교+선생님+고맙습니다"
header = {
"Referer": "m.naver.com",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1944.0 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding": "gzip,deflate,sdch",
"Accept-Language": "ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4",
"Accept-Charset": "utf-8;q=0.7,*;q=0.3"
}
if len(sys.argv) is not 2:
tc = 12
else:
try:
tc = int(sys.argv[1]) - 1
if 1>tc or 16<tc:
raise Exception
except Exception:
exit("argument 1 must be thread count, 2~16")
request = urllib2.Request(target, headers=header)
start = time()
attack_th = []
count = []
init_total = -1
total = -1
def ta(thn=0):
global i
try:
while 1:
f = urllib2.urlopen(request)
d = f.read()
f.close()
b = StringIO(d)
r = GzipFile(fileobj=b).read()
count[thn] += 1
except Exception:
pass
def rx():
global total, init_total
try:
while 1:
f = urllib2.urlopen(request)
d = f.read()
f.close()
b = StringIO(d)
r = GzipFile(fileobj=b).read()
total = int(re.search('(\d+)</strong>등', r).group(1))
if init_total == -1:
init_total = total
count[0] += 1
except Exception:
pass
uth = thread.start_new_thread(rx, tuple())
for k in range(tc):
attack_th.append(thread.start_new_thread(ta, (k, )))
count.append(0)
# screen update
try:
while 1:
e = time() - start
sys.stdout.write("[%12.3f] attacked %d @ %4.2freq/s, global %d @ %6.2freq/s)\r" % (
e,
sum(count),
sum(count)/e,
int(total),
(total-init_total)/e)
)
sys.stdout.flush()
except KeyboardInterrupt:
thread.exit()
print "[ "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment