Re: s

From Cream Monkey, 4 Months ago, written in Plain Text, viewed 65 times. This paste is a reply to s from Colossal Crane - view diff
URL https://code.nat.moe/view/74343cb2 Embed
Download Paste or View Raw
  1. #!/usr/bin/env python3
  2.  
  3. import socket, argparse, select
  4.  
  5. REPLY = \
  6. "\x48\x54\x54\x50\x2f\x31\x2e\x31\x20\x32\x30\x30\x20\x4f\x4b\x0d" \
  7. "\x0a\x43\x61\x63\x68\x65\x2d\x43\x6f\x6e\x74\x72\x6f\x6c\x3a\x20" \
  8. "\x6e\x6f\x2d\x63\x61\x63\x68\x65\x2c\x20\x6e\x6f\x2d\x74\x72\x61" \
  9. "\x6e\x73\x66\x6f\x72\x6d\x0d\x0a\x43\x6f\x6e\x6e\x65\x63\x74\x69" \
  10. "\x6f\x6e\x3a\x20\x4b\x65\x65\x70\x2d\x41\x6c\x69\x76\x65\x0d\x0a" \
  11. "\x43\x6f\x6e\x74\x65\x6e\x74\x2d\x54\x79\x70\x65\x3a\x20\x61\x70" \
  12. "\x70\x6c\x69\x63\x61\x74\x69\x6f\x6e\x2f\x6f\x63\x74\x65\x74\x2d" \
  13. "\x73\x74\x72\x65\x61\x6d\x0d\x0a\x43\x6f\x6f\x6b\x69\x65\x3a\x20" \
  14. "\x4d\x51\x49\x50\x54\x53\x65\x73\x73\x69\x6f\x6e\x49\x64\x3d\x70" \
  15. "\x61\x6e\x2d\x36\x33\x39\x34\x30\x0d\x0a\x50\x72\x6f\x78\x79\x2d" \
  16. "\x43\x6f\x6e\x6e\x65\x63\x74\x69\x6f\x6e\x3a\x20\x4b\x65\x65\x70" \
  17. "\x2d\x41\x6c\x69\x76\x65\x0d\x0a\x53\x65\x72\x76\x65\x72\x3a\x20" \
  18. "\x57\x65\x62\x53\x70\x68\x65\x72\x65\x2d\x4d\x51\x2d\x69\x6e\x74" \
  19. "\x65\x72\x6e\x65\x74\x2d\x70\x61\x73\x73\x2d\x74\x68\x72\x75\x2f" \
  20. "\x32\x2e\x31\x2e\x30\x2e\x34\x0d\x0a\x44\x61\x74\x65\x3a\x20\x57" \
  21. "\x65\x64\x2c\x20\x32\x33\x20\x46\x65\x62\x20\x32\x30\x32\x32\x20" \
  22. "\x30\x32\x3a\x30\x39\x3a\x30\x31\x20\x47\x4d\x54\x0d\x0a\x43\x6f" \
  23. "\x6e\x74\x65\x6e\x74\x2d\x4c\x65\x6e\x67\x74\x68\x3a\x20\x30\x0d" \
  24. "\x0a\x0d\x0a"
  25.  
  26. parser = argparse.ArgumentParser()
  27. parser.add_argument('--port', type = int, help = 'listen port', required = True)
  28. parser.add_argument('--host', type = str, help = 'listen host', required = True)
  29. ns = parser.parse_args()
  30.  
  31. def drain(sock: socket.socket):
  32.     rlist = [sock]
  33.     while True:
  34.         ready, _, _ = select.select(rlist, [], [], .0)
  35.         if len(ready) == 0: break
  36.         sock.recv(65535)
  37.  
  38. def wait(sock: socket.socket):
  39.     rlist = [sock]
  40.     while True:
  41.         ready, _, _ = select.select(rlist, [], [])
  42.         if len(ready) > 0: break
  43.  
  44. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  45.     s.bind((ns.host, ns.port))
  46.     s.listen()
  47.     c, remote = s.accept()
  48.     with c:
  49.         print('accept {}'.format(remote))
  50.         while True:
  51.             drain(c)
  52.             c.sendall(REPLY)
  53.             wait(c)
  54.  

Reply to "Re: s"

Here you can reply to the paste above

captcha