示例#1
0
    print("Needs IP as arguement")
    sys.exit()

ip = sys.argv[1]


def loadCookie():
    global cookie
    cookie = "NULL"
    if (os.path.isfile("cookie")):
        cookie = open("cookie", "r").read()
        print("Loaded cookie")
    return cookie


socketSemaphore = threading.BoundedSemaphore(1)
cookie = loadCookie()


def saveCookie():
    global cookie
    open("cookie", "w").write(cookie)
    print("saved cookie")


def main():
    postDaemon = threading.Thread(target=postReader, daemon=True)
    postDaemon.start()
    while True:
        m = input()
        reply = sendToSocket(m, ip)
示例#2
0
     download_history = open(
         os.path.join(output_dir, 'download_history.pickle'), 'rb')
     tried_urls = pickle.load(download_history)
     image_md5s = pickle.load(download_history)
     download_history.close()
 except (OSError, IOError):
     tried_urls = []
 if adult_filter:
     adlt = ''
 else:
     adlt = 'off'
 if args.adult_filter_off:
     adlt = 'off'
 elif args.adult_filter_on:
     adlt = ''
 pool_sema = threading.BoundedSemaphore(args.threads)
 if args.search_string:
     fetch_images_from_keyword(pool_sema, args.search_string, output_dir,
                               args.filters, args.limit)
 elif args.search_file:
     try:
         inputFile = open(args.search_file)
     except (OSError, IOError):
         print("Couldn't open file {}".format(args.search_file))
         exit(1)
     for keyword in inputFile.readlines():
         output_sub_dir = os.path.join(output_dir_origin,
                                       keyword.strip().replace(' ', '_'))
         if not os.path.exists(output_sub_dir):
             os.makedirs(output_sub_dir)
         fetch_images_from_keyword(pool_sema, keyword, output_sub_dir,
示例#3
0
文件: tuitang.py 项目: qsx6003/DXGDM
import requests
import threading
thread_lock = threading.BoundedSemaphore(value=10)


# 传入参数得到字典

def get_page(url,lib,start):
    date = {'kw':'{}'.format(lib),'limit':1000,'start':start}
    r = requests.get(url,params=date)
    return r.json()
#解析字典的到图片地址
def jiexi(resul):
    l = []
    urls = resul['data']['object_list']
    for ch in urls:
       l.append(ch['photo']['path'])
    return l
# 输入关键字拿到总共的图片;改变地址 去拿到所有的图片链接
def urls(url,lib):
    l1=[]
    for start in range(0,1000,100):
        page = get_page(url,lib,start)
        l = jiexi(page)
        l1.extend(l)
    return l1

def image(ch,n):

    data = requests.get(ch)
示例#4
0
def run_tests(tests, verbose=1, cluster_queue=None, tot_nprocs=0):
    '''Run tests.

tests: list of tests.
verbose: level of verbosity in output.
cluster_queue: name of cluster system to use.  If None, tests are run locally.
    Currently only PBS is implemented.
tot_nprocs: total number of processors available to run tests on.  As many
    tests (in a LIFO fashion from the tests list) are run at the same time as
    possible without using more processors than this value.  If less than 1 and
    cluster_queue is specified, then all tests are submitted to the cluster at
    the same time.  If less than one and cluster_queue is not set, then
    tot_nprocs is ignored and the tests are run sequentially (default).
'''
    def run_test_worker(semaphore, semaphore_lock, tests, *run_test_args):
        '''Launch a test after waiting until resources are available to run it.

semaphore: threading.Semaphore object containing the number of cores/processors
    which can be used concurrently to run tests.
semaphore.lock: threading.Lock object used to restrict acquiring the semaphore
    to one thread at a time.
tests: list of (serialized) tests to run in this thread.
run_test_args: arguments to pass to test.run_test method.
'''

        # Ensure that only one test attempts to register resources with the
        # semaphore at a time.  This restricts running the tests to a LIFO
        # fashion which is not perfect (we don't attempt to backfill with
        # smaller tests, for example) but is a reasonable and (most
        # importantly) simple first-order approach.
        for test in tests:
            semaphore_lock.acquire()
            # test.nprocs is <1 when program is run in serial.
            nprocs_used = max(1, test.nprocs)
            for i in range(nprocs_used):
                semaphore.acquire()
            semaphore_lock.release()

            test.run_test(*run_test_args)

            for i in range(nprocs_used):
                semaphore.release()

    # Check executables actually exist...
    compat = testcode2.compatibility
    executables = [test.test_program.exe for test in tests]
    executables = compat.compat_set(executables)
    for exe in executables:
        mswin = sys.platform.startswith('win') or sys.platform.startswith(
            'cyg')
        # The test is not reliable if there's an unholy combination of windows
        # and cygwin being used to run the program.  We've already warned the
        # user (in config.set_program_name) that we struggled to find the
        # executable.
        if not os.path.exists(exe) and not mswin:
            err = 'Executable does not exist: %s.' % (exe)
            raise testcode2.exceptions.TestCodeError(err)

    if tot_nprocs <= 0 and cluster_queue:
        # Running on cluster.  Default to submitting all tests at once.
        tot_nprocs = sum(test.nprocs for test in tests)

    if tot_nprocs > 0:
        # Allow at most tot_nprocs cores to be used at once by tests.
        max_test_nprocs = max(test.nprocs for test in tests)
        if max_test_nprocs > tot_nprocs:
            err = (
                'Number of available cores less than the number required by '
                'the largest test: at least %d needed, %d available.' %
                (max_test_nprocs, tot_nprocs))
            raise testcode2.exceptions.TestCodeError(err)

        # Need to serialize tests that run in the same directory with wildcard
        # patterns in the output file--otherwise we can't figure out which
        # output file belongs to which test.  We might be able to for some
        # wildcards, but let's err on the side of caution.
        wildcards = re.compile('.*(\*|\?|\[.*\]).*')
        serialized_tests = []
        test_store = {}
        for test in tests:
            if test.output and wildcards.match(test.output):
                if test.path in test_store:
                    test_store[test.path].append(test)
                else:
                    test_store[test.path] = [test]
            else:
                serialized_tests.append([test])
        for (key, stests) in test_store.items():
            if (len(stests) > 1) and verbose > 2:
                print('Warning: cannot run tests in %s concurrently.' %
                      stests[0].path)
        serialized_tests += test_store.values()

        semaphore = threading.BoundedSemaphore(tot_nprocs)
        slock = threading.Lock()
        jobs = [
            threading.Thread(target=run_test_worker,
                             args=(semaphore, slock, test, verbose,
                                   cluster_queue, os.getcwd()))
            for test in serialized_tests
        ]
        for job in jobs:
            # daemonise so thread terminates when master dies
            try:
                job.setDaemon(True)
            except AttributeError:
                job.daemon = True
            job.start()

        # We avoid .join() which is blocking making it unresponsive to TERM
        while threading.activeCount() > 1:
            time.sleep(0.5)
    else:
        # run straight through, one at a time
        for test in tests:
            test.run_test(verbose, cluster_queue, os.getcwd())
示例#5
0
import flask as f
import samehadaku as s
import time
import base64 as b64
import requests
import threading

app = f.Flask(__name__, template_folder='.')
app.cache = {}
app.init_time = time.time()
app.bounded_semaphore = threading.BoundedSemaphore(12)
app.client_bounded_semaphore_list = {}


@app.before_request
def br():
    if time.time() - app.init_time >= 2*60*60:
        app.cache = {}
        app.init_time = time.time()
    if f.request.endpoint in ['query', 'get_dl']:
        ip_addr = f.request.remote_addr
        if ip_addr not in app.client_bounded_semaphore_list:
            app.client_bounded_semaphore_list[ip_addr] = threading.BoundedSemaphore(
                3)
        app.client_bounded_semaphore_list[ip_addr].acquire()


@app.after_request
def ar(resp):
    try:
        ip_addr = f.request.remote_addr
示例#6
0
from flask import Flask
from flask_restful import Resource, Api
import boto3
import requests
import time, threading
from threading import Thread
import os
import logging
import json

logging.basicConfig(level=logging.INFO,
                    format="%(asctime)s - %(levelname)s - %(message)s")

max_number_of_threads = 15
thread_limiter = threading.BoundedSemaphore(max_number_of_threads)


def rate_limitter(max_per_second: int):
    """Limits rate of function call to `max_per_second`."""
    min_interval = 1.0 / float(max_per_second)
    lock = threading.Lock()

    def decorate(func):
        last_time_called = [0.0]

        def function_call(args, **kargs):
            lock.acquire()
            elapsed = time.perf_counter() - last_time_called[0]
            wait = min_interval - elapsed
            if wait > 0:
                time.sleep(wait)
示例#7
0
            str(self.poli) + " page_size:" + str(self.page) + " tlb_size:" +
            str(self.tlb) + " total_handled:" + str(total_handled) +
            " total_misses:" + str(total_misses) + " total_hits:" +
            str(total_hits) + " miss_rate:" + misses_rate)

        data.append(
            str(self.pro) + " " + str(self.ins) + " " + str(self.tlb) + " " +
            str(self.poli) + " " + str(self.page) + " " + str(misses_rate))

        threadLock.release()
        threadLimiter.release()


if __name__ == "__main__":
    threadLock = threading.Lock()
    threadLimiter = threading.BoundedSemaphore(12)

    for pro in program:
        for ins in ins_size:
            for tlb in tlb_size:
                for p in policy:
                    for page in page_size:
                        t = mythread(p, page, tlb, pro, ins)
                        t.start()
                        t.join()

    i = len(page_size) * len(tlb_size) * len(policy)
    j = 0
    filedata = {}
    misses_rate = []
    for dat in data:
示例#8
0
from pytube import YouTube
from moviepy.editor import *
import argparse
import moviepy.editor as mp
import re
import os
from tkinter import Tk
import threading
import time

root = Tk()
linkBuffer = []
semaphor = threading.Semaphore(0)
MAX_YOUTUBE_DL_THREADS = 5
maxthreads = threading.BoundedSemaphore(MAX_YOUTUBE_DL_THREADS)


parser = argparse.ArgumentParser(description='Process the file')
parser.add_argument('--targetd',type=str,help='target directory to  place the grabbed  files',required=True)
args = parser.parse_args()

def getClipboard():
    x = root.clipboard_get()
    return x

def watch_clipboard():
    print("creating clipboard module.....")
    new = getClipboard()
    while True:

        old = getClipboard()
示例#9
0
import time as t

number = 0
lock = td.Lock()


def hello():
    print("hello, world")


def plug(lk):
    global number
    with lk:
        for _ in range(1000000):
            number += 1
        print("子线程%s运算结束后,number = %s" % (td.current_thread().getName(), number))
        # print("run the thread: %s" % n)


if __name__ == '__main__':
    semaphore = td.BoundedSemaphore(1)
    for i in range(6):
        d = td.Thread(target=plug, args=(lock,))
        # d = td.Thread(target=plug, args=(i, semaphore))
        d.start()

    ts = Timer(3, hello)
    ts.start()
    # t.sleep(2)
    # print("主线程执行完毕后,number = ", number)
示例#10
0
 def __init__(self, host, ports):
     self.threads = 25
     self.host = host
     self.ports = ports
     self.lock = threading.BoundedSemaphore(value=self.threads)
示例#11
0
    def _prepare_appcache(self) -> None:
        dbname = os.path.join(config.APPCACHE,
                              os.path.basename(self.script_name))

        self.__appcache = shelve.open(dbname, flag="c", writeback=False)
        self.__appcache_mutex = threading.BoundedSemaphore(value=1)
示例#12
0
 def __init__(self):
     self.client = connector.ZVMConnector(connection_type='socket',
                                          ip_addr=CONF.sdkserver.bind_addr,
                                          port=CONF.sdkserver.bind_port)
     self.dd_semaphore = threading.BoundedSemaphore(
         value=CONF.wsgi.max_concurrent_deploy_capture)
示例#13
0
文件: make.py 项目: emuflight/imu-f
asm_command = "arm-none-eabi-gcc -c {USECOLOR} -o output/{OUTPUT_FILE} {ASMFLAGS} {INPUT_FILE}"

compile_command = "arm-none-eabi-gcc -c {USECOLOR} -o output/{OUTPUT_FILE} {CFLAGS} {INPUT_FILE}"

link_command = "arm-none-eabi-gcc {USECOLOR} -o output/{OUTPUT_NAME}.elf {OBJS} {LDFLAGS}"

size_command = "arm-none-eabi-size output/{OUTPUT_NAME}.elf"

copy_obj_command = "arm-none-eabi-objcopy -O binary output/{OUTPUT_NAME}.elf output/{OUTPUT_NAME}.bin"

#excluded_files = [
#    ".*_template.c",
#]

THREAD_LIMIT = args.threads
threadLimiter = threading.BoundedSemaphore(THREAD_LIMIT)
locker = threading.Lock()
threadRunning = list()
isStop = False


def find_between(s, first, last):
    try:
        start = s.index(first)
        end = s.index(last, start)
        return s[start:end]
    except ValueError:
        return ""


class CommandRunnerThread(threading.Thread):
示例#14
0
if args.nocerts:
    s = ssl.wrap_socket(s, cert_reqs=ssl.CERT_NONE)
else:
    s = ssl.wrap_socket(s,
                        keyfile=args.key,
                        certfile=args.cert,
                        ca_certs=args.rootca,
                        cert_reqs=ssl.CERT_REQUIRED)
s.connect((args.host, args.port))

print s.getpeercert()

lastRequestTime = 0.0

# Allow only one transaction at a time
transactSema = threading.BoundedSemaphore(1)


def outputPrinter(s):
    """Print the output in a separate thread."""

    try:
        while True:
            n = s.recv(1)
            if n == "":
                raise RuntimeError("socket connection broken")
            chunks = []
            received = 0
            n = ord(n)
            while received < n:
                chunk = s.recv(n - received)
示例#15
0
import threading, time

def run(n):
    semaphore.acquire()  #调用acquire时计数器-1
    time.sleep(2.5)
    print("run the thread: %s\n" % n)
    semaphore.release()   #调用release时计数器+1

num = 0
semaphore = threading.BoundedSemaphore(2)  # 最多允许5个线程同时运行
for i in range(20):
    t = threading.Thread(target=run, args=(i,))
    t.start()

while threading.active_count() != 1:
    pass
    # print(threading.active_count())
else:
    print('----all threads done---')
    #print(num)
示例#16
0
parser.add_argument('--verbose',
                    help="Verbose output for checking of commands",
                    action='store_true')
parser.add_argument(
    '--threads',
    help="Number of connection threads when checking file of IPs (default 10)",
    default="10")

args = parser.parse_args()
ip = args.ip
filename = args.file
net = args.net
timeout = args.timeout
verbose = args.verbose
num_threads = int(args.threads)
semaphore = threading.BoundedSemaphore(value=num_threads)
print_lock = threading.Lock()


def calculate_doublepulsar_xor_key(s):
    x = (2 * s ^ (((s & 0xff00 | (s << 16)) << 8) |
                  (((s >> 16) | s & 0xff0000) >> 8)))
    x = x & 0xffffffff  # this line was added just to truncate to 32 bits
    return x


def print_status(ip, message):
    global print_lock

    with print_lock:
        print "[*] [%s] %s" % (ip, message)
示例#17
0
import logging
import multiprocessing
import os
import time
import threading

from . import style, util, linter as linter_module

logger = logging.getLogger(__name__)

WILDCARD_SYNTAX = '*'
MAX_CONCURRENT_TASKS = multiprocessing.cpu_count() or 1

task_count = count(start=1)
counter_lock = threading.Lock()
process_limit = threading.BoundedSemaphore(MAX_CONCURRENT_TASKS)


def lint_view(linters, view, view_has_changed, next):
    """Lint the given view.

    This is the top level lint dispatcher. It is called
    asynchronously.
    """
    lint_tasks = get_lint_tasks(linters, view, view_has_changed)

    run_concurrently(
        partial(run_tasks, tasks, next=partial(next, linter))
        for linter, tasks in lint_tasks)

示例#18
0
 def __init__(self, iterable=dict()):
     # s는 semaphore
     self.d = iterable
     self.s = threading.BoundedSemaphore(1)  # s의 초기값 1, 항상 0 <= s <= 1
        file_list.extend(gfile.Glob(file_glob))
    if not file_list:
        print('No files found')
        continue

    #create sub-directory in destination
    sub_folder = os.path.join(FLAGS.dest_image_dir, dir_name)
    if not gfile.Exists(sub_folder):
        try:
            tf.io.gfile.mkdir(sub_folder)
        except:
            print("Could not create directory '" + sub_folder + "'",
                  file=sys.stderr)
            continue

    semaphore = threading.BoundedSemaphore(FLAGS.cores)

    for file_name in file_list:
        base_name = os.path.basename(file_name)
        print("Scaling file '" + base_name + "'")
        colorImage = Image.open(
            os.path.abspath(os.path.join(FLAGS.src_image_dir, file_name)))
        prefix_name = os.path.abspath(
            os.path.join(FLAGS.dest_image_dir, dir_name,
                         os.path.splitext(base_name)[0]))
        file_extention = os.path.splitext(file_name)[1]

        semaphore.acquire()  #decrease counter

        #scale image size
        t = threading.Thread(target=tranform_data,
import zipfile
'''
使用方法:
urls.txt用于存放目标HOST,然后直接运行此脚本即可 python POC.py
漏洞验证成功的目标存放于success.txt,连接失败的错误信息存放于error.txt中
'''

#消除安全请求的提示信息,增加重试连接次数
urllib3.disable_warnings()
requests.adapters.DEFAULT_RETRIES = 4
s = requests.session()
s.keep_alive = False  #关闭连接,防止出现最大连接数限制错误
urllib3.util.ssl_.DEFAULT_CIPHERS += 'HIGH:!DH:!aNULL'  #openssl 拒绝短键,防止SSL错误

# 设置最大线程数
thread_max = threading.BoundedSemaphore(value=150)

#HTTP请求-head头
headers = {
    'User-Agent':
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 12_10) AppleWebKit/600.1.25 (KHTML, like Gecko) Version/12.0 Safari/1200.1.25',
}

proxies = {
    'http': 'socks5://127.0.0.1:1081',
    'https': 'socks5://127.0.0.1:1081'
}

targets = []  #定义目标列表
threads = []  #定义线程池
示例#21
0
import threading, time


def run(n):
    semaphore.acquire()
    time.sleep(1)
    print("run the thread:%s\n" % n)
    semaphore.release()


if __name__ == '__main__':
    num = 0
    semaphore = threading.BoundedSemaphore(3)
    for i in range(20):
        t = threading.Thread(target=run, args=(i, ))
        t.start()

    # while threading.active_count() != 1:
    #     print(threading.active_count())
    # else:
    #     print('-----all threads done----')
    #     print(num)
示例#22
0
# Output:
# 0

# Semaphore
# 管理一个内置的计数器, 每当调用acquire()时-1, 调用release()时+1
# 计数器不能小于0; 当计数器为0时, acquire()将阻塞线程至同步锁定状态, 直到其他线程调用
# release()

# 注意: 同时acquire semaphore的线程仍然可能会有race condition

# BoundedSemaphore
# 在Semaphore的基础上, 不允许计数器超过initial value (设置上限)

# A bounded semaphore with initial value 2
bounded_sema = threading.BoundedSemaphore(value=2)


def func() -> None:
    """
    Dummy function.
    :return: None
    """
    thread_name = threading.current_thread().name
    # 请求Semaphore, 成功后计数器-1
    print('{th_name} acquiring semaphore...'.format(th_name=thread_name))
    # Note that BoundedSemaphore objects can be used in a traditional way, i.e.,
    # via acquire() and release() methods, but it can also simply be used as a
    # context manager, as a syntax sugar
    with bounded_sema:  # 释放Semaphore的时候, 计数器+1
        print('{th_name} gets semaphore'.format(th_name=thread_name))
示例#23
0
 def __init__(self, subdomains, ports):
     self.subdomains = subdomains
     self.ports = ports
     self.threads = 20
     self.lock = threading.BoundedSemaphore(value=self.threads)
示例#24
0
def _calculate_sha256_internal(src_list, sizes, results):
    total_size = sum(
        size
        for size, result in zip(sizes, results)
        if result is None or isinstance(result, Exception)
    )
    # This controls how many parts can be stored in the memory.
    # This includes the ones that are being downloaded or hashed.
    # The number was chosen empirically.
    s3_max_pending_parts = MAX_CONCURRENCY * 4
    stopped = False

    def get_file_chunks(src, size):
        with open(src.path, 'rb') as file:
            yield from read_file_chunks(file)

            current_file_size = file.tell()
            if current_file_size != size:
                warnings.warn(
                    f"Expected the package entry at {src!r} to be {size} B in size, but "
                    f"found an object which is {current_file_size} B instead. This "
                    f"indicates that the content of the file changed in between when you "
                    f"included this  entry in the package (via set or set_dir) and now. "
                    f"This should be avoided if possible."
                )

    def _process_url(src, size):
        hash_obj = hashlib.sha256()

        generator, exceptions_to_retry = (
            (get_file_chunks(src, size), ())
            if src.is_local() else
            (
                _calculate_hash_get_s3_chunks(s3_context, src, size),
                (ConnectionError, HTTPClientError, ReadTimeoutError)
            )
        )
        try:
            for chunk in generator:
                hash_obj.update(chunk)
                progress_update(len(chunk))
                if stopped:
                    return
        except exceptions_to_retry as e:
            return e
        else:
            return hash_obj.hexdigest()
        finally:
            # We want this generator to be finished immediately,
            # so it finishes its own tasks.
            del generator

    with tqdm(desc="Hashing", total=total_size, unit='B', unit_scale=True, disable=DISABLE_TQDM) as progress, \
         ThreadPoolExecutor() as executor, \
         ThreadPoolExecutor(
             MAX_CONCURRENCY,
             thread_name_prefix='s3-executor',
         ) as s3_executor:
        s3_context = types.SimpleNamespace(
            find_correct_client=with_lock(S3ClientProvider().find_correct_client),
            pending_parts_semaphore=threading.BoundedSemaphore(s3_max_pending_parts),
            executor=s3_executor,
        )
        progress_update = with_lock(progress.update)
        future_to_idx = {
            executor.submit(_process_url, src, size): i
            for i, (src, size, result) in enumerate(zip(src_list, sizes, results))
            if result is None or isinstance(result, Exception)
        }
        try:
            for future in concurrent.futures.as_completed(future_to_idx):
                results[future_to_idx.pop(future)] = future.result()
        finally:
            stopped = True
            while future_to_idx:
                future, idx = future_to_idx.popitem()
                future.cancel()

    return results
示例#25
0
host = 'HOSTBBDD'
userbbdd = 'USERBBDD'
bbddpass = '******'
host2 = 'HOSTBBDD2'
userbbdd2 = 'USERBBDD2'
bbddpass2 = 'PASSBBDD2'
password_read = 'PASS_OF_YOUR_TAGS'

# TIME LOOP
t_end = time.time() + 5  # seconds
switchAccion = 0  # 0 add from repair, 1 remove, 2 defective, 3 piece broked, 4 internal use
switchLoopButton = 0

## THREADS
threads = []
threadLimiter = threading.BoundedSemaphore(
    40)  #Max number of threads 40max mysql
exitLoop = 0


def pintaLCD(mensaje, lcd, color=(0, 0, 0)):
    lcd.set_color(color[0], color[1], color[2])
    lcd.clear()
    lcd.message(mensaje)


def selectEPC(lecturaRFID):  #Receive as string, not int encode to hex
    EPC = ''
    # Extract 96 bits of EPC from read value
    for x in range(18, 42):
        EPC = EPC + lecturaRFID[x]
    return EPC
示例#26
0
    def __init__(self,
                 host_and_ports=[('localhost', 61613)],
                 user=None,
                 passcode=None,
                 prefer_localhost=True,
                 try_loopback_connect=True,
                 reconnect_sleep_initial=0.1,
                 reconnect_sleep_increase=0.5,
                 reconnect_sleep_jitter=0.1,
                 reconnect_sleep_max=60.0,
                 use_ssl=False,
                 ssl_key_file=None,
                 ssl_cert_file=None,
                 ssl_ca_certs=None,
                 ssl_cert_validator=None):
        """
        Initialize and start this connection.

        \param host_and_ports            
                 a list of (host, port) tuples.

        \param prefer_localhost
                 if True and the local host is mentioned in the (host,
                 port) tuples, try to connect to this first

        \param try_loopback_connect    
                 if True and the local host is found in the host
                 tuples, try connecting to it using loopback interface
                 (127.0.0.1)

        \param reconnect_sleep_initial 

                 initial delay in seconds to wait before reattempting
                 to establish a connection if connection to any of the
                 hosts fails.

        \param reconnect_sleep_increase 

                 factor by which the sleep delay is increased after
                 each connection attempt. For example, 0.5 means
                 to wait 50% longer than before the previous attempt,
                 1.0 means wait twice as long, and 0.0 means keep
                 the delay constant.

        \param reconnect_sleep_max

                 maximum delay between connection attempts, regardless
                 of the reconnect_sleep_increase.

        \param reconnect_sleep_jitter

                 random additional time to wait (as a percentage of
                 the time determined using the previous parameters)
                 between connection attempts in order to avoid
                 stampeding. For example, a value of 0.1 means to wait
                 an extra 0%-10% (randomly determined) of the delay
                 calculated using the previous three parameters.

        \param use_ssl

                 connect using SSL to the socket.  This wraps the 
                 socket in a SSL connection.  The constructor will 
                 raise an exception if you ask for SSL, but it can't
                 find the SSL module.

        \param ssl_cert_file

                 The path to a X509 certificate 

        \param ssl_key_file

                 The path to a X509 key file

        \param ssl_ca_certs

                 The path to the a file containing CA certificates
                 to validate the server against.  If this is not set,
                 server side certificate validation is not done. 

        \param ssl_cert_validator

                 Function which performs extra validation on the client
                 certificate, for example checking the returned
                 certificate has a commonName attribute equal to the
                 hostname (to avoid man in the middle attacks)

                 The signature is:
                     (OK, err_msg) = validation_function(cert, hostname)

                 where OK is a boolean, and cert is a certificate structure
                 as returned by ssl.SSLSocket.getpeercert()

        """

        sorted_host_and_ports = []
        sorted_host_and_ports.extend(host_and_ports)

        # If localhost is preferred, make sure all (host, port) tuples
        # that refer to the local host come first in the list
        if prefer_localhost:

            def is_local_host(host):
                return host in Connection.__localhost_names

            sorted_host_and_ports.sort(lambda x, y: (int(is_local_host(y[0])) -
                                                     int(is_local_host(x[0]))))

        # If the user wishes to attempt connecting to local ports
        # using the loopback interface, for each (host, port) tuple
        # referring to a local host, add an entry with the host name
        # replaced by 127.0.0.1 if it doesn't exist already
        loopback_host_and_ports = []
        if try_loopback_connect:
            for host_and_port in sorted_host_and_ports:
                if is_local_host(host_and_port[0]):
                    port = host_and_port[1]
                    if (not ("127.0.0.1", port) in sorted_host_and_ports and
                            not ("localhost", port) in sorted_host_and_ports):
                        loopback_host_and_ports.append(("127.0.0.1", port))

        # Assemble the final, possibly sorted list of (host, port) tuples
        self.__host_and_ports = []
        self.__host_and_ports.extend(loopback_host_and_ports)
        self.__host_and_ports.extend(sorted_host_and_ports)

        self.__recvbuf = ''

        self.__listeners = {}

        self.__reconnect_sleep_initial = reconnect_sleep_initial
        self.__reconnect_sleep_increase = reconnect_sleep_increase
        self.__reconnect_sleep_jitter = reconnect_sleep_jitter
        self.__reconnect_sleep_max = reconnect_sleep_max

        self.__connect_headers = {}
        if user is not None and passcode is not None:
            self.__connect_headers['login'] = user
            self.__connect_headers['passcode'] = passcode

        self.__socket = None
        self.__socket_semaphore = threading.BoundedSemaphore(1)
        self.__current_host_and_port = None

        self.__receiver_thread_exit_condition = threading.Condition()
        self.__receiver_thread_exited = False

        if use_ssl and not ssl:
            print "Raising exception ..."
            raise Exception(
                "SSL connection requested, but SSL library not found.")
        self.__ssl = use_ssl
        self.__ssl_cert_file = ssl_cert_file
        self.__ssl_key_file = ssl_key_file
        self.__ssl_ca_certs = ssl_ca_certs
        self.__ssl_cert_validator = ssl_cert_validator
示例#27
0
class AbstractExternalModule:
    """
    Abstract base of all external modules.
    When you inherit and make new modules, please inherit like
    `class NewLanguage$Filetype($AbstractFiletype, $AbstractLang): ...`.

    Stream `stdout` is set to `DEVNULL` in subprocess
    for all external modules for better performance.

    `originalModulePath` is NOT a real original module path here,
    it's meant to be COPIED module's path. `self.executable` or
    `self.modulePath` will be executed.
    """

    def __init__(self, originalModulePath: Path, fs: TempFileSystem,
                 parameterInfo: Const.ParamInfoList,
                 returnInfo: Const.ReturnInfoType,
                 *args, name: str = "", **kwargs):
        self.originalModulePath = originalModulePath
        self.fs = fs
        self.parameterInfo = parameterInfo
        self.returnInfo = returnInfo
        self.prepared = False
        self.modulePath: Const.OptionalPath = None  # Execution Priority #2
        self.executable: Const.OptionalPath = None  # Execution Priority #1
        self.name = name

    @classmethod
    def generateCompilationArgs(cls, *args, **kwargs) -> Const.ArgType:
        """
        Generate arguments to compile.
        """
        raise NotImplementedError

    @classmethod
    def generateExecutionArgs(cls, *args, **kwargs) -> Const.ArgType:
        """
        Generate arguments to invoke.
        """
        raise NotImplementedError

    @staticmethod
    def replaceSymbols(sourceCodePath: Path, mapping: dict) -> str:
        """
        Read sourcecode and replace symbols by mapping.
        """
        with open(sourceCodePath, "r") as sourceCodeFile:
            template = StringTemplate(sourceCodeFile.read())
        return template.substitute(mapping)

    # Global semaphore for invocation
    globalInvokeSemaphore = threading.BoundedSemaphore()

    @staticmethod
    def invoke(
            args: Const.ArgType, stdin: Path = None, stderr: Path = None,
            timelimit: float = Const.DefaultTimeLimit,
            memorylimit: float = Const.DefaultMemoryLimit,
            cwd: Path = None) -> Const.ExitCode:
        """
        Invoke given args with given stdin, stderr, timelimit and cwd.
        Note that stdin and stderr should be either None or existing file's path.
        Otherwise, it will be `DEVNULL`.
        """

        # Open stdin and stderr, and go
        stdin = open(stdin, "r") \
            if isExistingFile(stdin) else DEVNULL
        stderr = open(stderr, "w") \
            if isExistingFile(stderr) else DEVNULL
        result = Const.ExitCode.GeneralUnintendedFail

        # Make everything to be an absolute path
        for i in range(len(args)):
            if isinstance(args[i], Path):
                args[i] = args[i].absolute()

        # Execute
        try:
            with AbstractExternalModule.globalInvokeSemaphore:
                P = Popen(
                    args, stdin=stdin, stdout=DEVNULL, stderr=stderr,
                    cwd=cwd, encoding='ascii',
                    preexec_fn=limitSubprocessResource(timelimit, memorylimit)
                )
            exitcode = P.wait(60)  # One minute for max
            for ec in Const.ExitCode:
                if ec.value == exitcode or ec.value + 256 == exitcode:
                    result = ec
                    break
        except TimeoutExpired:  # Something went wrong.
            result = Const.ExitCode.Killed
            P.kill()
        finally:  # Close file objects
            logger.debug("Executed \"%s\" with TL = %ds, ML = %gMB, exitcode = %d (%s)",
                         P.args, timelimit, memorylimit, P.returncode, result.name)
            if stdin != DEVNULL:
                stdin.close()
            if stderr != DEVNULL:
                stderr.close()

        # Return exitcode
        return result

    @classmethod
    def generateCode(cls, *args, **kwargs) -> str:
        """
        The most abstract method of `generateCode`.
        Read each child class's abstract method for details.
        """
        raise NotImplementedError

    def preparePipeline(self) -> None:
        """
        The most abstract method of `preparePipeline`.
        Read each child class's abstract method for details.
        At the end of this method, `self.prepared` should be set to True.
        """
        raise NotImplementedError

    def run(self, *args, **kwargs) -> Const.EXOO:
        """
        The most abstract method of `run`.
        This method should return `(ExitCode, outfile, stderr)`.
        Read each child class's abstract method for details.
        """
        raise NotImplementedError
示例#28
0
 def __init__(self):
     self.itemlist = []
     self.totalDuration = 0
     self.processingSemaphore = threading.BoundedSemaphore()
示例#29
0
import WateringSysPubSub as wps
import WateringSysVars
import socket
import threading
import json
import sys
import time
#import traceback

sem_publish_AWS = threading.BoundedSemaphore(1)
sem_cmd_water = threading.BoundedSemaphore(1)
sem_cmd_light = threading.BoundedSemaphore(1)

#Bind sockets
UDPServerSocket_water = socket.socket(family=socket.AF_INET,
                                      type=socket.SOCK_DGRAM)
UDPServerSocket_water.bind(
    (WateringSysVars.localIP, WateringSysVars.localPort_water))

UDPServerSocket_light = socket.socket(family=socket.AF_INET,
                                      type=socket.SOCK_DGRAM)
UDPServerSocket_light.bind(
    (WateringSysVars.localIP, WateringSysVars.localPort_light))


def ServiceCommands(client, userdata, message):
    print("Recevied shadow")
    print(message.payload)
    sem_cmd_water.acquire()  #lock the semaphore to update local vars
    sem_cmd_light.acquire()  #lock the semaphore to update local vars
    data = json.loads(message.payload)
示例#30
0
    def __init__(self, globalData: GlobalData):
        self.fileName = os.path.basename(__file__)

        # get global configured data
        self.globalData = globalData
        self.options = self.globalData.options
        self.nodes = self.globalData.nodes
        self.sensors = self.globalData.sensors
        self.managers = self.globalData.managers
        self.alerts = self.globalData.alerts
        self.sensorAlerts = self.globalData.sensorAlerts
        self.serverComm = self.globalData.serverComm
        self.pins = self.globalData.pins
        self.timeDelayedActivation = self.globalData.timeDelayedActivation
        self.audioOutput = self.globalData.audioOutput
        self.sensorWarningStates = self.globalData.sensorWarningStates

        # lock that is being used so only one thread can update the screen
        self.consoleLock = threading.BoundedSemaphore(1)

        # urwid object that shows the connection status
        self.connectionStatus = None

        # urwid object that shows if the alert system is active
        self.alertSystemActive = None

        # the file descriptor for the urwid callback to update the screen
        self.screenFd = None

        # this is the urwid object of the pin field
        self.pinEdit = None

        # this is the urwid object of the options menu
        self.menuPile = None

        # this is the urwid object of the whole edit part of the screen
        self.editPartScreen = None

        # gives the time in seconds when the screen was unlocked
        # (used to check if it was timed out)
        self.screenUnlockedTime = 0

        # the main render loop for the interactive session
        self.mainLoop = None

        # the final body that contains the left and right part of the screen
        self.finalBody = None

        # the main frame around the final body
        self.mainFrame = None

        # the urwid object of the warning view
        self.warningView = None

        # flag that signalizes if the pin view is shown or not
        self.inPinView = True

        # flag that signalizes if the menu view is shown or not
        self.inMenuView = False

        # flag that signalizes if the warning view is shown or not
        self.inWarningView = False

        # callback function of the action that is chosen during the menu view
        # (is used to show warnings if some sensors are not in
        # the correct state and after confirmation execute the chosen option)
        self.callbackOptionToExecute = None

        # list of sensors that are in the warning state and need user
        # confirmation
        self.sensorsToWarn = list()