Skip to content

Instantly share code, notes, and snippets.

@jossef
Last active August 25, 2021 11:33
Show Gist options
  • Save jossef/5b94b7bac62d1779344d4ca1b03e02ea to your computer and use it in GitHub Desktop.
Save jossef/5b94b7bac62d1779344d4ca1b03e02ea to your computer and use it in GitHub Desktop.
sockets timeout recv tests script
import select
import socket
def scenario_1__stuck():
sock = socket.create_connection(('neverssl.com', 80))
sock.send(b'GET / HTTP/1.1\r\nHost: neverssl.com\r\n\r\n')
data = sock.recv(4096)
print('1st recv', data)
data = sock.recv(4096) # <- gonna stuck here
print('2nd recv', data)
# output
# 1st recv b'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: 1416\r\nConnection: keep-alive\r\nLast-Modified: Thu, 06 Jun 2019 04:19:28 GMT\r\nAccept-Ranges: bytes\r\nServer: AmazonS3\r\nDate: Tue, 06 Oct 2020 11:23:35 GMT\r\nETag: "92b6819fcf2865f6be341e02390d23bf"\r\nVary: Accept-Encoding\r\nX-Cache: Hit from cloudfront\r\nVia: 1.1 9b3a0b2647b64bb06aa470977314bbb3.cloudfront.net (CloudFront)\r\nX-Amz-Cf-Pop: TLV50-C1\r\nX-Amz-Cf-Id: WWtg8laR998pxbuhKBHyt84EbEvfLy6Fzf0PGIxp1OJettEBNTsWMw==\r\nAge: 33014\r\n\r\n<html>\n <head>\n <title>NeverSSL - Connecting ... </title>\n\n <style>\n body {\n font-family: Montserrat, helvetica, arial, sans-serif; \n font-size: 16x;\n color: #444444;\n margin: 0;\n }\n h2 {\n font-weight: 700;\n font-size: 1.6em;\n margin-top: 30px;\n }\n p {\n line-height: 1.6em;\n }\n .container {\n max-width: 650px;\n margin: 20px auto 20px auto;\n padding-left: 15px;\n padding-right: 15px\n }\n .header {\n background-color: #42C0FD;\n color: #FFFFFF;\n padding: 10px 0 10px 0;\n font-size: 2.2em;\n }\n <!-- CSS from Mark Webster https://gist.github.com/markcwebster/9bdf30655cdd5279bad13993ac87c85d -->\n </style>\n\n <script>\n var prefix = \'bcdfhklmnrstvwxz\';\n \n prefix = prefix.split(\'\').sort(function(){return 0.5-Math.random()}).join(\'\')\n\n window.location.href = \'http://\' + prefix + \'.neverssl.com/online\'; \n </script>\n </head>\n <body>\n\n <div class="header">\n <div class="container">\n <h1>NeverSSL</h1>\n </div>\n </div>\n \n <div class="content">\n <div class="container">\n\n <h1>Connecting ...</h1>\n\n </div>\n </div>\n \n </body>\n</html>\n'
def scenario_2__socket_settimeout():
sock = socket.create_connection(('neverssl.com', 80))
timeout_seconds = 2
sock.settimeout(timeout_seconds)
sock.send(b'GET / HTTP/1.1\r\nHost: neverssl.com\r\n\r\n')
data = sock.recv(4096)
print('1st recv', data)
data = sock.recv(4096) # <- will raise socket.timeout exception after 2 seconds
print('2nd recv', data)
# output
# 1st recv b'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: 1416\r\nConnection: keep-alive\r\nLast-Modified: Thu, 06 Jun 2019 04:19:28 GMT\r\nAccept-Ranges: bytes\r\nServer: AmazonS3\r\nDate: Tue, 06 Oct 2020 11:23:35 GMT\r\nETag: "92b6819fcf2865f6be341e02390d23bf"\r\nVary: Accept-Encoding\r\nX-Cache: Hit from cloudfront\r\nVia: 1.1 9b3a0b2647b64bb06aa470977314bbb3.cloudfront.net (CloudFront)\r\nX-Amz-Cf-Pop: TLV50-C1\r\nX-Amz-Cf-Id: WWtg8laR998pxbuhKBHyt84EbEvfLy6Fzf0PGIxp1OJettEBNTsWMw==\r\nAge: 33014\r\n\r\n<html>\n <head>\n <title>NeverSSL - Connecting ... </title>\n\n <style>\n body {\n font-family: Montserrat, helvetica, arial, sans-serif; \n font-size: 16x;\n color: #444444;\n margin: 0;\n }\n h2 {\n font-weight: 700;\n font-size: 1.6em;\n margin-top: 30px;\n }\n p {\n line-height: 1.6em;\n }\n .container {\n max-width: 650px;\n margin: 20px auto 20px auto;\n padding-left: 15px;\n padding-right: 15px\n }\n .header {\n background-color: #42C0FD;\n color: #FFFFFF;\n padding: 10px 0 10px 0;\n font-size: 2.2em;\n }\n <!-- CSS from Mark Webster https://gist.github.com/markcwebster/9bdf30655cdd5279bad13993ac87c85d -->\n </style>\n\n <script>\n var prefix = \'bcdfhklmnrstvwxz\';\n \n prefix = prefix.split(\'\').sort(function(){return 0.5-Math.random()}).join(\'\')\n\n window.location.href = \'http://\' + prefix + \'.neverssl.com/online\'; \n </script>\n </head>\n <body>\n\n <div class="header">\n <div class="container">\n <h1>NeverSSL</h1>\n </div>\n </div>\n \n <div class="content">\n <div class="container">\n\n <h1>Connecting ...</h1>\n\n </div>\n </div>\n \n </body>\n</html>\n'
# Traceback (most recent call last):
# File ".../sockets.py", line 61, in <module>
# test_timeout()
# File ".../sockets.py", line 36, in test_timeout
# data = sock.recv(4096)
# socket.timeout: timed out
def recv_timeout(sock, bytes_to_read, timeout_seconds):
sock.setblocking(0)
ready = select.select([sock], [], [], timeout_seconds)
if ready[0]:
return sock.recv(bytes_to_read)
raise socket.timeout()
def scenario_3__select():
sock = socket.create_connection(('neverssl.com', 80))
timeout_seconds = 2
sock.send(b'GET / HTTP/1.1\r\nHost: neverssl.com\r\n\r\n')
data = recv_timeout(sock, 4096, timeout_seconds)
print('1st recv', data)
data = recv_timeout(sock, 4096, timeout_seconds) # <- will raise socket.timeout exception after 2 seconds
print('2nd recv', data)
# output
# 1st recv b'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: 1416\r\nConnection: keep-alive\r\nLast-Modified: Thu, 06 Jun 2019 04:19:28 GMT\r\nAccept-Ranges: bytes\r\nServer: AmazonS3\r\nDate: Tue, 06 Oct 2020 11:23:35 GMT\r\nETag: "92b6819fcf2865f6be341e02390d23bf"\r\nVary: Accept-Encoding\r\nX-Cache: Hit from cloudfront\r\nVia: 1.1 9b3a0b2647b64bb06aa470977314bbb3.cloudfront.net (CloudFront)\r\nX-Amz-Cf-Pop: TLV50-C1\r\nX-Amz-Cf-Id: WWtg8laR998pxbuhKBHyt84EbEvfLy6Fzf0PGIxp1OJettEBNTsWMw==\r\nAge: 33014\r\n\r\n<html>\n <head>\n <title>NeverSSL - Connecting ... </title>\n\n <style>\n body {\n font-family: Montserrat, helvetica, arial, sans-serif; \n font-size: 16x;\n color: #444444;\n margin: 0;\n }\n h2 {\n font-weight: 700;\n font-size: 1.6em;\n margin-top: 30px;\n }\n p {\n line-height: 1.6em;\n }\n .container {\n max-width: 650px;\n margin: 20px auto 20px auto;\n padding-left: 15px;\n padding-right: 15px\n }\n .header {\n background-color: #42C0FD;\n color: #FFFFFF;\n padding: 10px 0 10px 0;\n font-size: 2.2em;\n }\n <!-- CSS from Mark Webster https://gist.github.com/markcwebster/9bdf30655cdd5279bad13993ac87c85d -->\n </style>\n\n <script>\n var prefix = \'bcdfhklmnrstvwxz\';\n \n prefix = prefix.split(\'\').sort(function(){return 0.5-Math.random()}).join(\'\')\n\n window.location.href = \'http://\' + prefix + \'.neverssl.com/online\'; \n </script>\n </head>\n <body>\n\n <div class="header">\n <div class="container">\n <h1>NeverSSL</h1>\n </div>\n </div>\n \n <div class="content">\n <div class="container">\n\n <h1>Connecting ...</h1>\n\n </div>\n </div>\n \n </body>\n</html>\n'
# Traceback (most recent call last):
# File ".../sockets.py", line 66, in <module>
# test_select()
# File ".../sockets.py", line 57, in test_select
# data = recv_timeout(sock, 4096, timeout_seconds)
# File ".../sockets.py", line 48, in recv_timeout
# raise socket.timeout()
# socket.timeout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment