Untitled

From Scorching Motmot, 4 Months ago, written in Plain Text, viewed 56 times.
URL https://code.nat.moe/view/aa4d0213 Embed
Download Paste or View Raw
  1. #!/usr/bin/env python3
  2.  
  3. import socket, argparse, select
  4.  
  5. REQUEST = 'POST http://{host}:{port}/mqipt HTTP/1.1\r\nHost: {host}:{port}\r\nUser-Agent: WebSphere-MQ-internet-pass-thru/2.1.0.4\r\nContent-Length: {len}\r\nAccept-Encoding: identity\r\nConnection: Keep-Alive\r\nContent-Type: application/octet-stream\r\nCookie: MQIPTSessionType=FullDuplex-S; MQIPTSessionId=test-{count}\r\nProxy-Connection: Keep-Alive\r\n\r\n'
  6. REPLY = 'HTTP/1.1 200 OK\r\nCache-Control: no-cache, no-transform\r\nConnection: Keep-Alive\r\nContent-Type: application/octet-stream\r\nCookie: MQIPTSessionId=test\r\nProxy-Connection: Keep-Alive\r\nServer: WebSphere-MQ-internet-pass-thru/2.1.0.4\r\nDate: Tue, 22 Feb 2022 22:22:22 GMT\r\nContent-Length: 0\r\n\r\n'
  7.  
  8. parser = argparse.ArgumentParser()
  9. parser.add_argument('--client', help = 'client mode', action = 'store_true')
  10. parser.add_argument('--host', type = str, help = 'remote/listen host', required = True)
  11. parser.add_argument('--port', type = int, help = 'remote/listen port', required = True)
  12. parser.add_argument('--size', type = int, help = '(client mode) content length')
  13. parser.add_argument('--count', type = int, help = '(client mode) number of posts (0 = unlimited)')
  14. ns = parser.parse_args()
  15.  
  16. if ns.client and (ns.size is None or ns.count is None):
  17.     parser.error('--client requires --size and --count')
  18.  
  19. def drain(sock: socket.socket):
  20.     rlist = [sock]
  21.     while True:
  22.         ready, _, _ = select.select(rlist, [], [], .0)
  23.         if len(ready) == 0: break
  24.         sock.recv(65535)
  25.  
  26. def wait(sock: socket.socket):
  27.     rlist = [sock]
  28.     while True:
  29.         ready, _, _ = select.select(rlist, [], [])
  30.         if len(ready) > 0: break
  31.  
  32. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  33.     if ns.client:
  34.         s.connect((ns.host, ns.port))
  35.         count = 0
  36.         while count < ns.count or ns.count == 0:
  37.             s.sendall(REQUEST.format(host = ns.host, port = ns.port, len = ns.size, count = count).encode('ascii'))
  38.             s.sendall(bytes(0 for _ in range(1, ns.size)))
  39.            
  40.             wait(s)
  41.             drain(s)
  42.  
  43.             count += 1
  44.             if count % 100 == 0:
  45.                 print('{} posts sent'.format(count))
  46.     else:
  47.         s.bind((ns.host, ns.port))
  48.         s.listen()
  49.         while True:
  50.             c, remote = s.accept()
  51.             with c:
  52.                 print('accept {}'.format(remote))
  53.                 try:
  54.                     while True:
  55.                         drain(c)
  56.                         c.sendall(REPLY.encode('ascii'))
  57.                         wait(c)
  58.                 except ConnectionResetError:
  59.                     pass

Reply to "Untitled"

Here you can reply to the paste above

captcha