Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
blog/en-2018-04-22-on-incomplete-http-reads-and-the-requests-library-in-python/servers/transfer-encoding-chunked.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
25 lines (21 sloc)
715 Bytes
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
#!/usr/bin/env python3 | |
# | |
# An HTTP server that returns fewer bytes in the body of the response than | |
# stated in the chunk size when using "Transfer-Encoding: Chunked". | |
# | |
import socketserver | |
class MyTCPHandler(socketserver.BaseRequestHandler): | |
def handle(self): | |
data = self.request.recv(1024) | |
print(data.decode()) | |
self.request.sendall( | |
b'HTTP/1.1 200 OK\r\n' | |
b'Transfer-Encoding: chunked\r\n' | |
b'\r\n' | |
b'a\r\n' # size of the chunk (0xa = 10) | |
b'123456' | |
) | |
class MyTCPSever(socketserver.TCPServer): | |
allow_reuse_address = True | |
with MyTCPSever(('localhost', 8080), MyTCPHandler) as server: | |
server.serve_forever() |