Django and Selenium Web testing error: [Errno 10054] - django

I'm running some basic functional web test with the Selenium web driver and I'm noticing this error in two of my functional web test cases. The test cases all pass at the end but I get this in the console:
Exception happened during processing of request from ('127.0.0.1', 1169)
data = self._sock.recv(self._rbufsize)
error: [Errno 10054] An existing connection was forcibly closed by the remote host
Traceback (most recent call last):
File "C:\dev\django-projects\barbwire\venv\lib\site-packages\django\test\testcases.py", line 981, in _handle_request_noblock
self.process_request(request, client_address)
File "C:\Python27\Lib\SocketServer.py", line 310, in process_request
self.finish_request(request, client_address)
File "C:\Python27\Lib\SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\dev\django-projects\barbwire\venv\lib\site-packages\django\core\servers\basehttp.py", line 139, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "C:\Python27\Lib\SocketServer.py", line 638, in __init__
self.handle()
File "C:\Python27\Lib\wsgiref\simple_server.py", line 116, in handle
self.raw_requestline = self.rfile.readline()
File "C:\Python27\Lib\socket.py", line 447, in readline
data = self._sock.recv(self._rbufsize)
error: [Errno 10054] An existing connection was forcibly closed by the remote host
Traceback (most recent call last):
File "C:\dev\django-projects\barbwire\venv\lib\site-packages\django\test\testcases.py", line 981, in _handle_request_noblock
self.process_request(request, client_address)
File "C:\Python27\Lib\SocketServer.py", line 310, in process_request
self.finish_request(request, client_address)
File "C:\Python27\Lib\SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\dev\django-projects\barbwire\venv\lib\site-packages\django\core\servers\basehttp.py", line 139, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "C:\Python27\Lib\SocketServer.py", line 638, in __init__
self.handle()
File "C:\Python27\Lib\wsgiref\simple_server.py", line 116, in handle
self.raw_requestline = self.rfile.readline()
File "C:\Python27\Lib\socket.py", line 447, in readline
data = self._sock.recv(self._rbufsize)
error: [Errno 10054] An existing connection was forcibly closed by the remote host
----------------------------------------
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 1170)
----------------------------------------
Destroying test database for alias 'default'...
Here's an example of one of the test cases:
def test_can_join_main_site(self):
self.browser.get(self.live_server_url)
self.browser.find_element_by_link_text('Register').click()
time.sleep(5)
self.browser.find_element_by_name('submit').click()
time.sleep(5)
It runs to completion but dumps the above exception. The idea is to test a simple registration page. After clicking the submit button the page re-displays and prompts the user to fill out additional form fields. As expected and everything appears to work correctly but why the errors? Am I missing something?

Replacing localhost with 127.0.0.1 did not work for me, and adding sleep just slows the test down. I was able to get rid of the error by calling refresh before quitting the browser:
from selenium import webdriver
browser = webdriver.Firefox()
# do stuff with browser
browser.refresh()
browser.quit()
In my case it was the loading of static files in <link> and <script> tags that was causing the problem. But it went away when I added refresh before quitting.

Using FireFox, I was able to resolve this by sufficiently slowing the shutdown process:
#classmethod
def tearDownClass(cls):
time.sleep(3)
cls.selenium.quit()
time.sleep(3)
super(TestClass, cls).tearDownClass()

I fixed the problem by replacing localhost with 127.0.0.1 in the URL:
url = self.live_server_url
url = url.replace('localhost', '127.0.0.1')
self.driver.get('%s%s' % (url, reverse('whatever')))
I found the solution here: https://code.djangoproject.com/ticket/15178

Django Debug Toolbar generates tons of these errors. uninstall it and try again

Related

what should be the best way to move my django rest app to https

I am using https://github.com/teddziuba/django-sslserver as an https server for my django-rest app. it works fine if I test it on admin or in chrome's api client(when admin runs on chrome as well). But whenever I try to test it from firefox, I get the following exception:
Exception happened during processing of request from ('127.0.0.1', 51984)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/home/diwakar/virtualenvs/everybillapienv/local/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 102, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "/usr/lib/python2.7/SocketServer.py", line 649, in __init__
self.handle()
File "/home/diwakar/virtualenvs/everybillapienv/local/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 167, in handle
self.raw_requestline = self.rfile.readline(65537)
File "/usr/lib/python2.7/socket.py", line 476, in readline
data = self._sock.recv(self._rbufsize)
File "/usr/lib/python2.7/ssl.py", line 341, in recv
return self.read(buflen)
File "/usr/lib/python2.7/ssl.py", line 260, in read
return self._sslobj.read(len)
SSLError: [Errno 1] _ssl.c:1429: error:14094418:SSL routines:SSL3_READ_BYTES:tlsv1 alert unknown ca
Should I use that csrf_exempt decorator to get rid of this exception ? my app only have a mobile client, it would never have an browser/javascript client for sure.
How strong is django-sslserver ? Can I use it on production ?
What should be the easiest way to redirect http traffic to https here, I've heard a combination of easy-userena and django-privatebeta does it. Any suggestions ?

Error 32: Running multiple Django sites using 'python manage.py runserver' in Gunicorn-Nginx

I have some Django sites running on a Nginx-Gunicorn server. But at some point I am getting Broken Pipe errors as below
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 582, in process_request_thread
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/root/djangoapps/env/mmiradio/local/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 143, in _init_
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "/usr/lib/python2.7/SocketServer.py", line 640, in _init_
self.finish()
File "/usr/lib/python2.7/SocketServer.py", line 693, in finish
self.wfile.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
I have 'python manage.py runserver' in my gunicorn config for each site, so doubts whether that causes the problem ? As official Django says NEVER use it on production server.
You are running two servers. See here:
https://docs.djangoproject.com/en/dev/ref/django-admin/#runserver-port-or-address-port
The runserver command is just a convenient little server for use in development.

Django shows an error after close IE10

I'm getting this error below after set SESSION_EXPIRE_AT_BROWSER_CLOSE = True on my settings.py. I can open and login and logout with no errors...but if I close before login I get this error. Is there a way to fix it?
Thanks in advance!
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 63792)
Traceback (most recent call last):
File "c:\Python27\lib\SocketServer.py", line 582, in process_request_thread
self.finish_request(request, client_address)
File "c:\Python27\lib\SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "c:\Python27\lib\site-packages\django\core\servers\basehttp.py", line 150
, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "c:\Python27\lib\SocketServer.py", line 638, in __init__
self.handle()
File "c:\Python27\lib\wsgiref\simple_server.py", line 116, in handle
self.raw_requestline = self.rfile.readline()
File "c:\Python27\lib\socket.py", line 447, in readline
data = self._sock.recv(self._rbufsize)
error: [Errno 10054] An existing connection was cancelled by the remote host
----------------------------------------

Django REST Framework POST data with file error (using ModelResource)

I have a really big problem trying to post data with file in a Django REST Framework app. I've created a simple application by the example at djangorestframework website. So I have the urls file:
class MyImageResource(ModelResource):
model = Image
and in urlpatters:
url(r'^image/$', ListOrCreateModelView.as_view(resource=MyImageResource)),
url(r'^image/(?P<pk>[^/]+)/$', InstanceModelView.as_view(resource=MyImageResource)),
The Image model is simple:
class Image(models.Model):
image = models.ImageField(upload_to=get_file_path)
name = models.CharField(max_length=256, blank=True)
description = models.TextField(blank=True)
Testing the REST page in browser, works perfect. Even posting data with files.
My problem is that I want to create a simple python application to post data. I've used simple urllib2, but I get 500 Internal Error or 400 Bad Request:
poza = open('poza.jpg', 'rb')
initial_data = (
{'name', 'Imagine de test REST'},
{'description', 'Dude, this is awesome'},
{'image', poza},
)
d = urllib.urlencode(initial_data)
r = urllib2.Request('http://localhost:8000/api/image/', data=d,
headers={'Content-Type':'multipart/form-data'})
resp = urllib2.urlopen(r)
code = resp.getcode()
data = resp.read()
I've tried also with MultipartPostHandler:
import MultipartPostHandler, urllib2, cookielib
cookies = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies),
MultipartPostHandler.MultipartPostHandler)
params = {
"name":"bob",
"description":"riviera",
"content" : open("poza.jpg", "rb")
}
opener.open("http://localhost:8000/api/image/", params)
but same: 500 or 400 errors and the server (python manage.py runserver) stops with the following errors:
Exception happened during processing of request from ('127.0.0.1', 64879)
Traceback (most recent call last):
File "C:\Python27\lib\SocketServer.py", line 284, in _handle_request_noblock
self.process_request(request, client_address)
File "C:\Python27\lib\SocketServer.py", line 310, in process_request
self.finish_request(request, client_address)
File "C:\Python27\lib\SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Python27\lib\site-packages\django\core\servers\basehttp.py", line 570
, in __init__
BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
File "C:\Python27\lib\SocketServer.py", line 641, in __init__
self.finish()
File "C:\Python27\lib\SocketServer.py", line 694, in finish
self.wfile.flush()
File "C:\Python27\lib\socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] An established connection was aborted by the software in yo
ur host machine
If somebody have, please give me an example of posting data with files or tell me what's wrong in my posting python code. I couldn't find any more examples.
The server looks ok, I can POST data in browser. Thank you very much.
It doesn't look as if you are reading in the file, and are instead passing the file pointer?
Try:
initial_data = (
{'name', 'Imagine de test REST'},
{'description', 'Dude, this is awesome'},
{'image', poza.read()},
)

Django development server not handling ajax request

I am making a simple ajax request but for some reason request.is_ajax return false. I am using jquery and Django development server.
$('#save').click(
function()
{
$.ajax({
type: "POST",
url: "/order/start",
});
});
And in views.py
if request.POST and 'save' in request.POST :
if request.is_ajax()== True:
But, it does not return true, and on runserver i see errors
Exception happened during processing of request from ('127.0.0.1', 1625)
Traceback (most recent call last):
File "c:\python27\lib\SocketServer.py", line 284, in _handle_request_noblock
self.process_request(request, client_address)
File "c:\python27\lib\SocketServer.py", line 310, in process_request
self.finish_request(request, client_address)
File "c:\python27\lib\SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "c:\python27\lib\site-packages\django\core\servers\basehttp.py", line 56
, in __init__
BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
File "c:\python27\lib\SocketServer.py", line 641, in __init__
self.finish()
File "c:\python27\lib\SocketServer.py", line 694, in finish
self.wfile.flush()
File "c:\python27\lib\socket.py", line 301, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] An established connection was aborted by the software in y
ur host machine
I guess that you have the standard middleware enabled and settings.APPEND_SLASH is True (the default), which means that POSTing to "/order/start" automatically redirects to "/order/start/" with a slash, losing the POST in the process.
Make sure the URL in your JS ends with a slash.