Django服务器有时响应4xx响应代码

时间:2017-10-28 13:18:58

标签: python django python-3.x django-rest-framework

背景资料:

  1. 我在django python中开发了服务器,并使用ELB部署在AWS Elastic Beanstalk上。
  2. 服务器内容很多名为/ testapi,/ xyz等的API
  3. 面临问题:

    有时候,当我打电话给/ testapi时,它给了我4xx或200。

    附上显示某些请求的AWS EC2即时通讯记录失败了4xx响应代码,有些正常,即200。

    请指导我走正确的道路,以便我能解决。

    -----------------------代码开头------------------ -

    @api_view(['POST'])
    @permission_classes((AllowAny,))
    def get_profile_details(request):
        if request.method == 'POST':
            try:
                logger = logging.getLogger("oauth")
                logger.info("get_profile_details POST endpoint called.")
                data = dict()
    
                client_id = request.data.get(UiString.client_id)
                client_secret = request.data.get(UiString.client_secret)
    
                logger.info("get_profile_details : client_id = " + client_id)
                logger.info("get_profile_details : client_secret = " + client_secret)
    
                if helper.validateClientIdAndClientSecret(client_id, client_secret):
                    logger.info("get_profile_details : Invalid Client Credentials")
                    return helper.returnResponse(ResponseCodes.Fail, "Unauthorized access", {}, False, status.HTTP_401_UNAUTHORIZED)
    
                try:
                    type = request.data.get(UiString.type)
                    email = request.data.get(UiString.email)
                    mobile = request.data.get(UiString.mobile)
                    username = request.data.get(UiString.username)
    
    
    
                    if helper.isNullOrEmpty(type):
                        return helper.returnResponse(ResponseCodes.missing_required_param, "Type should not be null", data, False,
                                                     status.HTTP_400_BAD_REQUEST)
    
                    if type == 'email' and helper.isNullOrEmpty(email):
                        return helper.returnResponse(ResponseCodes.missing_required_param, "Email should not be null", data, False,
                                                     status.HTTP_400_BAD_REQUEST)
                    elif type == 'mobile' and helper.isNullOrEmpty(mobile):
                        return helper.returnResponse(ResponseCodes.missing_required_param, "Mobile should not be null", data, False,
                                                     status.HTTP_400_BAD_REQUEST)
                    elif type == 'username' and helper.isNullOrEmpty(username):
                        return helper.returnResponse(ResponseCodes.missing_required_param, "Username should not be null", data,
                                                     False, status.HTTP_400_BAD_REQUEST)
    
                    try:
                        # Check user detail based on type
                        if type == 'email':
                            logger.info("get_profile_details : by using email as type")
                            user_details = User.objects.get(email=email)
                            user_profile_detail = user_details.user_profile
                        elif type == 'username':
                            logger.info("get_profile_details : by using username as type")
                            user_details = User.objects.get(username=username)
                            user_profile_detail = user_details.user_profile
                        elif type == 'mobile':
                            logger.info("get_profile_details : by using mobile as type")
                            user_profile_detail = UserProfile.objects.get(mobile1=mobile)
                            #user_details = User.objects.get(pk=user_profile_detail.user_id)
                    except (UserProfile.DoesNotExist, User.DoesNotExist) as ex:
                        logger.info("get_profile_details : user does not exist")
                        logger.exception(ex)
                        return helper.returnResponse(ResponseCodes.user_does_not_exist, "user does not exist",
                                                     data,
                                                     False, status.HTTP_200_OK)
                    except Exception as ex:
                        logger.info("get_profile_details : Got a exception")
                        logger.exception(ex)
                        return helper.returnResponse(ResponseCodes.exception_in_finding_user, "Got a exception",
                                                     data,
                                                     False, status.HTTP_200_OK)
    
                    user = user_profile_detail.user
                    user_profile_serializer = UserProfileSerializer(user.user_profile)
                    logger.info("get_profile_details : Got user detail : " + user.username)
    
                    data[UiString.user] = {
    
                        UiString.email : user.email,
                        UiString.username : user.username,
                        UiString.first_name : user.first_name,
                        UiString.last_name : user.last_name,
                        UiString.user_profile : user_profile_serializer.data,
                    }
                    logger.info("get_profile_details : User details retrived successfully")
                    return helper.returnResponse(ResponseCodes.success, "User details retrived successfully",
                                                 data,
                                                 False, status.HTTP_200_OK)
    
                except Exception as ex:
                    logger.info("get_profile_details : Exception in finding user")
                    logger.exception(ex)
                    return helper.returnResponse(ResponseCodes.exception_in_finding_user, "Exception in finding user detail api",
                                                 data,
                                                 False, status.HTTP_200_OK)
    
            except:
                response = helper.generateStandardResponse(ResponseCodes.exception_in_finding_user, "Failed get user from db. Something went wrong.", data,
                                                           False);
                return Response(response, status.HTTP_417_EXPECTATION_FAILED)
        return Response("Method not allowed.", status.HTTP_405_METHOD_NOT_ALLOWED)
    

    --------------------开始日志-------------------

    172.31.16.19 - - [28/Oct/2017:12:01:59 +0000] "POST /testapi/ HTTP/1.1" 404 218 "http://192.168.0.167/" "MozillaXYZ/1.0"
    172.31.2.71 - - [28/Oct/2017:12:02:01 +0000] "POST /testapi/ HTTP/1.1" 404 218 "http://192.168.0.167/" "MozillaXYZ/1.0"
    172.31.2.71 - - [28/Oct/2017:12:02:09 +0000] "POST /testapi/ HTTP/1.1" 404 218 "http://192.168.0.167/" "MozillaXYZ/1.0"
    127.0.0.1 (-) - - [28/Oct/2017:12:02:22 +0000] "GET / HTTP/1.1" 200 54 "-" "Python-urllib/2.7"
    172.31.2.71 (35.154.225.66) - - [28/Oct/2017:12:02:44 +0000] "PUT /xyz/ HTTP/1.1" 200 872 "http://192.168.0.167/" "MozillaXYZ/1.0"
    172.31.16.19 (35.154.225.66) - - [28/Oct/2017:12:02:54 +0000] "PUT /xyz/ HTTP/1.1" 200 834 "http://192.168.0.167/" "MozillaXYZ/1.0"
    172.31.2.71 (35.154.225.66) - - [28/Oct/2017:12:03:23 +0000] "POST /testapi/ HTTP/1.1" 200 866 "http://192.168.0.167/" "MozillaXYZ/1.0"
    172.31.16.19 (35.154.225.66) - - [28/Oct/2017:12:03:25 +0000] "PUT /xyz/ HTTP/1.1" 200 857 "http://192.168.0.167/" "MozillaXYZ/1.0"
    ::1 (-) - - [28/Oct/2017:12:03:33 +0000] "OPTIONS * HTTP/1.0" 200 - "-" "Apache/2.4.25 (Amazon) mod_wsgi/3.5 Python/3.4.3 (internal dummy connection)"
    ::1 (-) - - [28/Oct/2017:12:03:34 +0000] "OPTIONS * HTTP/1.0" 200 - "-" "Apache/2.4.25 (Amazon) mod_wsgi/3.5 Python/3.4.3 (internal dummy connection)"
    ::1 (-) - - [28/Oct/2017:12:03:44 +0000] "OPTIONS * HTTP/1.0" 200 - "-" "Apache/2.4.25 (Amazon) mod_wsgi/3.5 Python/3.4.3 (internal dummy connection)"
    ::1 (-) - - [28/Oct/2017:12:03:45 +0000] "OPTIONS * HTTP/1.0" 200 - "-" "Apache/2.4.25 (Amazon) mod_wsgi/3.5 Python/3.4.3 (internal dummy connection)"
    172.31.2.71 (35.154.225.66) - - [28/Oct/2017:12:03:51 +0000] "POST /testapi/ HTTP/1.1" 200 906 "http://192.168.0.167/" "MozillaXYZ/1.0"
    172.31.16.19 (35.154.225.66) - - [28/Oct/2017:12:04:03 +0000] "POST /testapi/ HTTP/1.1" 200 884 "http://192.168.0.167/" "MozillaXYZ/1.0"
    172.31.2.71 (35.154.225.66) - - [28/Oct/2017:12:04:08 +0000] "POST /testapi/ HTTP/1.1" 200 904 "http://192.168.0.167/" "MozillaXYZ/1.0"
    172.31.2.71 (35.154.225.66) - - [28/Oct/2017:12:04:30 +0000] "POST /testapi/ HTTP/1.1" 200 882 "http://192.168.0.167/" "MozillaXYZ/1.0"
    172.31.2.71 (52.66.89.157) - - [28/Oct/2017:12:05:17 +0000] "POST /testapi/ HTTP/1.1" 200 1268 "http://192.168.0.167/" "MozillaXYZ/1.0"
    172.31.16.19 (35.154.225.66) - - [28/Oct/2017:12:05:33 +0000] "POST /testapi/ HTTP/1.1" 200 861 "http://192.168.0.167/" "MozillaXYZ/1.0"
    172.31.16.19 (35.154.225.66) - - [28/Oct/2017:12:05:48 +0000] "POST /testapi/ HTTP/1.1" 400 88 "http://192.168.0.167/" "MozillaXYZ/1.0"
    172.31.2.71 (35.154.225.66) - - [28/Oct/2017:12:05:53 +0000] "POST /testapi/ HTTP/1.1" 200 882 "http://192.168.0.167/" "MozillaXYZ/1.0"
    172.31.2.71 (35.154.225.66) - - [28/Oct/2017:12:06:11 +0000] "PUT /xyz/ HTTP/1.1" 200 830 "http://192.168.0.167/" "MozillaXYZ/1.0"
    

0 个答案:

没有答案