Having a python program which open UDP socket
receiveSock = socket(AF_INET, SOCK_DGRAM)
receiveSock.bind(("", portReceive))
It sometimes happens that the program fails or I terminate it in running time and it doesn’t reach to
receiveSock.close()
So that at the next time I trying to run this program I get
receiveSock.bind(("",portReceive))
File "<string>", line 1, in bind
socket.error: [Errno 98] Address already in use
How could I close this socket using shell command (or any other useful idea)?
You have two options:
Or, for newer versions of Python:
The
with statementwill close the socket when the block is finished, or the program exits.