2009-04-22 4 views
1

간단한 양식에서 POST 데이터를 잡으려고합니다.WSGIREF를 사용하여 POST를 잡는 방법

WSGIREF로 처음 놀고 있는데,이를 수행하는 올바른 방법을 찾지 못하는 것 같습니다.

This is the form: 
<form action="test" method="POST"> 
<input type="text" name="name"> 
<input type="submit"></form> 

그리고 분명히 포스트 잡기 위해 올바른 정보를 누락 된 기능 : 서버에서

def app(environ, start_response): 
    """starts the response for the webserver""" 
    path = environ[ 'PATH_INFO'] 
    method = environ['REQUEST_METHOD'] 
    if method == 'POST': 
     if path.startswith('/test'): 
      start_response('200 OK',[('Content-type', 'text/html')]) 
      return "POST info would go here %s" % post_info 
    else: 
     start_response('200 OK', [('Content-type', 'text/html')]) 
     return form() 
+0

올바른 동작 대신 어떻게됩니까? 나는이 응용 프로그램을 빠른'paster server'로 실행 시켰고 모든 것이 제대로 작동하는 것처럼 보입니다. – hao

답변

3

당신은 읽기해야합니다 응답.

nosklo's answer에서 비슷한 문제 : "PEP 333you must read environ['wsgi.input']입니다." (this answer 각색)

시험 번호 :
        경고 :이 코드는 예시적인 목적으로 만 제공된다.
        경고 : 경로 또는 파일 이름을 하드 코딩하지 마십시오.

def app(environ, start_response): 
    path = environ['PATH_INFO'] 
    method = environ['REQUEST_METHOD'] 
    if method == 'POST': 
     if path.startswith('/test'): 
      try: 
       request_body_size = int(environ['CONTENT_LENGTH']) 
       request_body = environ['wsgi.input'].read(request_body_size) 
      except (TypeError, ValueError): 
       request_body = "0" 
      try: 
       response_body = str(request_body) 
      except: 
       response_body = "error" 
      status = '200 OK' 
      headers = [('Content-type', 'text/plain')] 
      start_response(status, headers) 
      return [response_body] 
    else: 
     response_body = open('test.html').read() 
     status = '200 OK' 
     headers = [('Content-type', 'text/html'), 
        ('Content-Length', str(len(response_body)))] 
     start_response(status, headers) 
     return [response_body] 
+0

고마워,이게 내가 필요한거야! 한 가지 수정은 response_body에 [5 :]를 추가하여 'size ='부분을 피하는 것이 었습니다. – alfredodeza

관련 문제