django IOError: request data read error from BlackBerry - django

My site is an Intranet and has hundreds of hits by day. The issue is that django crash some times and I received this trace back error:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 105, in get_response
response = middleware_method(request, callback, callback_args, callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/middleware/csrf.py", line 200, in process_view
request_csrf_token = request.POST.get('csrfmiddlewaretoken', '')
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 210, in _get_post
self._load_post_and_files()
File "/usr/local/lib/python2.7/dist-packages/django/http/__init__.py", line 284, in _load_post_and_files
self._post, self._files = QueryDict(self.raw_post_data, encoding=self._encoding), MultiValueDict()
File "/usr/local/lib/python2.7/dist-packages/django/http/__init__.py", line 248, in _get_raw_post_data
self._raw_post_data = self.read(content_length)
File "/usr/local/lib/python2.7/dist-packages/django/http/__init__.py", line 296, in read
return self._stream.read(*args, **kwargs)
IOError: request data read error
And the relevant information is that I have found this on debug data all times that program crash:
'HTTP_USER_AGENT': 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9300; es) AppleWebKit/534.8+ (KHTML, like Gecko) Version/6.0.0.668 Mobile Safari/534.8+',
'HTTP_X_RIM_HTTPS': '1.1',
'HTTP_X_WAP_PROFILE': '"http://www.blackberry.net/go/mobile/profiles/uaprof/9300_edge/6.0.0.rdf"',
App crash in login form. Some ideas?

as you might think, this is no django error.
see https://groups.google.com/group/django-users/browse_thread/thread/946936f69c012d96
have the error myself (but IE ajax requests only, no file upload, just post data).
will add an complete answer if i ever find out how to fix this.
REF: IOError: request data read error

Related

Django view html to pdf executed twice

I have following Class to generate a pdf file where I use django-renderpdf to generate a pdf from a html template. But the view is executed twice and an error is thrown.
My class:
class WeeklyMetre(PDFView):
template_name = 'reports/invoice/weekly_metre.html'
allow_force_html = True
prompt_download = True
#property
def download_name(self) -> str:
invoice = Invoice.objects.get(pk=self.kwargs['pk'])
return f"WeeklyMetre_{invoice.invoice_number}.pdf"
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
invoice = Invoice.objects.get(pk=self.kwargs.get('pk'))
market_labor_specifications = _getWeeklyMetreData(invoice=invoice)
# calculate reported items: reported market_labor_specifications
# invoiced specifications which are validated in invoice-period
# but labor_date before invoice-period
reported_mls = MarketLaborSpecification.objects.filter(invoice_id=self.kwargs.get('pk'), market_labor__labor_date__lt=invoice.period_from) \
.values('market_labor__labor_date', 'specification__position', 'specification__name') \
.order_by('market_labor__labor_date', 'specification__position', 'specification__name') \
.annotate(sum_pos=Sum('validated_quantity'))
context.update({
'invoice': invoice,
'market_labor_specifications': market_labor_specifications,
'reported_mlss': reported_mls
})
print('context data', datetime.datetime.now())
return context
Between the two excutions I have following error:
[01/Feb/2021 07:16:38] "GET /reports/invoice/select/17/ HTTP/1.1" 200 1414
context data 2021-02-01 07:16:44.835695
[01/Feb/2021 07:16:45] "GET /reports/weekly/metre/17/ HTTP/1.1" 200 58063
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 60114)
Traceback (most recent call last):
File "/usr/lib/python3.6/socketserver.py", line 654, in process_request_thread
self.finish_request(request, client_address)
File "/usr/lib/python3.6/socketserver.py", line 364, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python3.6/socketserver.py", line 724, in __init__
self.handle()
File "/home/t3tr4ktys/python-virtual-environments/BillOfQuantities/lib/python3.6/site-packages/django/core/servers/basehttp.py", line 174, in handle
self.handle_one_request()
File "/home/t3tr4ktys/python-virtual-environments/BillOfQuantities/lib/python3.6/site-packages/django/core/servers/basehttp.py", line 182, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
File "/usr/lib/python3.6/socket.py", line 586, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [Errno 104] Connection reset by peer
----------------------------------------
context data 2021-02-01 07:16:47.544189
[01/Feb/2021 07:16:48] "GET /reports/weekly/metre/17/ HTTP/1.1" 200 58063
First of all I don't know why it is executed twice and on the second execution the user is no more authenticated. At the end the pdf is generated well but I cannot apply the LoginRequiredMixin. More informations will be provided if needed and thank you for any help.
Looks like you closed tab in browser or browser stopped to accept response from server in some another way. But web server haven't stopped it's renderring and still generates response, but do not have whom to response. That's why you see:
ConnectionResetError: [Errno 104] Connection reset by peer
After that browser does retry with page reload and you see the second request in logs.
So, you need to understand why it happens. See what browser logs and Dev.Tools will show you (statuses, response and etc.).
P.S. About LoginRequiredMixin - don't know probably some internal things of browser caused with that "retry", but I believe it still should send cookies and session_id.
I believe I've experienced something like this.
The problem is that when you call your endpoint you call it in the URL without a '/' at the end causing DRF to return 301
that redirects you to the same path but with '/' at the end. The problem with that, that after the redirect you lose your headers and cookies and thus become unauthenticated.
That also explains why you can see 2 calls.
So basically if you have a call like this:
api/viewset/dosomethngs
to:
api/viewset/dosomethngs/

Python request to API keeps returning ZeroReturnError exception

Python 2.7.3
Calling an API from a Raspberry Pi 3, the API logs show it hits the correct endpoint and returns with a 200 status code, but the python code from the Pi spits out a huge error stack. I saw in some forums that the ZeroReturnError is always thrown meaning that there was nothing wrong, but that seems weird since I can't actually get the results of the response in an except block from the try.
My code is literally
import requests
response = requests.get(<URL I AM USING>, json={JSON I AM USING})
Not sure what to do.
Traceback (most recent call last):
File "music.py", line 13, in <module>
response = requests.get(url, json={'blah':{'blah':'*********'}})
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 60, in get
return request('get', url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 49, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 457, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 606, in send
r.content
File "/usr/lib/python2.7/dist-packages/requests/models.py", line 724, in content
self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or bytes()
File "/usr/lib/python2.7/dist-packages/requests/models.py", line 653, in generate
for chunk in self.raw.stream(chunk_size, decode_content=True):
File "/usr/lib/python2.7/dist-packages/urllib3/response.py", line 256, in stream
data = self.read(amt=amt, decode_content=decode_content)
File "/usr/lib/python2.7/dist-packages/urllib3/response.py", line 186, in read
data = self._fp.read(amt)
File "/usr/lib/python2.7/httplib.py", line 602, in read
s = self.fp.read(amt)
File "/usr/lib/python2.7/socket.py", line 380, in read
data = self._sock.recv(left)
File "/usr/lib/python2.7/dist-packages/urllib3/contrib/pyopenssl.py", line 188, in recv
data = self.connection.recv(*args, **kwargs)
OpenSSL.SSL.ZeroReturnError
Some more searching brought me to think it was version issues.
Ran sudo pip install urllib3 --upgrade on the Raspberry Pi and it cleared it up.
I am getting a DependencyWarning about installing PySocks, but its working correctly now.

Logging fail in Appengine

I'm trying to do some importing to a DB in App Store 1.9.26, in python 2.7.9, and to do so,I would like to get some logging so I can inspect some variables and have a look on whjt is happening
import logging
[...]
updateDueDate = '2016.3.15'
fmt ='%Y.%m.%d'
fechalinea = datetime.datetime.strptime(updateDueDate,fmt)
fecha_aviso = avisoDB.modificacion.due_date.strftime(fmt)
logging.error = ('Date Call= %s, Date DB= %s' %(fecha_aviso, fechalinea))
Which should be pretty straightforward, but its not working. I think I mess up my python instalation or something that has nothing to do with my code, since the output I get from the devserver is:
TypeError("'str' object is not callable",)
TypeError("'str' object is not callable",)
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 1302, in communicate
req.respond()
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 831, in respond
self.server.gateway(self).respond()
INFO 2015-11-22 16:44:48,554 module.py:809] default: "POST /admin/filemanager HTTP/1.1" 500 -
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 2115, in respond
response = self.req.server.wsgi_app(self.env, self.start_response)
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/wsgi_server.py", line 272, in __call__
return app(environ, start_response)
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/request_rewriter.py", line 314, in _rewriter_middleware
response_body = iter(application(environ, wrapped_start_response))
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/request_handler.py", line 154, in __call__
response = self.handle_normal_request(environ)
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/request_handler.py", line 184, in handle_normal_request
self._PYTHON_LIB_DIR)
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/runtime.py", line 152, in HandleRequest
error)
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 329, in HandleRequest
return WsgiRequest(environ, handler_name, url, post_data, error).Handle()
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 279, in Handle
logging.exception('')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 1609, in exception
error(msg, *args, **kwargs)
TypeError: 'str' object is not callable
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 1302, in communicate
req.respond()
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 831, in respond
self.server.gateway(self).respond()
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 2115, in respond
response = self.req.server.wsgi_app(self.env, self.start_response)
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/wsgi_server.py", line 272, in __call__
return app(environ, start_response)
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/request_rewriter.py", line 314, in _rewriter_middleware
response_body = iter(application(environ, wrapped_start_response))
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/request_handler.py", line 154, in __call__
response = self.handle_normal_request(environ)
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/request_handler.py", line 184, in handle_normal_request
self._PYTHON_LIB_DIR)
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/runtime.py", line 152, in HandleRequest
error)
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 329, in HandleRequest
return WsgiRequest(environ, handler_name, url, post_data, error).Handle()
File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 279, in Handle
logging.exception('')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 1609, in exception
error(msg, *args, **kwargs)
TypeError: 'str' object is not callable
INFO 2015-11-22 16:44:48,647 module.py:809] default: "GET /favicon.ico HTTP/1.1" 200 15086
Tried to upgrade python and GAE launcher, google for this problem, but with all the different options I get the same errors, and I'm pretty sure it has nothing to do with my code, but... any one has any idea on what is happening?
Thanks
logging.error = assigns the right-hand-side (in your case, a string) to attribute error of module logging, replacing its previous value (which was a function).
When any Python code (yours or in the SDK) later calls logging.error (which you've set to a string), of course it will produce TypeError("'str' object is not callable",)!
I think I mess up my python instalation
Yes you did, by assigning a string to logging.error.
or something that has nothing to do with my code
It has everything to do with your code -- it's your code that does the mistaken assigning!

Braintree errors with GAE - Connection Errors - 5 Attempt to get blocked socket option

INFO 2015-10-09 11:07:31,718 connectionpool.py:695] Starting new HTTPS connection (1): api.sandbox.braintreegateway.com
DEBUG 2015-10-09 11:07:31,724 api_server.py:277] Handled remote_socket.Resolve in 0.0028
DEBUG 2015-10-09 11:07:31,728 api_server.py:277] Handled remote_socket.CreateSocket in 0.0009
DEBUG 2015-10-09 11:07:32,049 api_server.py:277] Handled remote_socket.Connect in 0.3168
DEBUG 2015-10-09 11:07:32,055 api_server.py:272] Exception while handling service_name: "remote_socket"
method: "GetSocketOptions"
request: "\n$d15a35d7-d299-43c1-ba76-8bf4107f8850\022\006\010\001\020\003\032\000"
request_id: "aiUMNcTaLS"
Traceback (most recent call last):
File "/home/abc/Downloads/google-appengine/google_appengine/google/appengine/tools/devappserver2/api_server.py", line 247, in _handle_POST
api_response = _execute_request(request).Encode()
File "/home/abc/Downloads/google-appengine/google_appengine/google/appengine/tools/devappserver2/api_server.py", line 186, in _execute_request
make_request()
File "/home/abc/Downloads/google-appengine/google_appengine/google/appengine/tools/devappserver2/api_server.py", line 181, in make_request
request_id)
File "/home/abc/Downloads/google-appengine/google_appengine/google/appengine/api/apiproxy_stub.py", line 131, in MakeSyncCall
method(request, response)
File "/home/abc/Downloads/google-appengine/google_appengine/google/appengine/api/remote_socket/_remote_socket_stub.py", line 56, in WrappedMethod
return method(self, *args, **kwargs)
File "/home/abc/Downloads/google-appengine/google_appengine/google/appengine/api/remote_socket/_remote_socket_stub.py", line 265, in _Dynamic_GetSocketOptions
'Attempt to get blocked socket option.')
ApplicationError: ApplicationError: 5 Attempt to get blocked socket option.
DEBUG 2015-10-09 11:07:32,056 api_server.py:277] Handled remote_socket.GetSocketOptions in 0.0014
INFO 2015-10-09 11:07:32,058 views.py:570] handle_exception
INFO 2015-10-09 21:28:17,317 views.py:559] Traceback (most recent call last):
File "/home/abc/Downloads/google-appengine/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/home/abc/projects/src/views.py", line 806, in get
self._callHandlingMethod(url, self.getRegexps)
File "/home/abc/projects/src/views.py", line 883, in _callHandlingMethod
function(*matched.groups())
File "/home/abc/projects/src/views.py", line 2992, in buy_get
"client_token": braintree.ClientToken.generate(),
File "/home/abc/projects/src/lib/braintree/client_token.py", line 25, in generate
return gateway.generate(params)
File "/home/abc/projects/src/lib/braintree/client_token_gateway.py", line 17, in generate
response = self.config.http().post("/client_token", params)
File "/home/abc/projects/src/lib/braintree/util/http.py", line 49, in post
return self.__http_do("POST", path, params)
File "/home/abc/projects/src/lib/braintree/util/http.py", line 66, in __http_do
status, response_body = http_strategy.http_do(http_verb, full_path, self.__headers(), request_body)
File "/home/abc/projects/src/lib/braintree/util/http.py", line 87, in http_do
timeout=self.config.timeout
File "/home/abc/projects/src/lib/requests/api.py", line 92, in post
return request('post', url, data=data, **kwargs)
File "/home/abc/projects/src/lib/requests/api.py", line 48, in request
return session.request(method=method, url=url, **kwargs)
File "/home/abc/projects/src/lib/requests/sessions.py", line 451, in request
resp = self.send(prep, **send_kwargs)
File "/home/abc/projects/src/lib/requests/sessions.py", line 557, in send
r = adapter.send(request, **kwargs)
File "/home/abc/projects/src/lib/requests/adapters.py", line 407, in send
raise ConnectionError(err, request=request)
ConnectionError: ('Connection aborted.', error(13, 'Permission denied'))
This seemed to be a https issue. I have tried different approaches
a) https://github.com/agfor/braintree-python-appengine . Gave me same error
b) I thought this could be the error from this issue - https://urllib3.readthedocs.org/en/latest/security.html#openssl-pyopenssl
But on updating required libraries , I get stuck at OpenSSL.crypto failed import.
Help anyone!
It looks like braintree is trying to use a socket option unsupported by GAE, you can see a list of supported options here https://cloud.google.com/appengine/docs/python/sockets/ which also states that attempting to get an unsupported option will raise an error
Braintree Version - 3.20.0
Requests version - 2.7.0
With the help of my friend , I used the following hack -
in braintree/util/http.py in the method - __http_do
from google.appengine.api import urlfetch
.....
.....
try:
if http_verb in ["POST", "PUT"]:
result = urlfetch.fetch(url=full_path,
payload=request_body,
method=urlfetch.POST,
headers=self.__headers())
logging.debug('result: %r' % result)
status = result.status_code
response_body = result.content
logging.debug(result.content)
else:
status, response_body = http_strategy.http_do(http_verb, full_path, self.__headers(), request_body)
except Exception as e:
.....
.....
Using this hack, I was able to get things working. Hope this helps anybody coming across the same issue.

Django-allauth - Error Accessing FB User Profile - Max Retries Exceeded

I'm trying to finish the setup of django-allauth for my site (in development).
Using Django==1.6.5 and django-allauth==0.17.0.
After following the documentation, I have been able to get the FB dialog. When I click OK, it hangs on localhost:8000/accounts/facebook/login/token/ for about 2 minutes, before returning with an error. The console is showing:
Error accessing FB user profile
Traceback (most recent call last):
File "/home/amir/claudius/lib/python2.7/site-packages/allauth/socialaccount/providers/facebook/views.py", line 73, in login_by_token
login = fb_complete_login(request, app, token)
File "/home/amir/claudius/lib/python2.7/site-packages/allauth/socialaccount/providers/facebook/views.py", line 26, in fb_complete_login
params={'access_token': token.token})
File "/home/amir/claudius/lib/python2.7/site-packages/requests/api.py", line 55, in get
return request('get', url, **kwargs)
File "/home/amir/claudius/lib/python2.7/site-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/home/amir/claudius/lib/python2.7/site-packages/requests/sessions.py", line 456, in request
resp = self.send(prep, **send_kwargs)
File "/home/amir/claudius/lib/python2.7/site-packages/requests/sessions.py", line 559, in send
r = adapter.send(request, **kwargs)
File "/home/amir/claudius/lib/python2.7/site-packages/requests/adapters.py", line 375, in send
raise ConnectionError(e, request=request)
ConnectionError: HTTPSConnectionPool(host='graph.facebook.com', port=443): Max retries exceeded with url: /me?access_token=CAAUi8RJCRZAkBAPdHFKhckONnLwjOExZCeVXpW39GZAZBLdD5rTsukQqTPi9KP6neMDxtwdhZAQvmzCS92rxR0rIZCNlzenQ2jHiyANvToy6tOWrOh5ZAYFmJFYeYvbXGNc9fuPIa0hAUqGfPzFtZB0tepoxoO7bpt01izuTYBkmS9NJChXaX9iDZAQlDTDvtLTZBvLesjFtSfwp6RusbArRzH (Caused by <class 'socket.error'>: [Errno 101] Network is unreachable)
[26/Jul/2014 06:14:36] "POST /accounts/facebook/login/token/ HTTP/1.1" 200 1205
Anyone knows the cause for this?
Well, I found out that Facebook Graph API requires IPv6 in a PHP post by Etienne Rached, of all places. Tethering through my mobile phone solved it straight away after that for the development process, and this will be a non-issue when our website is deployed.
I hope this will help someone out there.