2014-06-16 7 views
-1

현재 HTML 파일을 제공하는 작은 gevent Python 응용 프로그램이 있습니다. 작은 파일을 업로드하고 싶습니다.python에서 gevent를 사용하여 파일 업로드 POST 요청을받는 방법

파일을 /file_upload 경로로 보내도록 양식이 만들어졌습니다.

파일을 디스크에 저장하려면 파이썬 측에서 파일을 수신하려면 어떻게해야합니까?


현재 나는 200 OK 응답을 보낸다 :

def __call__(self, environ, start_response): 
    """function used to serve files following an http request""" 
    path = environ['PATH_INFO'].strip('/') or 'index.html' 
    if path == 'file_upload': 
     start_response('200 OK',[('Content-Type', 'text/html')]) 
     return 'OK' 

답변

-1

으로 파일 내용을 얻을 수 있습니다을 environ['wsgi.input'].read()

def __call__(self, environ, start_response): 
    """function used to serve files following an http request""" 
    path = environ['PATH_INFO'].strip('/') or 'index.html' 
    if path == 'file_upload': 
     data = environ['wsgi.input'].read(int(environ.get('CONTENT_LENGTH','0'))) 
     f = open('./uploaded_file', 'wb') 
     f.write(data) 
     f.close() 
     start_response('200 OK',[('Content-Type', 'text/plain')]) 
     return 'OK'