2013-01-03 5 views
9

플라스크 웹 서비스가 작동하도록 노력하고 있습니다. 스트리밍 게시물에 몇 가지 문제가 있습니다. 즉 헤더에 Transfer-Encoding : chunked가 포함되어있는 경우입니다.플라스크 및 전송 인코딩 : 청크

기본 플라스크가 HTTP 1.1을 지원하지 않는 것 같습니다. 이 문제가 해결 되었습니까? 이 코드에 대해

$ curl -v -X PUT --header "Transfer-Encoding: chunked" -d @pylucene-3.6.1-2-src.tar.gz "http://localhost:5000/async-test" 

:

우리는이 명령을 실행하는

@app.route("/async-test", methods=['PUT']) 
def result(): 
    print '------->'+str(request.headers)+'<------------' 
    print '------->'+str(request.data)+'<------------' 
    print '------->'+str(request.form)+'<------------' 
    return 'OK' 

여기 컬 출력입니다 :

$ curl -v -X PUT --header "Transfer-Encoding: chunked" -d @pylucene-3.6.1-2-src.tar.gz "http://localhost:5000/async-test" 
* About to connect() to localhost port 5000 (#0) 
* Trying ::1... Connection refused 
* Trying 127.0.0.1... connected 
* Connected to localhost (127.0.0.1) port 5000 (#0) 
> PUT /async-test HTTP/1.1 
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5 
> Host: localhost:5000 
> Accept: */* 
> Transfer-Encoding: chunked 
> Content-Type: application/x-www-form-urlencoded 
> Expect: 100-continue 
> 
* HTTP 1.0, assume close after body 
< HTTP/1.0 200 OK 
< Content-Type: text/html; charset=utf-8 
< Content-Length: 2 
< Server: Werkzeug/0.8.3 Python/2.7.1 
< Date: Wed, 02 Jan 2013 21:43:24 GMT 
< 

그리고 여기 플라스크 서버 출력입니다 :

* Running on 0.0.0.0:5000/ 
------->Transfer-Encoding: chunked 
Content-Length: 
User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5 
Host: localhost:5000 
Expect: 100-continue 
Accept: */* 
Content-Type: application/x-www-form-urlencoded 

<------------ 
-------><------------ 
------->ImmutableMultiDict([])<------------ 
+0

에서 얻을 수있는 구문 분석 청크를 처리하는 코드를 추가? –

+0

알림을 켜지 않아야하므로 지금까지이 설명을 보지 못했습니다. Waqas의 대답은 정확합니다. 테스트 코드를 Java로 옮겼습니다. –

답변

3

Flask Python이 아니고 mod_wsgi입니다. mod_wsgi 버전 3.0+만이 청크 분할 HTTP 전송을 지원하기 시작했습니다. Flask Python은 내부적으로 mod_wsgi에 대한 인터페이스로 Werkzeug 툴킷을 사용합니다. apt 소스에서 설치 한 경우 이전 버전 일 수 있습니다.

mod_wsgi의 최신 버전을 컴파일 한 다음 Flask 프레임 워크를 설치하면 문제가 해결 될 수 있습니다.

1

나를 위해 작동하지만 가장 세련된 방식으로 청크 분할을 shimming하는 것은 아닙니다. 나는 반응의 환경에 몸을 꽂는 방법을 사용했다.

Get raw POST body in Python Flask regardless of Content-Type header

그러나이

class WSGICopyBody(object): 
    def __init__(self, application): 
     self.application = application 

    def __call__(self, environ, start_response): 
     from cStringIO import StringIO 
     input = environ.get('wsgi.input') 
     length = environ.get('CONTENT_LENGTH', '0') 
     length = 0 if length == '' else int(length) 
     body = '' 
     if length == 0: 
      environ['body_copy'] = '' 
      if input is None: 
       return 
      if environ.get('HTTP_TRANSFER_ENCODING','0') == 'chunked': 
       size = int(input.readline(),16) 
       while size > 0: 
        body += input.read(size+2) 
        size = int(input.readline(),16) 
     else: 
      body = environ['wsgi.input'].read(length) 
     environ['body_copy'] = body 
     environ['wsgi.input'] = StringIO(body) 

     # Call the wrapped application 
     app_iter = self.application(environ, 
            self._sr_callback(start_response)) 

     # Return modified response 
     return app_iter 

    def _sr_callback(self, start_response): 
     def callback(status, headers, exc_info=None): 

      # Call upstream start_response 
      start_response(status, headers, exc_info) 
     return callback 


app.wsgi_app = WSGICopyBody(app.wsgi_app) 

사용이 혹시 이것에 대한 해결책을 찾았나요 그것을

request.environ['body_copy'] 
관련 문제