Created
October 19, 2015 22:17
-
-
Save studiawan/a51876f2be006a572fa9 to your computer and use it in GitHub Desktop.
Simple UDP client with socket timeout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
server_address = ('127.0.0.1', 5001) | |
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
client_socket.connect(server_address) | |
message = 'Hi ...' | |
delay = 1 | |
while True: | |
client_socket.send(message) | |
client_socket.settimeout(delay) | |
try: | |
recv_message = client_socket.recv(1024) | |
print recv_message, 'delay:', delay | |
except socket.timeout: | |
delay *= 2 | |
if delay > 2: | |
print 'Server is down' | |
else: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment