summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorFlorian Apolloner <florian@apolloner.eu>2013-09-22 15:55:09 +0200
committerFlorian Apolloner <florian@apolloner.eu>2013-09-22 22:08:59 +0200
commit18fe77e4ed01a7761a24de7ec7b1c3a1314fc197 (patch)
treebc1ee81404e1b25f1f747f526abe2bb7423b0469 /django
parentb5eddde09523bd55cde1cf8e22d46d3f594503d7 (diff)
[1.5.x] Fixed "Address already in use" from liveserver.
Our WSGIServer rewrapped the socket errors from server_bind into WSGIServerExceptions, which is used later on to provide nicer error messages in runserver and used by the liveserver to see if the port is already in use. But wrapping server_bind isn't enough since it only binds to the socket, socket.listen (which is called from server_activate) could also raise "Address already in use". Instead of overriding server_activate too I chose to just catch socket errors, which seems to make more sense anyways and should be more robust against changes in wsgiref. Backport of 2ca00faa913754cd5860f6e1f23c8da2529c691a from master
Diffstat (limited to 'django')
-rw-r--r--django/core/management/commands/runserver.py15
-rw-r--r--django/core/servers/basehttp.py9
-rw-r--r--django/test/testcases.py9
3 files changed, 13 insertions, 20 deletions
diff --git a/django/core/management/commands/runserver.py b/django/core/management/commands/runserver.py
index 391e0b440a..740764d5bd 100644
--- a/django/core/management/commands/runserver.py
+++ b/django/core/management/commands/runserver.py
@@ -1,12 +1,13 @@
from optparse import make_option
from datetime import datetime
+import errno
import os
import re
import sys
import socket
from django.core.management.base import BaseCommand, CommandError
-from django.core.servers.basehttp import run, WSGIServerException, get_internal_wsgi_application
+from django.core.servers.basehttp import run, get_internal_wsgi_application
from django.utils import autoreload
naiveip_re = re.compile(r"""^(?:
@@ -112,16 +113,16 @@ class Command(BaseCommand):
handler = self.get_handler(*args, **options)
run(self.addr, int(self.port), handler,
ipv6=self.use_ipv6, threading=threading)
- except WSGIServerException as e:
+ except socket.error as e:
# Use helpful error messages instead of ugly tracebacks.
ERRORS = {
- 13: "You don't have permission to access that port.",
- 98: "That port is already in use.",
- 99: "That IP address can't be assigned-to.",
+ errno.EACCES: "You don't have permission to access that port.",
+ errno.EADDRINUSE: "That port is already in use.",
+ errno.EADDRNOTAVAIL: "That IP address can't be assigned-to.",
}
try:
- error_text = ERRORS[e.args[0].args[0]]
- except (AttributeError, KeyError):
+ error_text = ERRORS[e.errno]
+ except KeyError:
error_text = str(e)
self.stderr.write("Error: %s" % error_text)
# Need to use an OS exit because sys.exit doesn't work in a thread
diff --git a/django/core/servers/basehttp.py b/django/core/servers/basehttp.py
index 68ca0c1079..9aed63774d 100644
--- a/django/core/servers/basehttp.py
+++ b/django/core/servers/basehttp.py
@@ -67,10 +67,6 @@ def get_internal_wsgi_application():
return app
-class WSGIServerException(Exception):
- pass
-
-
class ServerHandler(simple_server.ServerHandler, object):
error_status = str("500 INTERNAL SERVER ERROR")
@@ -131,10 +127,7 @@ class WSGIServer(simple_server.WSGIServer, object):
def server_bind(self):
"""Override server_bind to store the server name."""
- try:
- super(WSGIServer, self).server_bind()
- except Exception as e:
- raise WSGIServerException(e)
+ super(WSGIServer, self).server_bind()
self.setup_environ()
diff --git a/django/test/testcases.py b/django/test/testcases.py
index 0941fa09d1..99f979494a 100644
--- a/django/test/testcases.py
+++ b/django/test/testcases.py
@@ -4,6 +4,7 @@ import difflib
import json
import os
import re
+import socket
import sys
from copy import copy
from functools import wraps
@@ -24,8 +25,7 @@ from django.core.handlers.wsgi import WSGIHandler
from django.core.management import call_command
from django.core.management.color import no_style
from django.core.signals import request_started
-from django.core.servers.basehttp import (WSGIRequestHandler, WSGIServer,
- WSGIServerException)
+from django.core.servers.basehttp import WSGIRequestHandler, WSGIServer
from django.core.urlresolvers import clear_url_caches
from django.core.validators import EMPTY_VALUES
from django.db import (transaction, connection, connections, DEFAULT_DB_ALIAS,
@@ -1064,10 +1064,9 @@ class LiveServerThread(threading.Thread):
try:
self.httpd = StoppableWSGIServer(
(self.host, port), QuietWSGIRequestHandler)
- except WSGIServerException as e:
+ except socket.error as e:
if (index + 1 < len(self.possible_ports) and
- hasattr(e.args[0], 'errno') and
- e.args[0].errno == errno.EADDRINUSE):
+ e.errno == errno.EADDRINUSE):
# This port is already in use, so we go on and try with
# the next one in the list.
continue