Python trick: error Address already in use
Translation of the original in Catalan. Read it in: CatalàCastellano
When we work with sockets in python, often when binding a port, if we kill the process that was bound to the port even if we do it properly, we find that when we launch it again, that is, when we try to bind the port again, that process gives an error, an error that says something like Address already in use. That is, the operating system thinks the port is still being bound by the process I've already killed. If we run a netstat we see that the port isn't there, but even so, until a little while later (some seconds or minutes) there's no way to be able to use that port again.
Well then, a little trick so that doesn't happen and we can use it instantly is to do this after creating the object of the socket class:
# creació de l'objecte
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# afegim noves propietats a l'objecte
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)