2

I am using python and just build an API Server on django. And I wrote a script using urllib2 to access this server.

To access this API, request must have basic-auth information. If authorize failed, API give a response that code is 401.

This authorization check several arguments. So I add come content in response. like this:

    API_ERROR_DEF = {
        10001: 'Request without Basic Auth.',
        30001: 'Timestamp outdated.',
        ...
        ...
        'default': 'API Error: uncatched error.',
   }

   def _request_auth(error_code):
       response = HttpResponse()
       response['WWW-Authenticate'] = 'Basic realm=testMyAPI'
       response.status_code = 401 
       response.content = API_ERROR_DEF.get(error_code, API_ERROR_DEF['default'])
       return response

But there is a problem. When I run this server in bash directly, I can use script to get a 401 response with content. like this:

    -> print e.read()
    (Pdb) l
     92                     res = urllib2.urlopen(req)
     93                     print res.read()
     94             except urllib2.HTTPError as e:
     95                     traceback.print_exc()
     96                     __import__("pdb").set_trace()
     97  ->                 print e.read()
     98     
     99     if "__main__" == __name__:
    100             test(sys.argv[1])
    101             #ret = make_signature(sys.argv[1], sys.argv[2])
    102             #print ret
    (Pdb) c
    Timestamp outdated.

But when I run this server on nginx+uwsgi. I found server do not give me any content when script post and returns 401 response. However, only I send a request that method is get, this content appears again.

I think there must be some option on nginx or uwsgi that affect that. But I searched in their documents for a long time and only make me some more anxious.

So can anyone help me.

00000100
  • 21
  • 4

0 Answers0