Last active
May 14, 2020 20:13
-
-
Save ulidtko/7a40c0be1e26f9d86538042a5752714a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import socket | |
import struct | |
from time import sleep | |
FRAG1 = b"""HTTP/1.0 200 OK\r | |
Server: netcat\r | |
Content-Type: text/plain\r | |
Content-Length: 300\r | |
\r | |
asdfasdfsdf | |
asdf | |
-------------------- | |
234234234 | |
^Z""" | |
def main(): | |
argp = argparse.ArgumentParser() | |
argp.add_argument('host', default='localhost', | |
help="which address to bind on") | |
argp.add_argument('port', default=8097, type=int, | |
help="which port to listen on") | |
opts = argp.parse_args() | |
# --- # | |
gai = socket.getaddrinfo(opts.host, opts.port, | |
socket.AF_INET, socket.SOCK_STREAM, | |
flags=socket.AI_ADDRCONFIG) | |
print(f"Resolved to {len(gai)} address pair(s):") | |
for addrinfo in gai: | |
print(f" - {addrinfo}") | |
while True: | |
try: | |
for addrinfo in gai: | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.setsockopt(socket.SOL_SOCKET, | |
socket.SO_LINGER, | |
struct.pack('ii', 1, 0)) | |
print(f"binding {addrinfo[4]}") | |
s.bind(addrinfo[4]) | |
print("Listening...") | |
s.listen() | |
connection, (clientaddr, clientport) = s.accept() | |
print(f"Accepted connection from {clientaddr}:{clientport}.") | |
_request = connection.recv(1024) | |
sleep(0.128) | |
connection.send(FRAG1) | |
print(f"Sent response len {len(FRAG1)}:\n{FRAG1!r}") | |
sleep(0.3) | |
#-- With SO_LINGER set, sends RST | |
connection.close() | |
print("*** RST sent! ***") | |
print() | |
sleep(3.5) | |
except KeyboardInterrupt: | |
break | |
if __name__ == "__main__": | |
import sys | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment