Skip to content

Instantly share code, notes, and snippets.

@tai2
Created June 9, 2022 04:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tai2/3ad3b3ae824f2a78392a659a157b64ec to your computer and use it in GitHub Desktop.
import socket
import time
import threading
RECEIVE = False
#RECEIVE = True
NUM_THREADS = 300
finish_flags = [False for i in range(NUM_THREADS)]
def print_sock_params():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
rcvbuf_size = s.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)
print('SO_RCVBUF', rcvbuf_size)
s.close()
def do_http_get(i):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(10000)
s.connect(('127.0.0.1', 3000))
print('connected', i)
s.sendall(bytes('''GET /?cnt={} HTTP/1.1\r
Host: localhost\r
\r
'''.format(i), 'utf-8'))
if RECEIVE:
while (True):
data = s.recv(1024)
if not(data):
break
s.close()
print(i, 'received')
finish_flags[i] = True
# Put a sleep to keep the connection
time.sleep(10000)
if __name__ == '__main__':
print_sock_params()
threads = []
for i in range(NUM_THREADS):
t = threading.Thread(target=do_http_get, args=(i,))
t.start()
threads.append(t)
time.sleep(0.1)
while(True):
if sum(finish_flags) == NUM_THREADS:
break
time.sleep(0.1)
print('complete!!!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment