define(function() {
"use strict";
function SocketClient(url, opts = {})
{
let $this = this;
this.ws = null;
this.connected = false;
this.url = url;
this._recon_intrv = null;
this._send_intrv = null;
this.options = {
auto_reconnect: true,
reconnect_delay: 4004
};
this._on = {
open: [],
msg: [],
close: [],
connectError: [],
error: []
}
this._onCmd = {};
$.each(opts, function(k, v) {
if ($this.options[k] != undefined) {
$this.options[k] = v;
}
});
this._requests = {};
this._queue = [];
this._busy = false;
this.ready();
};
SocketClient.prototype.ready = function()
{
let $this = this;
if (this._send_intrv) {
clearInterval(this._send_intrv);
}
this._send_intrv = setInterval(function() {
if (!$this._busy && $this._queue.length > 0 && $this.hasConnected()) {
$this._sendWork();
}
}, 100);
};
SocketClient.prototype._sendWork = function()
{
let $this = this,
remove_hanlded = [];
this._busy = true;
$this._queue.forEach(function(data, index) {
if ($this.hasConnected()) {
remove_hanlded.push(index)
$this.ws.send(data);
}
});
for (var i = remove_hanlded.length -1; i>=0; i--) {
$this._queue.splice(remove_hanlded[i], 1);
}
this._busy = false;
};
SocketClient.prototype.hasConnected = function()
{
return this.connected && this.ws && this.ws.readyState === 1;
};
SocketClient.prototype.connect = function()
{
let $this = this;
return new Promise((resolve, reject) => {
if (this.hasConnected()) {
return resolve($this);
}
this.ws = new WebSocket(this.url);
this.ws.onopen = function(event) {
$this.connected = true;
$.each($this._on.open, function(i, callback) {
callback(event);
});
resolve($this);
};
this.ws.onmessage = function(event) {
let data = event.data;
if (event.data.indexOf('{"') === 0) {
data = JSON.parse(event.data);
if (data.cmd && data.request_id && $this._onCmd[data.cmd]) {
$.each($this._onCmd[data.cmd], function(i, callback) {
callback(data.arg);
});
} else if (data.response_id) {
let request = $this._requests[data.response_id];
if (request) {
if (data.status === true) {
request.resolve(data);
} else {
request.reject(data);
}
delete $this._requests[data.response_id];
}
}
}
$.each($this._on.msg, function(i, callback) {
callback(data);
});
};
this.ws.onclose = function(event) {
$this.connected = false;
if (!event.wasClean || event.code == 1011) {
$.each($this._on.connectError, function(i, callback) {
callback(event);
});
if ($this.options.auto_reconnect) {
$this._reconnect();
}
} else {
$.each($this._on.close, function(i, callback) {
callback(event);
});
}
};
this.ws.onerror = function(event) {
if (!$this.connected) {
reject(event);
}
$this.connected = false;
$.each($this._on.error, function(i, callback) {
callback(event);
});
};
});
};
SocketClient.prototype._reconnect = function()
{
let $this = this;
if (this._recon_intrv) {
clearTimeout(this._recon_intrv);
}
return new Promise((resolve, reject) => {
this._recon_intrv = setTimeout(function(Socket) {
$this.connect()
.then(() => {
}).catch((e) => {
});
}, this.options.reconnect_delay);
});
};
SocketClient.prototype.send = function(data)
{
this._queue.push(data);
return this;
};
SocketClient.prototype.cmd = function(cmd, arg = null)
{
let data = {
cmd: cmd,
arg: arg,
request_id: Math.floor(Date.now()*Math.random())
};
return new Promise((resolve, reject) => {
try {
this.send(JSON.stringify(data).replace(/&/g,'%26'));
data.resolve = resolve;
data.reject = reject;
this._requests[data.request_id] = data;
} catch(e) {
reject(e);
}
});
};
SocketClient.prototype.on = function(ev, callback)
{
if (this._on[ev]) {
this._on[ev].push(callback);
}
return this;
};
SocketClient.prototype.onCmd = function(cmd, callback)
{
if (this._onCmd[cmd] == undefined) {
this._onCmd[cmd] = [];
}
this._onCmd[cmd].push(callback);
return this;
};
SocketClient.prototype.close = function()
{
return this.ws.close();
};
return SocketClient;
});
Поэтому тут надо уточнить само задание. Чем мы заняты?Это не одноразовая история, таким запросом будет пользоваться отдел из 6 человек практически каждый день.
@bot.message_handler(content_types=['text'])
def message_test(message):
words = ['накладная', 'не', 'пришла']
for word in words:
if word in message.text:
bot.send_message(message.chat.id, 'Вот И Н С Т Р У К Ц И Я, почитай!')
else:
bot.send_message(message.chat.id, 'Зову людей, пару минут.')
import subprocess, ctypes, os, sys
from subprocess import Popen, DEVNULL
def check_admin():
""" Force to start application with admin rights """
try:
isAdmin = ctypes.windll.shell32.IsUserAnAdmin()
except AttributeError:
isAdmin = False
if not isAdmin:
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
def add_rule(rule_name, file_path):
""" Add rule to Windows Firewall """
subprocess.call(
f"netsh advfirewall firewall add rule name={rule_name} dir=out action=block enable=no program={file_path}",
shell=True,
stdout=DEVNULL,
stderr=DEVNULL
)
print(f"Rule {rule_name} for {file_path} added")
def modify_rule(rule_name, state):
""" Enable/Disable specific rule, 0 = Disable / 1 = Enable """
state, message = ("yes", "Enabled") if state else ("no", "Disabled")
subprocess.call(
f"netsh advfirewall firewall set rule name={rule_name} new enable={state}",
shell=True,
stdout=DEVNULL,
stderr=DEVNULL
)
print(f"Rule {rule_name} {message}")
if __name__ == '__main__':
check_admin()
add_rule("RULE_NAME", "PATH_TO_FILE")
modify_rule("RULE_NAME", 1)
Traceback (most recent call last):
File "c:\Users\kolom\Desktop\Main\Auto_EGAIS\win7_firewall.py", line 39, in <module>
add_rule()
TypeError: add_rule() missing 2 required positional arguments: 'rule_name' and 'file_path'