Django server response with 4xx response code sometimes - python

Background Information :
I have developed server in django python and deployed on AWS Elastic Beanstalk by using ELB.
Server content many API named like as /testapi, /xyz, etc.
Problem Facing :
Sometime it gave me 4xx or 200 when i called /testapi.
Attaching logs of AWS EC2 instant which show some of request are failed with 4xx response code and some are ok i.e 200.
Please guide me on the right path so I can solve it.
-----------------------Start of code-------------------
#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)
--------------------Start Of Logs -------------------
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"

Related

Regex Expression in python

I am trying to match the pattern in the given statement and print it as a dictionary. That pattern has four parts. When I test those parts individually then they are printed individually but when I combined them they give none. Kindly help me.
I tried the following code, but it does not work as expected.
pattern= """
(?P<host>[0-9]{3}\.[0-9]{3}\.[0-9]{3}\.[0-9]{3}) - (?P<user_name>[\w]*) (?P<time>\[.*?\]) (?P<Request>\".*?\")
"""
statement= """ 146.204.224.152 - feest6811 [21/Jun/2019:15:45:24 -0700] "POST /incentivize HTTP/1.1"
"""
item=re.match(pattern,statement,re.VERBOSE|re.IGNORECASE)
print(item.groupdict())
I want to print the following output.
host :146.204.224.152 , user_name: feest6811 , time:[21/Jun/2019:15:45:24 -0700], Request: "POST /incentivize HTTP/1.1"
This could be helpful:
import re, json
regex = r"(?P<host>(?:\d{3}\.){3}\d{3})\s-\s(?P<user_name>\S+)\s\[(?P<date>.+?)\]\s\"(?P<request>.+?)\""
test_str = "146.204.224.152 - feest6811 [21/Jun/2019:15:45:24 -0700] \"POST /incentivize HTTP/1.1\""
for m in re.finditer(regex, test_str):
if m:
print(m.groupdict())
# as a string
print(json.dumps(m.groupdict()))
else:
print("No match")
Should give output as:
{'host': '146.204.224.152', 'user_name': 'feest6811', 'date': '21/Jun/2019:15:45:24 -0700', 'request': 'POST /incentivize HTTP/1.1'}

serve() is called twice everytime I reload a blog page in wagtail

I am using wagtail to build a blog website. In order to count the visits of each blog, I override the get_context() and serve() to set cookie. The codes are as follows:
def get_context(self, request):
authorname=self.author.get_fullname_or_username()
data = count_visits(request, self)
context = super().get_context(request)
context['client_ip'] = data['client_ip']
context['location'] = data['location']
context['total_hits'] = data['total_hits']
context['total_visitors'] =data['total_vistors']
context['cookie'] = data['cookie']
context['author']=authorname
return context
def serve(self, request):
context = self.get_context(request)
print(request.COOKIES)
template = self.get_template(request)
response = render(request, template, context)
response.set_cookie(context['cookie'], 'true', max_age=3000)
return response
And the function count_visits is as follows:
def count_visits(request, obj):
data={}
ct = ContentType.objects.get_for_model(obj)
key = "%s_%s_read" % (ct.model, obj.pk)
if not request.COOKIES.get(key):
blog_visit_count, created =BlogVisitNumber.objects.get_or_create(content_type=ct, object_id=obj.pk)
blog_visit_count.count += 1
blog_visit_count.save()
The problem I met is that when I reload the blog page, serve() was loaded twice. The first time request was with cookie info. But the second time the cookie of the request was {}, which caused the count of visit does not work correctly.
I do not understand why serve() is called twice everytime I reload page.
After adding a print command in serve(), the log of reload blog page is as follows:
[19/Sep/2022 23:08:44] "GET /notifications/api/unread_list/?max=5 HTTP/1.1" 200 38
{'csrftoken': 'vAnNLAp8Fb9zPkUOSrHTFGB5Bp4W4aLu8mQWPklIhX1JpDTcKIceeId03jTKJpA3', 'sessionid': '9qdgjulitvosan1twtqe0h5bpfn4z3hg', 'bloglistingpage_7_read': 'true', 'blogdetailpage_6_read': 'true'}
[19/Sep/2022 23:08:44] "GET /blog-1/ HTTP/1.1" 200 27594
{}
[19/Sep/2022 23:08:45] "GET /blog-1/ HTTP/1.1" 200 23783
[19/Sep/2022 23:08:46] "GET /notifications/api/unread_list/?max=5 HTTP/1.1" 200 38
And the log of curl is:
[19/Sep/2022 23:12:24] "GET /notifications/api/unread_list/?max=5 HTTP/1.1" 200 38
{}
[19/Sep/2022 23:12:29] "GET /blog-1/ HTTP/1.1" 200 23783

I get status code 300 while updating the space information and image

i have been trying to update the space information along with the images that is on the same template.I could add the content but could not update.i tried to pass slug while submitting for updated space information for updating images too but the slug is shown null. My ajax code and view was working for the add part but not for update part. I get my console.log('rent worked') printed that is inside success function but 'request.post' is not printed which is inside EditSpaceView function What might be the reason? What should i do to make it work ?
views.py
class EditSpaceView(View):
def post(self,request,*args,**kwargs):
print ('edit space view',request)
if request.POST:
print('request.post')
response = HttpResponse('')
print('edited owner name is',request.POST.get('ownerName'))
print('edited amenities',request.POST.get('amenities'))
rental = Rental.objetcs.get(slug=self.kwargs['slug'])
rental.ownerName = request.POST.get('ownerName')
rental.email = request.POST.get('email')
rental.phoneNumber = request.POST.get('phoneNumber')
rental.listingName = request.POST.get('listingName')
rental.summary = request.POST.get('summary')
rental.property = request.POST.get('property')
rental.room = request.POST.get('room')
rental.price = request.POST.get('price')
rental.city = request.POST.get('city')
rental.place = request.POST.get('place')
rental.water = request.POST.get('water')
rental.amenities = request.POST.get('amenities')
rental.save()
response['pk-user'] = rental.slug
#response['pk-user'] = rental.pk
print('response slug', response);
return response
return HttpResponseRedirect('/')
class EditImage(View):
model = Rental
template_name = 'rentals/rent_detail.html'
def get(self, request, *args, **kwargs):
return render(request, self.template_name)
def post(self,request,*args,**kwargs):
try:
rental = Rental.objects.get(slug = self.kwargs['slug'])
print('rental slug',rental)
except Rental.DoesNotExist:
error_dict = {'message': 'Rental spae not found'}
return self.render(request,'rentals/rent_detail.html',error_dict)
if request.FILES:
for file in request.FILES.getlist('image'):
print('file',file)
image = Gallery.objects.create(rental = rental, image=file)
print('image',image)
return HttpResponseRedirect('/')
urls.py
url(r'^edit/image/(?P<slug>[\w-]+)/$', EditImage.as_view(), name="editImage"),
url(r'^edit/space/(?P<slug>[\w-]+)/$', EditSpaceView.as_view(), name="editSpace"),
ajax code
$.ajax({
url:'/edit/space/'+this.props.slug+'/',
contentType: "application/json",
data:sendData,
type:'POST',
success: function(data, textStatus, xhr ) {
console.log('rent worked');
var slug = xhr.getResponseHeader('pk-user');
console.log('slug',slug);
$.ajax({
url:'/edit/image/'+slug+'/',
data:image,
contentType:false,
processData:false,
type:'POST',
mimeType: "multipart/form-data",
success: function(data) {
console.log('success');
}
});
}
});
Stacktrace in server console
[05/Apr/2016 02:58:35] "GET /api/v1/rental/tushant-khatiwada/ HTTP/1.1" 200 963
edit space view <WSGIRequest: POST '/edit/space/tushant-khatiwada/'>
[05/Apr/2016 02:58:50] "POST /edit/space/tushant-khatiwada/ HTTP/1.1" 302 0
[05/Apr/2016 02:58:51] "GET / HTTP/1.1" 200 2363
Not Found: /edit/image/null/
[05/Apr/2016 02:58:51] "POST /edit/image/null/ HTTP/1.1" 404 6882
[05/Apr/2016 02:58:35] "GET /api/v1/rental/tushant-khatiwada/ HTTP/1.1" 200 963
edit space view <WSGIRequest: POST '/edit/space/tushant-khatiwada/'>
[05/Apr/2016 02:58:50] "POST /edit/space/tushant-khatiwada/ HTTP/1.1" 302 0
[05/Apr/2016 02:58:51] "GET / HTTP/1.1" 200 2363
These requests suggests that editing with EditSpaceView worked as expected. The 300 response is expected and due to HttpResponseRedirect("/") at the end of that view.
The request
[05/Apr/2016 02:58:51] "POST /edit/image/null/ HTTP/1.1" 404 6882
suggests that slug that you are using to build the URL for ajax request is not valid or is null. Specifically check this line,
var slug = xhr.getResponseHeader('pk-user');

flask session not reloading

Right now I am using Flask and a flask 3rd party library Flask-Session
Using the code below, I reload the page 4 times and get the following output:
set userid[0]
127.0.0.1 - - [27/Sep/2014 22:28:35] "GET / HTTP/1.1" 200 -
set userid[1]
127.0.0.1 - - [27/Sep/2014 22:28:37] "GET / HTTP/1.1" 200 -
set userid[2]
127.0.0.1 - - [27/Sep/2014 22:28:37] "GET / HTTP/1.1" 200 -
set userid[3]
127.0.0.1 - - [27/Sep/2014 22:28:38] "GET / HTTP/1.1" 200 -
Code:
from flask import Flask, session
from flask.ext.session import Session
app = Flask(__name__)
sess = Session()
nextId = 0
def verifySessionId():
global nextId
if not 'userId' in session:
session['userId'] = nextId
nextId += 1
sessionId = session['userId']
print ("set userid[" + str(session['userId']) + "]")
else:
print ("using already set userid[" + str(session['userId']) + "]")
sessionId = session.get('userId', None)
return sessionId
#app.route("/")
def hello():
userId = verifySessionId()
return str(userId)
if __name__ == "__main__":
app.config['SECRET_KEY'] = 'super secret key'
app.config['SESSION_TYPE'] = 'filesystem'
sess.init_app(app)
app.debug = True
app.run()
Shouldn't session['userId] be 'saved out' each time I reload the page?
You need to have cookies enabled for sessions to work. Even Flask-Session cannot track a browser without those.
Flask-Session sets a cookie with a unique id, then later on finds your session data again by that cookie.

Flask session member not persisting across requests

I'm writing a quick app to view a giant XML file with some AJAX style calls to viewgroup. My problem is session['groups'] not persisting. I have some old array with only 4 members that is stuck somewhere (cookie?..). That value is present when view is called. I then overwrite that session member with info from the recently opened xml file that contains 20+ members.
However, when viewgroup is called the session variable has reverted to the old value with only 4 members in the array!
Code followed by output. Note the 3 sessionStatus() calls
def sessionStatus():
print "# of groups in session = " + str(len(session['groups']))
#app.route('/')
def index():
cams = [file for file in os.listdir('xml/') if file.lower().endswith('xml')]
return render_template('index.html', cam_files=cams)
#app.route('/view/<xmlfile>')
def view(xmlfile):
path = 'xml/' + secure_filename(xmlfile)
print 'opening ' + path
xmlf = open(path, 'r')
tree = etree.parse(xmlf)
root = tree.getroot()
p = re.compile(r'Group')
groups = []
for g in root:
if (p.search(g.tag) is not None) and (g.attrib['Comment'] != 'Root'):
groups.append(Group(g.attrib['Comment']))
sessionStatus()
session['groups'] = groups
sessionStatus()
return render_template('view.html', xml=xmlfile, groups=groups)
#app.route('/viewgroup/<name>')
def viewGroup(name):
groups = session['groups']
sessionStatus()
if groups is None or len(groups) == 0:
raise Exception('invalid group name')
groups_filtered = [g for g in groups if g.name == name]
if len(groups_filtered) != 1:
raise Exception('invalid group name', groups_filtered)
group = groups_filtered[0]
prop_names = [p.name for p in group.properties]
return prop_names
Output
opening xml/d.xml
# of groups in session = 5
# of groups in session = 57
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /view/d.xml HTTP/1.1" 200 -
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /static/ivtl.css HTTP/1.1" 304 -
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /static/jquery.js HTTP/1.1" 304 -
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /static/raphael-min.js HTTP/1.1" 304 -
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /static/ivtl.css HTTP/1.1" 304 -
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /favicon.ico HTTP/1.1" 404 -
# of groups in session = 5
127.0.0.1 - - [17/Aug/2011 17:27:31] "GET /viewgroup/DeviceInformation HTTP/1.1" 200 -
I need all 57 groups to stay around. Any hints?
The data was simply too big to serialize into the session. Now I generate a key into a global dict and store that key in the session.
gXmlData[path] = groups
There's the problem that the global dict will stay around forever with more and more keys but the process isn't meant to live long.
Maybe your data is too big.
If your data is larger than 4KB, a server-side session is needed. Have a look at Flask-Session.

Categories