Patch from Nick Vatamaniuc, fixes #83. Thanks!

This commit is contained in:
Ryan Williams
2011-04-06 10:08:00 -07:00
parent 2dd4148f2c
commit 8b6106eb56
3 changed files with 14 additions and 3 deletions

View File

@@ -73,3 +73,4 @@ Thanks To
* Soren Hansen, finding and fixing issue in subprocess (#77)
* Stefano Rivera, making tests pass in absence of postgres (#78)
* Joshua Kwan, fixing busy-wait in eventlet.green.ssl.
* Nick Vatamaniuc, Windows SO_REUSEADDR patch (#83)

View File

@@ -25,7 +25,7 @@ def listen(addr, family=socket.AF_INET, backlog=50):
"""Convenience function for opening server sockets. This
socket can be used in :func:`~eventlet.serve` or a custom ``accept()`` loop.
Sets SO_REUSEADDR on the socket to save on annoyance.
Sets SO_REUSEADDR on the socket to save on annoyance.
:param addr: Address to listen on. For TCP sockets, this is a (host, port) tuple.
:param family: Socket family, optional. See :mod:`socket` documentation for available families.
@@ -33,7 +33,8 @@ def listen(addr, family=socket.AF_INET, backlog=50):
:return: The listening green socket object.
"""
sock = socket.socket(family, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if sys.platform[:3]=="win":
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(addr)
sock.listen(backlog)
return sock

View File

@@ -2,6 +2,7 @@ import os
import eventlet
from eventlet import event
from eventlet.green import socket
from tests import LimitedTestCase, s2b
certificate_file = os.path.join(os.path.dirname(__file__), 'test_server.crt')
@@ -119,4 +120,12 @@ class TestServe(LimitedTestCase):
client.sendall("echo")
self.assertEquals("echo", client.recv(1024))
def test_socket_reuse(self):
lsock1 = eventlet.listen(('localhost',0))
port = lsock1.getsockname()[1]
def same_socket():
return eventlet.listen(('localhost',port))
self.assertRaises(socket.error,same_socket)
lsock1.close()
assert same_socket()