Re: cssa

From Unique Pintail, 4 Months ago, written in Python, viewed 59 times. This paste is a reply to cssa from Mature Owl - view diff
URL https://code.nat.moe/view/b0679c1b Embed
Download Paste or View Raw
  1. #!/usr/bin/env python3
  2. import argparse, requests
  3. from http.server import HTTPServer, BaseHTTPRequestHandler
  4.  
  5. CLIENT_HEADERS = {
  6.     'Connection': 'Keep-Alive',
  7.     'Content-Type': 'application/octet-stream',
  8. }
  9.  
  10. class Handler(BaseHTTPRequestHandler):
  11.     protocol_version = 'HTTP/1.1'
  12.  
  13.     def do_POST(self):
  14.         self.rfile.read(int(self.headers.get('Content-Length')))
  15.  
  16.         self.send_response(200)
  17.         self.send_header('Connection', 'keep-alive')
  18.         self.send_header('Content-Type', 'application/octet-stream')
  19.         self.send_header('Content-Length', '0')
  20.         self.end_headers()
  21.        
  22.         return
  23.  
  24. parser = argparse.ArgumentParser()
  25. parser.add_argument('--client', help = 'client mode', action = 'store_true')
  26. parser.add_argument('--host', type = str, help = 'remote/listen host', required = True)
  27. parser.add_argument('--port', type = int, help = 'remote/listen port', required = True)
  28. parser.add_argument('--size', type = int, help = '(client mode) content length')
  29. parser.add_argument('--count', type = int, help = '(client mode) number of posts (0 = unlimited)')
  30. ns = parser.parse_args()
  31.  
  32. if ns.client and (ns.size is None or ns.count is None):
  33.     parser.error('--client requires --size and --count')
  34.  
  35. if ns.client:
  36.     data = bytes(0 for _ in range(1, ns.size))
  37.     client = requests.Session()
  38.     count = 0
  39.    
  40.     while count < ns.count or ns.count == 0:
  41.         client.post(url = 'http://{}:{}/mqipt'.format(ns.host, ns.port), data = data, headers = CLIENT_HEADERS)
  42.         count += 1
  43.         if count % 100 == 0:
  44.             print('client: {} posts sent'.format(count))
  45. else:
  46.     server = HTTPServer((ns.host, ns.port), Handler)
  47.     server.serve_forever()

Reply to "Re: cssa"

Here you can reply to the paste above

captcha