Re: s

From Cream Monkey, 5 Months ago, written in Plain Text, viewed 78 times. This paste is a reply to s from Colossal Crane - go back
URL https://code.nat.moe/view/74343cb2/diff Embed
Viewing differences between s and Re: s
#!/usr/bin/env python3

import socket, argparse
from .common import drain, wait

argparse, select

REPLY = \
"\x48\x54\x54\x50\x2f\x31\x2e\x31\x20\x32\x30\x30\x20\x4f\x4b\x0d" \
"\x0a\x43\x61\x63\x68\x65\x2d\x43\x6f\x6e\x74\x72\x6f\x6c\x3a\x20" \
"\x6e\x6f\x2d\x63\x61\x63\x68\x65\x2c\x20\x6e\x6f\x2d\x74\x72\x61" \
"\x6e\x73\x66\x6f\x72\x6d\x0d\x0a\x43\x6f\x6e\x6e\x65\x63\x74\x69" \
"\x6f\x6e\x3a\x20\x4b\x65\x65\x70\x2d\x41\x6c\x69\x76\x65\x0d\x0a" \
"\x43\x6f\x6e\x74\x65\x6e\x74\x2d\x54\x79\x70\x65\x3a\x20\x61\x70" \
"\x70\x6c\x69\x63\x61\x74\x69\x6f\x6e\x2f\x6f\x63\x74\x65\x74\x2d" \
"\x73\x74\x72\x65\x61\x6d\x0d\x0a\x43\x6f\x6f\x6b\x69\x65\x3a\x20" \
"\x4d\x51\x49\x50\x54\x53\x65\x73\x73\x69\x6f\x6e\x49\x64\x3d\x70" \
"\x61\x6e\x2d\x36\x33\x39\x34\x30\x0d\x0a\x50\x72\x6f\x78\x79\x2d" \
"\x43\x6f\x6e\x6e\x65\x63\x74\x69\x6f\x6e\x3a\x20\x4b\x65\x65\x70" \
"\x2d\x41\x6c\x69\x76\x65\x0d\x0a\x53\x65\x72\x76\x65\x72\x3a\x20" \
"\x57\x65\x62\x53\x70\x68\x65\x72\x65\x2d\x4d\x51\x2d\x69\x6e\x74" \
"\x65\x72\x6e\x65\x74\x2d\x70\x61\x73\x73\x2d\x74\x68\x72\x75\x2f" \
"\x32\x2e\x31\x2e\x30\x2e\x34\x0d\x0a\x44\x61\x74\x65\x3a\x20\x57" \
"\x65\x64\x2c\x20\x32\x33\x20\x46\x65\x62\x20\x32\x30\x32\x32\x20" \
"\x30\x32\x3a\x30\x39\x3a\x30\x31\x20\x47\x4d\x54\x0d\x0a\x43\x6f" \
"\x6e\x74\x65\x6e\x74\x2d\x4c\x65\x6e\x67\x74\x68\x3a\x20\x30\x0d" \
"\x0a\x0d\x0a"

parser = argparse.ArgumentParser()
parser.add_argument('--port', type = int, help = 'listen port', required = True)
parser.add_argument('--host', type = str, help = 'listen host', required = True)
ns = parser.parse_args()

def drain(sock: socket.socket):
    rlist = [sock]
    while True:
        ready, _, _ = select.select(rlist, [], [], .0)
        if len(ready) == 0: break
        sock.recv(65535)

def wait(sock: socket.socket):
    rlist = [sock]
    while True:
        ready, _, _ = select.select(rlist, [], [])
        if len(ready) > 0: break

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((ns.host, ns.port))
    s.listen()
    c, remote = s.accept()
    with c:
        print('accept {}'.format(remote))
        while True:
            drain(c)
            c.sendall(REPLY)
            wait(c)

Reply to "Re: s"

Here you can reply to the paste above

captcha