Check if you can connect to a TCP port in Python

It is used for such purposes as waiting for a host to start.

import sys
import socket
import time
import argparse

parser = argparse.ArgumentParser(description='Check tcp connectivity')
parser.add_argument('--host', dest='host', type=str, help='target host')
parser.add_argument('--port', dest='port', type=int, help='target port')
parser.add_argument('--interval', dest='interval', type=int, default='3', help='interval to check')
parser.add_argument('--retries', dest='retries', type=int, default='3', help='number of retries')
args = parser.parse_args()

def check_tcp_connectivity(host, port, interval=0, retries=1):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    for x in range(retries):
        try:
            s.connect((host, port))
            s.close()
            return True
        except socket.error as e:
            time.sleep(interval)

    s.close()
    return False

if check_tcp_connectivity(args.host, args.port, args.interval, args.retries):
    sys.exit(0)
else:
    sys.exit(1)

use

python check.py --host 192.168.0.1 --port 22

reference

argparse socket time sys

Recommended Posts

Check if you can connect to a TCP port in Python
What to do if you get a minus zero in Python
If you want to assign csv export to a variable in python
Check if the string is a number in python
If you encounter a "Unicode Decode Error" in Python
[Python] What to check when you get a Unicode Decode Error in Django
How to check in Python if one of the elements of a list is in another list
Understand Python yield If you put yield in a function, it will change to a generator
[python] How to check if the Key exists in the dictionary
Why do you add a main ()-if statement in Python?
If you write TinderBot in Python, she can do it
How to import a file anywhere you like in Python
How to check if a value exists in an enum
Create a tool to check scraping rules (robots.txt) in Python
If you know Python, you can make a web application with Django
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
Have python check if the string can be converted / converted to int
Check and receive Serial port in Python (Port check)
Check if the URL exists in Python
How to get a stacktrace in python
How to check opencv version in python
If you want a singleton in python, think of the module as a singleton
Note: [Python3] Convert datetime to a string in any format you like
[Python] If you want to draw a scatter plot of multiple clusters
Connect to postgreSQL from Python and use stored procedures in a loop.
If you want to count words in Python, it's convenient to use Counter.
I made a program to check the size of a file in Python
What happens if you do "import A, B as C" in Python?
If you get a no attribute error in boto3, check the version
Try to calculate a statistical problem in Python
How to clear tuples in a list (Python)
To execute a Python enumerate function in JavaScript
How to embed a variable in a python string
Check if the characters are similar in Python
I want to create a window in Python
How to create a JSON file in Python
You can easily create a GUI with Python
A clever way to time processing in Python
Steps to develop a web application in Python
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
What to do if you get a "No versions found" error in pipenv
A solution if you accidentally remove the python interpreter in / usr / local / bin /.
I tried to create a class that can easily serialize Json in Python
If you want to make a TODO application (distributed) now using only Python
Create a plugin that allows you to search Sublime Text 3 tabs in Python
I want to create a priority queue that can be updated in Python (2.7)
If you want to display values using choices in a template in a Django model
If you guys in the scope kitchen can do it with a margin ~ ♪
What to do if a Unicode Encode Error occurs in Sublime Text Python
What to do if you get "Python not configured." Using PyDev in Eclipse
If you want to make a discord bot with python, let's use a framework
I created a script to check if English is entered in the specified position of the JSON file in Python.
If you want to make a Windows application (exe) that can be actually used now using only Python
I wanted to create a program in Reverse Polish Notation in Python (determining if a string can be converted to a number)
I want to write in Python! (1) Code format check
Create a python script to check if the link at the specified URL is valid 2
Parse a JSON string written to a file in Python
How to convert / restore a string with [] in python
I want to embed a variable in a Python string