<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket
import re
import sys
import subprocess
import os
import pepi_config


def check_server(address, port):
    # Create a TCP socket
    s = socket.socket()
    s.settimeout(0.1)
    print "Attempting to connect to %s on port %s" % (address, port)
    try:
        s.connect((address, port))
        print "Connected to %s on port %s" % (address, port)
        s.close()
        return True
    except socket.error, e:
        print "Connection to %s on port %s failed: %s" % (address, port, e)
        return False


def sweep_ping(base_address, start=1, stop=255):
    activeips = []
    with open(os.devnull, "wb") as limbo:
        for n in xrange(start, stop):
            ip = base_address + str(n)
            result = subprocess.Popen(["ping", "-c", "1", "-n", "-W", "2", ip],
                                      stdout=limbo, stderr=limbo).wait()
            if result:
                print ip, "inactive"
            else:
                print ip, "active"
                if check_server(ip, pepi_config.port):
                    activeips.append(ip)
    return activeips


if __name__ == '__main__':
    ipList = sweep_ping('192.168.1.')
    if ipList:
        with open("active_cameras.txt", "w") as f:
            [f.write(ip+"\n") for ip in ipList]<commit_msg>Parallelise sweep_ping function<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket
import re
import sys
import subprocess
import os
import pepi_config
import time


def check_server(address, port):
    # Create a TCP socket
    s = socket.socket()
    s.settimeout(0.1)
    print "Attempting to connect to %s on port %s" % (address, port)
    try:
        s.connect((address, port))
        print "Connected to %s on port %s" % (address, port)
        s.close()
        return True
    except socket.error, e:
        print "Connection to %s on port %s failed: %s" % (address, port, e)
        return False


def sweep_ping(base_address, start=1, stop=255):
    activeips = []
    with open(os.devnull, "wb") as limbo:
        for n in xrange(start, stop):
            ip = base_address + str(n)
            result = subprocess.Popen(["ping", "-c", "1", "-n", "-W", "2", ip],
                                      stdout=limbo, stderr=limbo).wait()
            if result:
                print ip, "inactive"
            else:
                print ip, "active"
                if check_server(ip, pepi_config.port):
                    activeips.append(ip)
    return activeips


def parallel_sweep_ping(base_address, start=1, stop=255, verbose=False):
    activeServers = []
    with open(os.devnull, "wb") as limbo:
        runningProcs = []
        for n in xrange(start, stop):
            ip = base_address + str(n)
            procTuple = (ip, subprocess.Popen(["ping", "-c", "1", "-n", "-W", "2", ip], stdout=limbo, stderr=limbo))
            runningProcs.append(procTuple)

        while runningProcs:
            for (procip, proc) in runningProcs:
                retcode = proc.poll()
                if retcode is not None:
                    # Process has completed
                    runningProcs.remove((procip, proc))
                    if retcode == 0:
                        print procip, "active"
                        if check_server(procip, pepi_config.port):
                            activeServers.append(procip)
                    else:
                        if verbose: print procip, "inactive"
            time.sleep(1)
    return activeServers


if __name__ == '__main__':
    ipList = parallel_sweep_ping('192.168.1.')
    if ipList:
        with open("active_cameras.txt", "w") as f:
            [f.write(ip + "\n") for ip in ipList]
{'fraction_non_alphanumeric': 0.12269938650306748, 'fraction_numerical': 0.010669511869831956, 'mean_word_length': 1.856054836252856, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3256226', 'n_tokens_mistral': 1197, 'n_tokens_neox': 1138, 'n_words': 332}