You'll never be able to force the OS (with any sort of reasonable effort) to release a socket.
Instead you just want to say you don't care with setsockopt
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
This means that the option takes place at the Socket Object Level SOCKET (as opposed to the TCP or something else) and you're setting the Socket Option REUSEADDR (you're telling the OS that it's OK, you really do want to listen here). And finally, you're turning the option on with 1
, rather than off (with 0
).