Created
February 27, 2018 10:39
-
-
Save clayg/16212c0c3f058178c6cd6dec2e7cfe97 to your computer and use it in GitHub Desktop.
Listen on a port and do nothing good
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
# Timeout server program | |
import sys | |
from eventlet import spawn_n, sleep, Timeout | |
from eventlet.green import socket | |
HOST = '' | |
PORT = int(sys.argv[1]) | |
TIMEOUT = 3.1 | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
print 'starting on port %s' % PORT | |
s.bind((HOST, PORT)) | |
s.listen(1) | |
open_conns = [] | |
def do_nothing(conn): | |
open_conns.append(conn) | |
try: | |
sleep(TIMEOUT) | |
conn.close() | |
finally: | |
open_conns.remove(conn) | |
print 'closed conn (%s)' % len(open_conns) | |
while True: | |
while len(open_conns) >= 1000: | |
sleep(0.01) | |
with Timeout(0.01, False): | |
conn, addr = s.accept() | |
print 'accept conn (%s)' % len(open_conns) | |
spawn_n(do_nothing, conn) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment