Created
July 28, 2016 11:44
-
-
Save santtu/a38bb50c44623a162df72cb6d0a45f0a to your computer and use it in GitHub Desktop.
Test case for connection: keep-alive handling in requests
This file contains hidden or 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 | |
from __future__ import print_function | |
import requests | |
import sys | |
url = sys.argv[1] | |
s = requests.Session() | |
while True: | |
r = s.get(url, allow_redirects=False) | |
print(r.status_code, r.reason) | |
if r.status_code not in [200, 302]: | |
break |
This file contains hidden or 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 socketserver | |
import time | |
class Handler(socketserver.BaseRequestHandler): | |
def handle(self): | |
self.data = self.request.recv(1024).strip() | |
print("{} wrote:".format(self.client_address[0])) | |
print(self.data) | |
data = "Hello there!" | |
headers = {'Connection': 'keep-alive', | |
'Content-Type': 'text/plain', | |
'Content-Length': len(data)} | |
response = (["HTTP/1.1 200 OK\r\n"] + | |
["{}: {}\r\n".format(k, v) for k, v in headers.items()] + | |
["\r\n\r\n", data]) | |
self.request.sendall("".join(response).encode('utf-8')) | |
time.sleep(2) | |
server = socketserver.TCPServer(("localhost", 8080), Handler) | |
server.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment