Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tiagocoutinho/a4e620bb9c476eb617dd3cf790394244 to your computer and use it in GitHub Desktop.
Save tiagocoutinho/a4e620bb9c476eb617dd3cf790394244 to your computer and use it in GitHub Desktop.
Python TCP reconnection
"""
Example output:
2020-05-05 20:04:47,200 trying to connect to bl04cryocon:5000
2020-05-05 20:04:47,701 error: TimeoutError()
2020-05-05 20:04:47,702 trying to connect to bl04cryocon:5000
2020-05-05 20:04:48,203 error: TimeoutError()
2020-05-05 20:04:48,204 trying to connect to bl04cryocon:5000
2020-05-05 20:04:48,706 error: TimeoutError()
2020-05-05 20:04:48,707 trying to connect to bl04cryocon:5000
2020-05-05 20:04:49,209 error: TimeoutError()
2020-05-05 20:04:49,209 trying to connect to bl04cryocon:5000
2020-05-05 20:04:49,711 error: TimeoutError()
2020-05-05 20:04:49,711 trying to connect to bl04cryocon:5000
2020-05-05 20:04:50,213 error: TimeoutError()
2020-05-05 20:04:50,214 trying to connect to bl04cryocon:5000
2020-05-05 20:04:50,279 error: OSError(113, "Connect call failed ('84.89.224.6', 5000)")
2020-05-05 20:04:50,279 trying to connect to bl04cryocon:5000
2020-05-05 20:04:50,781 error: TimeoutError()
2020-05-05 20:04:50,782 trying to connect to bl04cryocon:5000
2020-05-05 20:04:51,283 error: TimeoutError()
2020-05-05 20:04:51,284 trying to connect to bl04cryocon:5000
2020-05-05 20:04:51,786 error: TimeoutError()
2020-05-05 20:04:51,787 trying to connect to bl04cryocon:5000
2020-05-05 20:04:52,289 error: TimeoutError()
2020-05-05 20:04:52,290 trying to connect to bl04cryocon:5000
2020-05-05 20:04:52,791 error: TimeoutError()
2020-05-05 20:04:52,792 trying to connect to bl04cryocon:5000
2020-05-05 20:04:53,293 error: TimeoutError()
2020-05-05 20:04:53,294 trying to connect to bl04cryocon:5000
2020-05-05 20:04:53,351 error: OSError(113, "Connect call failed ('84.89.224.6', 5000)")
2020-05-05 20:04:53,351 trying to connect to bl04cryocon:5000
2020-05-05 20:04:53,853 error: TimeoutError()
2020-05-05 20:04:53,854 trying to connect to bl04cryocon:5000
"""
import asyncio
import logging
logging.basicConfig(level='INFO', format='%(asctime)s %(message)s')
import async_timeout
async def main(host, port, timeout):
while True:
try:
logging.info('trying to connect to %s:%d', host, port)
async with async_timeout.timeout(timeout):
r, w = await asyncio.open_connection(host, port)
logging.info('connected')
break
except Exception as error:
logging.error('error: %r', error)
asyncio.run(main('bl04cryocon', 5000, 0.5))
"""
Example output:
2020-05-05 19:59:56,780 trying to connect to bl04cryocon:5000
2020-05-05 19:59:59,847 error: OSError(113, "Connect call failed ('84.89.224.6', 5000)")
2020-05-05 19:59:59,847 trying to connect to bl04cryocon:5000
2020-05-05 20:00:02,919 error: OSError(113, "Connect call failed ('84.89.224.6', 5000)")
2020-05-05 20:00:02,920 trying to connect to bl04cryocon:5000
2020-05-05 20:00:05,991 error: OSError(113, "Connect call failed ('84.89.224.6', 5000)")
2020-05-05 20:00:05,991 trying to connect to bl04cryocon:5000
2020-05-05 20:00:09,063 error: OSError(113, "Connect call failed ('84.89.224.6', 5000)")
2020-05-05 20:00:09,063 trying to connect to bl04cryocon:5000
"""
import asyncio
import logging
logging.basicConfig(level='INFO', format='%(asctime)s %(message)s')
async def main(host, port):
while True:
try:
logging.info('trying to connect to %s:%d', host, port)
r, w = await asyncio.open_connection(host, port)
logging.info('connected')
break
except Exception as error:
logging.error('error: %r', error)
asyncio.run(main('bl04cryocon', 5000))
"""
Example output:
2020-05-05 20:01:04,855 trying to connect to bl04cryocon:5000
2020-05-05 20:01:05,356 error: TimeoutError()
2020-05-05 20:01:05,356 trying to connect to bl04cryocon:5000
2020-05-05 20:01:05,858 error: TimeoutError()
2020-05-05 20:01:05,858 trying to connect to bl04cryocon:5000
2020-05-05 20:01:06,360 error: TimeoutError()
2020-05-05 20:01:06,360 trying to connect to bl04cryocon:5000
2020-05-05 20:01:06,862 error: TimeoutError()
2020-05-05 20:01:06,863 trying to connect to bl04cryocon:5000
2020-05-05 20:01:07,365 error: TimeoutError()
2020-05-05 20:01:07,365 trying to connect to bl04cryocon:5000
2020-05-05 20:01:07,867 error: TimeoutError()
2020-05-05 20:01:07,868 trying to connect to bl04cryocon:5000
2020-05-05 20:01:07,911 error: OSError(113, "Connect call failed ('84.89.224.6', 5000)")
2020-05-05 20:01:07,911 trying to connect to bl04cryocon:5000
2020-05-05 20:01:08,412 error: TimeoutError()
2020-05-05 20:01:08,413 trying to connect to bl04cryocon:5000
2020-05-05 20:01:08,914 error: TimeoutError()
2020-05-05 20:01:08,915 trying to connect to bl04cryocon:5000
^C2020-05-05 20:01:08,925 error: CancelledError()
2020-05-05 20:01:08,925 trying to connect to bl04cryocon:5000
2020-05-05 20:01:09,427 error: TimeoutError()
2020-05-05 20:01:09,427 trying to connect to bl04cryocon:5000
"""
import asyncio
import logging
logging.basicConfig(level='INFO', format='%(asctime)s %(message)s')
async def main(host, port, timeout):
while True:
try:
logging.info('trying to connect to %s:%d', host, port)
coro = asyncio.open_connection(host, port)
r, w = await asyncio.wait_for(coro, timeout)
logging.info('connected')
break
except Exception as error:
logging.error('error: %r', error)
asyncio.run(main('bl04cryocon', 5000, 0.5))
"""
Example output:
2020-05-05 19:57:07,755 trying to connect to bl04cryocon:5000
2020-05-05 19:57:08,256 error: timeout('timed out')
2020-05-05 19:57:08,256 trying to connect to bl04cryocon:5000
2020-05-05 19:57:08,757 error: timeout('timed out')
2020-05-05 19:57:08,758 trying to connect to bl04cryocon:5000
2020-05-05 19:57:09,259 error: timeout('timed out')
2020-05-05 19:57:09,259 trying to connect to bl04cryocon:5000
2020-05-05 19:57:09,760 error: timeout('timed out')
2020-05-05 19:57:09,760 trying to connect to bl04cryocon:5000
2020-05-05 19:57:10,261 error: timeout('timed out')
2020-05-05 19:57:10,261 trying to connect to bl04cryocon:5000
2020-05-05 19:57:10,762 error: timeout('timed out')
2020-05-05 19:57:10,762 trying to connect to bl04cryocon:5000
2020-05-05 19:57:10,823 error: OSError(113, 'No route to host')
2020-05-05 19:57:10,823 trying to connect to bl04cryocon:5000
2020-05-05 19:57:11,324 error: timeout('timed out')
2020-05-05 19:57:11,324 trying to connect to bl04cryocon:5000
2020-05-05 19:57:11,825 error: timeout('timed out')
2020-05-05 19:57:11,825 trying to connect to bl04cryocon:5000
2020-05-05 19:57:12,326 error: timeout('timed out')
2020-05-05 19:57:12,326 trying to connect to bl04cryocon:5000
2020-05-05 19:57:12,827 error: timeout('timed out')
2020-05-05 19:57:12,828 trying to connect to bl04cryocon:5000
2020-05-05 19:57:13,329 error: timeout('timed out')
2020-05-05 19:57:13,329 trying to connect to bl04cryocon:5000
"""
import socket
import logging
logging.basicConfig(level='INFO', format='%(asctime)s %(message)s')
def connect(host, port, timeout=None):
sock = socket.socket()
if timeout is not None:
sock.settimeout(timeout)
sock.connect((host, port))
return sock
def main(host, port, timeout):
while True:
try:
logging.info('trying to connect to %s:%d', host, port)
conn = connect(host, port, timeout)
logging.info('connected')
return conn
except Exception as error:
logging.error('error: %r', error)
main('bl04cryocon', 5000, 0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment