/src/test_maxnum.py

https://bitbucket.org/chenfuzhi/scan_proxy_ip · Python · 163 lines · 129 code · 31 blank · 3 comment · 30 complexity · ff9d7d32ab1c4acdc3e4f1793fe1a77a MD5 · raw file

  1. #coding:utf-8
  2. from twisted.internet import epollreactor
  3. epollreactor.install()
  4. from twisted.application import internet, service
  5. from twisted.internet import reactor, defer
  6. from twisted.python import log
  7. import setting
  8. import txmongo
  9. import txredisapi
  10. import time
  11. import sys
  12. import socket
  13. import struct
  14. from txmongo import ObjectId
  15. from twisted.internet.endpoints import TCP4ClientEndpoint
  16. from twisted.web import client
  17. import subprocess
  18. from twisted.internet.protocol import Protocol
  19. import signal
  20. import sys
  21. from ExTx.SuperAgent import SuperAgent, BeginningPrinter
  22. from multiprocessing import Process
  23. import os
  24. from common import *
  25. import random
  26. import json
  27. class TestMax:
  28. def __init__(self):
  29. self.srange = [map(strip2int, x) for x in setting.SRANGE]
  30. self.ip_queue = []
  31. self.run_count = 0
  32. self.max_queue = 1000000
  33. self.run_ip = 22437777
  34. self.pre_seeds = 10000
  35. def get_next_ip(self):
  36. try:
  37. self.run_count += 1
  38. return self.ip_queue.pop()
  39. except:
  40. return None
  41. @defer.inlineCallbacks
  42. def start(self):
  43. for i in range(0, 10000):
  44. self.scan_ip()
  45. while 1:
  46. try:
  47. yield self.input_next_ips()
  48. except:
  49. pass
  50. yield wait()
  51. print "wait...", len(self.ip_queue), self.run_count
  52. @defer.inlineCallbacks
  53. def scan_ip(self):
  54. while 1:
  55. ip_addr = self.get_next_ip()
  56. if ip_addr is None:
  57. yield wait(5)
  58. continue
  59. print ip_addr
  60. port = 6666
  61. try:
  62. response = yield test_proxy2(ip_addr, int(port))
  63. except Exception, e:
  64. log.err()
  65. yield wait()
  66. continue
  67. if response is True:
  68. data = {}
  69. data["uri"] = "%s:%s" % (ip_addr, port)
  70. data["time"] = time.strftime("%Y-%m-%d %H:%I:%S")
  71. open("/var/log/proxyip.txt", "a").write("%s:%s" % (ip_addr, int(port)))
  72. print "successfull scan ip:%s" % ret
  73. print ret, len(self.ip_queue), self.run_count
  74. @defer.inlineCallbacks
  75. def input_next_ips(self):
  76. if self.srange is None:
  77. defer.returnValue(None)
  78. try:
  79. for r in self.srange:
  80. while 1:
  81. if len(self.ip_queue) < self.max_queue:
  82. if r[0] <= self.run_ip < r[1]:
  83. add_num = self.pre_seeds -1
  84. if (self.run_ip + add_num) >= r[1]:
  85. add_num = r[1] - self.run_ip -1
  86. for i in range(0, add_num):
  87. ip_addr = intip2str(self.run_ip + i)
  88. self.ip_queue.append(ip_addr)
  89. self.run_ip = self.run_ip + add_num
  90. if r[1] - 1 == self.run_ip:
  91. index = self.srange.index(r)
  92. if len(self.srange) - 1 > index:
  93. self.run_ip = self.srange[index + 1][0]
  94. break
  95. else:
  96. break
  97. else:
  98. break
  99. defer.returnValue(True)
  100. except Exception, e:
  101. log.err(e)
  102. defer.returnValue(False)
  103. @defer.inlineCallbacks
  104. def test_proxy2(ip_addr, port):
  105. agent = SuperAgent(proxy=[ip_addr, port])
  106. response = yield agent.request("GET", 'http://iframe.ip138.com/ic.asp', timeout=10)
  107. if response.code == 200:
  108. defer.returnValue(True)
  109. defer.returnValue(False)
  110. if __name__ == "__main__":
  111. from optparse import OptionParser
  112. parser = OptionParser(usage='usage: %prog [options]')
  113. # commands
  114. parser.add_option('--daemon', dest='daemon', action="store_true", help='run deamon', default=False)
  115. options, args = parser.parse_args()
  116. print options, args
  117. if options.daemon:
  118. try:
  119. # Store the Fork PID
  120. pid = os.fork()
  121. if pid > 0:
  122. print 'PID: %d' % pid
  123. os._exit(0)
  124. except OSError, error:
  125. print 'Unable to fork. Error: %d (%s)' % (error.errno, error.strerror)
  126. os._exit(1)
  127. log.startLogging(open('/var/log/scanipd.log', 'a'))
  128. else:
  129. log.startLogging(sys.stdout)
  130. mon = TestMax()
  131. mon.start()
  132. reactor.run()