How do I copy wsgi.input if I want to process POST data more than once?

前端 未结 3 2037
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-20 14:58

In WSGI, post data is consumed by reading the file-like object environ[\'wsgi.input\']. If a second element in the stack also wants to read post data it may han

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-20 15:54

    If you're gonna read it in one fell swoop, you could always read it in, create a CStringIO file-like object of the stuff you've read and then assign it back, like this:

    import cStringIO
    import copy
    lines = []
    for line in environ['wsgi.input']:
        lines.append(line)
    newlines = copy.copy(lines)
    environ['wsgi.input'] = cStringIO.StringIO(''.join(newlines))
    

    There's most likely a more efficient way to do this, but I in general find wsgi's post stuff pretty brittle if you want to do anything non-trivial (like read post data muptiple times)...

提交回复
热议问题