Created
February 8, 2017 14:39
-
-
Save kmike/b1c1344c93d6865aadba90abe7c9bcb0 to your computer and use it in GitHub Desktop.
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
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 78, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "from twisted.web.http import _ChunkedTransferDecoder" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 125, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "def decode_chunked_transfer(chunked_body):\n", | |
| " \"\"\"Parsed body received with chunked transfer encoding, and return the\n", | |
| " decoded body.\n", | |
| "\n", | |
| " For more info see:\n", | |
| " http://en.wikipedia.org/wiki/Chunked_transfer_encoding\n", | |
| "\n", | |
| " \"\"\"\n", | |
| " body_parts, h, t = [], b'', chunked_body\n", | |
| " while t:\n", | |
| " h, t = t.split(b'\\r\\n', 1)\n", | |
| " if h == b'0':\n", | |
| " break\n", | |
| " size = int(h, 16)\n", | |
| " body_parts.append(t[:size])\n", | |
| " t = t[size+2:]\n", | |
| " return b''.join(body_parts)\n", | |
| "\n", | |
| "\n", | |
| "def decode_chunked_transfer_fast(chunked_body):\n", | |
| " \"\"\"Parsed body received with chunked transfer encoding, and return the\n", | |
| " decoded body.\n", | |
| "\n", | |
| " For more info see:\n", | |
| " http://en.wikipedia.org/wiki/Chunked_transfer_encoding\n", | |
| "\n", | |
| " \"\"\"\n", | |
| " body_parts = []\n", | |
| " pos = 0\n", | |
| " while pos < len(chunked_body):\n", | |
| " separator_pos = chunked_body.find(b'\\r\\n', pos)\n", | |
| " if separator_pos == -1:\n", | |
| " separator_pos = len(chunked_body)\n", | |
| " \n", | |
| " chunk_size = chunked_body[pos:separator_pos]\n", | |
| " if chunk_size == b'0':\n", | |
| " break\n", | |
| " size = int(chunk_size, 16)\n", | |
| " pos = separator_pos + 2\n", | |
| " chunk_data = chunked_body[pos:pos+size]\n", | |
| " body_parts.append(chunk_data)\n", | |
| " pos += size + 2\n", | |
| " \n", | |
| " return b''.join(body_parts)\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 171, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "1960005" | |
| ] | |
| }, | |
| "execution_count": 171, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "chunked_body = b\"25\\r\\n\" + b\"This is the data in the first chunk\\r\\n\\r\\n\"\n", | |
| "chunked_body += b\"1C\\r\\n\" + b\"and this is the second one\\r\\n\\r\\n\"\n", | |
| "chunked_body += b\"3\\r\\n\" + b\"con\\r\\n\"\n", | |
| "chunked_body += b\"8\\r\\n\" + b\"sequence\\r\\n\"\n", | |
| "\n", | |
| "chunked_body = chunked_body * 20000\n", | |
| "\n", | |
| "chunked_body += b\"0\\r\\n\\r\\n\"\n", | |
| "\n", | |
| "len(chunked_body)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 172, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "def decode_chunked_transfer_twisted(chunked_body):\n", | |
| " parts = []\n", | |
| " dec = _ChunkedTransferDecoder(parts.append, lambda _:None)\n", | |
| " size = 10000\n", | |
| " pos = 0\n", | |
| " while pos < len(chunked_body):\n", | |
| " dec.dataReceived(chunked_body[pos:pos+size])\n", | |
| " pos += size\n", | |
| " return b''.join(parts)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 173, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# decode_chunked_transfer_fast(chunked_body)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 170, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "True" | |
| ] | |
| }, | |
| "execution_count": 170, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "decode_chunked_transfer(chunked_body) == decode_chunked_transfer_twisted(chunked_body)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 174, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": true | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1 loop, best of 3: 418 ms per loop\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%timeit decode_chunked_transfer_twisted(chunked_body)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 112, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[b'sdf']" | |
| ] | |
| }, | |
| "execution_count": 112, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "b'sdf'.split(b'23', 1)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 15, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# decode_chunked_transfer(chunked_body)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 16, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "chunked_body.find?" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.5.1" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment