mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
Issue #21455: Add a default backlog to socket.listen().
This commit is contained in:
parent
2b00c4999d
commit
644b8f52a8
4 changed files with 27 additions and 17 deletions
|
@ -906,12 +906,15 @@ to sockets.
|
||||||
On other platforms, the generic :func:`fcntl.fcntl` and :func:`fcntl.ioctl`
|
On other platforms, the generic :func:`fcntl.fcntl` and :func:`fcntl.ioctl`
|
||||||
functions may be used; they accept a socket object as their first argument.
|
functions may be used; they accept a socket object as their first argument.
|
||||||
|
|
||||||
.. method:: socket.listen(backlog)
|
.. method:: socket.listen([backlog])
|
||||||
|
|
||||||
Listen for connections made to the socket. The *backlog* argument specifies the
|
Enable a server to accept connections. If *backlog* is specified, it must
|
||||||
maximum number of queued connections and should be at least 0; the maximum value
|
be at least 0 (if it is lower, it is set to 0); it specifies the number of
|
||||||
is system-dependent (usually 5), the minimum value is forced to 0.
|
unaccepted connections that the system will allow before refusing new
|
||||||
|
connections. If not specified, a default reasonable value is chosen.
|
||||||
|
|
||||||
|
.. versionchanged:: 3.5
|
||||||
|
The *backlog* parameter is now optional.
|
||||||
|
|
||||||
.. method:: socket.makefile(mode='r', buffering=None, *, encoding=None, \
|
.. method:: socket.makefile(mode='r', buffering=None, *, encoding=None, \
|
||||||
errors=None, newline=None)
|
errors=None, newline=None)
|
||||||
|
|
|
@ -1344,10 +1344,13 @@ class GeneralModuleTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_listen_backlog(self):
|
def test_listen_backlog(self):
|
||||||
for backlog in 0, -1:
|
for backlog in 0, -1:
|
||||||
srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as srv:
|
||||||
|
srv.bind((HOST, 0))
|
||||||
|
srv.listen(backlog)
|
||||||
|
|
||||||
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as srv:
|
||||||
srv.bind((HOST, 0))
|
srv.bind((HOST, 0))
|
||||||
srv.listen(backlog)
|
srv.listen()
|
||||||
srv.close()
|
|
||||||
|
|
||||||
@support.cpython_only
|
@support.cpython_only
|
||||||
def test_listen_backlog_overflow(self):
|
def test_listen_backlog_overflow(self):
|
||||||
|
|
|
@ -84,6 +84,8 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #21455: Add a default backlog to socket.listen().
|
||||||
|
|
||||||
- Issue #21525: Most Tkinter methods which accepted tuples now accept lists too.
|
- Issue #21525: Most Tkinter methods which accepted tuples now accept lists too.
|
||||||
|
|
||||||
- Issue #10744: Fix PEP 3118 format strings on ctypes objects with a nontrivial
|
- Issue #10744: Fix PEP 3118 format strings on ctypes objects with a nontrivial
|
||||||
|
|
|
@ -121,7 +121,7 @@ getpeername() -- return remote address [*]\n\
|
||||||
getsockname() -- return local address\n\
|
getsockname() -- return local address\n\
|
||||||
getsockopt(level, optname[, buflen]) -- get socket options\n\
|
getsockopt(level, optname[, buflen]) -- get socket options\n\
|
||||||
gettimeout() -- return timeout or None\n\
|
gettimeout() -- return timeout or None\n\
|
||||||
listen(n) -- start listening for incoming connections\n\
|
listen([n]) -- start listening for incoming connections\n\
|
||||||
recv(buflen[, flags]) -- receive data\n\
|
recv(buflen[, flags]) -- receive data\n\
|
||||||
recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\
|
recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\
|
||||||
recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\
|
recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\
|
||||||
|
@ -2534,14 +2534,16 @@ info is a pair (hostaddr, port).");
|
||||||
/* s.listen(n) method */
|
/* s.listen(n) method */
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
sock_listen(PySocketSockObject *s, PyObject *arg)
|
sock_listen(PySocketSockObject *s, PyObject *args)
|
||||||
{
|
{
|
||||||
int backlog;
|
/* We try to choose a default backlog high enough to avoid connection drops
|
||||||
|
* for common workloads, yet not too high to limit resource usage. */
|
||||||
|
int backlog = Py_MIN(SOMAXCONN, 128);
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
backlog = _PyLong_AsInt(arg);
|
if (!PyArg_ParseTuple(args, "|i:listen", &backlog))
|
||||||
if (backlog == -1 && PyErr_Occurred())
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
Py_BEGIN_ALLOW_THREADS
|
Py_BEGIN_ALLOW_THREADS
|
||||||
/* To avoid problems on systems that don't allow a negative backlog
|
/* To avoid problems on systems that don't allow a negative backlog
|
||||||
* (which doesn't make sense anyway) we force a minimum value of 0. */
|
* (which doesn't make sense anyway) we force a minimum value of 0. */
|
||||||
|
@ -2556,12 +2558,12 @@ sock_listen(PySocketSockObject *s, PyObject *arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(listen_doc,
|
PyDoc_STRVAR(listen_doc,
|
||||||
"listen(backlog)\n\
|
"listen([backlog])\n\
|
||||||
\n\
|
\n\
|
||||||
Enable a server to accept connections. The backlog argument must be at\n\
|
Enable a server to accept connections. If backlog is specified, it must be\n\
|
||||||
least 0 (if it is lower, it is set to 0); it specifies the number of\n\
|
at least 0 (if it is lower, it is set to 0); it specifies the number of\n\
|
||||||
unaccepted connections that the system will allow before refusing new\n\
|
unaccepted connections that the system will allow before refusing new\n\
|
||||||
connections.");
|
connections. If not specified, a default reasonable value is chosen.");
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -3795,7 +3797,7 @@ static PyMethodDef sock_methods[] = {
|
||||||
{"share", (PyCFunction)sock_share, METH_VARARGS,
|
{"share", (PyCFunction)sock_share, METH_VARARGS,
|
||||||
sock_share_doc},
|
sock_share_doc},
|
||||||
#endif
|
#endif
|
||||||
{"listen", (PyCFunction)sock_listen, METH_O,
|
{"listen", (PyCFunction)sock_listen, METH_VARARGS,
|
||||||
listen_doc},
|
listen_doc},
|
||||||
{"recv", (PyCFunction)sock_recv, METH_VARARGS,
|
{"recv", (PyCFunction)sock_recv, METH_VARARGS,
|
||||||
recv_doc},
|
recv_doc},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue