@@ -76,6 +76,17 @@ def _format_pipe(fd):
76
76
return repr (fd )
77
77
78
78
79
+ def _set_reuseport (sock ):
80
+ if not hasattr (socket , 'SO_REUSEPORT' ):
81
+ raise ValueError ('reuse_port not supported by socket module' )
82
+ else :
83
+ try :
84
+ sock .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEPORT , 1 )
85
+ except OSError :
86
+ raise ValueError ('reuse_port not supported by socket module, '
87
+ 'SO_REUSEPORT defined but not implemented.' )
88
+
89
+
79
90
# Linux's sock.type is a bitmask that can include extra info about socket.
80
91
_SOCKET_TYPE_MASK = 0
81
92
if hasattr (socket , 'SOCK_NONBLOCK' ):
@@ -873,17 +884,7 @@ def create_datagram_endpoint(self, protocol_factory,
873
884
sock .setsockopt (
874
885
socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
875
886
if reuse_port :
876
- if not hasattr (socket , 'SO_REUSEPORT' ):
877
- raise ValueError (
878
- 'reuse_port not supported by socket module' )
879
- else :
880
- try :
881
- sock .setsockopt (
882
- socket .SOL_SOCKET , socket .SO_REUSEPORT , 1 )
883
- except OSError :
884
- raise ValueError ((
885
- 'reuse_port not supported by socket module, '
886
- 'SO_REUSEPORT defined but not implemented.' ))
887
+ _set_reuseport (sock )
887
888
if allow_broadcast :
888
889
sock .setsockopt (
889
890
socket .SOL_SOCKET , socket .SO_BROADCAST , 1 )
@@ -1006,17 +1007,7 @@ def create_server(self, protocol_factory, host=None, port=None,
1006
1007
sock .setsockopt (
1007
1008
socket .SOL_SOCKET , socket .SO_REUSEADDR , True )
1008
1009
if reuse_port :
1009
- if not hasattr (socket , 'SO_REUSEPORT' ):
1010
- raise ValueError (
1011
- 'reuse_port not supported by socket module' )
1012
- else :
1013
- try :
1014
- sock .setsockopt (
1015
- socket .SOL_SOCKET , socket .SO_REUSEPORT , 1 )
1016
- except OSError :
1017
- raise ValueError ((
1018
- 'reuse_port not supported by socket module, '
1019
- 'SO_REUSEPORT defined but not implemented.' ))
1010
+ _set_reuseport (sock )
1020
1011
# Disable IPv4/IPv6 dual stack support (enabled by
1021
1012
# default on Linux) which makes a single socket
1022
1013
# listen on both address families.
0 commit comments