豬頭 (beezari) wrote,
豬頭
beezari

Chunked decoding in python

I haven't been able to find any short and quick method of decoding "chunked-encoding" encoded data in python without whacking 3rd party libraries, so here is my bet:
(chunked content encoding explained here: http://www.faqs.org/rfcs/rfc2616.html)

assuming that data contains raw http response. Code simplified for readability
def decode_chunked(data):
    offset = 0
    encdata = ''
    newdata = ''
    offset = string.index(data, "\r\n\r\n") + 4 # get the offset 
    # of the data payload. you can also parse content-length header as well.
    encdata =data[offset:]
    try:
        while (encdata != ''):
            off = int(encdata[:string.index(encdata,"\r\n")],16)
            if off == 0:
                break
            encdata = encdata[string.index(encdata,"\r\n") + 2:]
            newdata = "%s%s" % (newdata, encdata[:off])
            encdata = encdata[off+2:]
                             
    except:
       line = traceback.format_exc()
       print "Exception! %s" %line # probably indexes are wrong
    return newdata


for php version look here:

http://www.codingforums.com/showthread.php?t=147061
Tags: programming. nah, python
Subscribe

  • Ubuntu Custom CD: a quick and dirty way

    A few notes on my practical experience of building custom ubuntu disks. I work here with Ubuntu Precise (12.04). But any other Ubuntu distro should…

  • OpenVAS 4.x/5.x from SVN source

    Didn't see any around. so here are my notes. Build instructions for Debian and ubuntu. Assuming pcap, libc, gcc are already installed. build…

  • socket proxy in erlang

    Since I started spitting out some code here, here's another thing I've been playing recently. Erlang. For kicks, I decided to implement a socket…

  • Post a new comment

    Error

    default userpic

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments