Skip to content

Instantly share code, notes, and snippets.

@webgtx
Created April 10, 2023 04:13
Show Gist options
  • Select an option

  • Save webgtx/a24722d5cbb8849351591b6c006b76a5 to your computer and use it in GitHub Desktop.

Select an option

Save webgtx/a24722d5cbb8849351591b6c006b76a5 to your computer and use it in GitHub Desktop.
How to reuse the same address in python sockets?
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # enable address reuse
server.bind((HOST, PORT))
server.listen(5)
print(f"RSHELL Server is running on {HOST}:{PORT}")

By calling setsockopt() on the socket object with the SO_REUSEADDR option, you are telling the operating system to allow the reuse of the same address. This should help you avoid the Address already in use error when restarting your server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment