import socket
import os


def check_port(host, port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    try:
        s.connect((host, port))
        s.shutdown(2)
    except OSError as msg:
        print('port %s is available' % port)
        print(msg)
        return True
    else:
        print('port %s is alrady be in used' % port)
        return False

def release_port(port):
    cmd_find = 'lsof -i tcp:%s' %port
    print(cmd_find)

    result = os.popen(cmd_find).read()

    if str(port) and 'LISTEN' in result:
        if isinstance(result, str):
            index = result.split().index('node')
            pid = result.split()[index+1]

            if pid:
                cmd_kill = 'kill %s' %pid
                os.popen(cmd_kill)
    else:
        print('port %s is available' %port)



if __name__ == '__main__':
    host = '127.0.0.1'
    port = 4723
    release_port(port)
