Based on the traditional definition of client
and server
, the server has a publicly known address which is known to the client, but the client does not have a publicly known address which is known to the server.
The code you have shown appears to be a mixture of attempted client and server code, but the change you will need to make will be on the client side for the reason described above.
You can simply have the client perform the following steps.
- Attempt to connect to the server.
- If successful , then we are done and continue to talk to the server. If failed, wait for some timeout (maybe 1 second?) and then go back to step 1.
If your question is, "how do I get around the error that the client throws and try again", the answer is to use try-except
.
Also, if the code you showed was supposed to be client code, the correct first line is s=socket.socket()
, not s=socket.accept()
, which will throw an error described by the comment.
For a simple example of working client-server code in Python, see my answer to another question.