diff options
author | Kostyantyn Ovechko <fastinetserver@gmail.com> | 2010-07-30 22:41:21 +0300 |
---|---|---|
committer | Kostyantyn Ovechko <fastinetserver@gmail.com> | 2010-07-30 22:41:21 +0300 |
commit | 7c6f655c2e65eb0644ecec6f1bd508c954862711 (patch) | |
tree | fafb1f2a5baffe65e765b0a05d92b48ad561aa64 /segget/scripts/functions.py | |
parent | Add options: GENERAL_LOG_TIME_FORMAT, ERROR_LOG_TIME_FORMAT and DEBUG_LOG_TIM... (diff) | |
download | idfetch-7c6f655c2e65eb0644ecec6f1bd508c954862711.tar.gz idfetch-7c6f655c2e65eb0644ecec6f1bd508c954862711.tar.bz2 idfetch-7c6f655c2e65eb0644ecec6f1bd508c954862711.zip |
Add [scripting_and_scheduling] section to segget.conf file.
[scripting_and_scheduling]
Segget provides Python scripting functionalyty to support scheduling.
Each time segget tries to start a new connection certain network it calls
a python script (client.py) to accept or reject this connection and
if necessary adjusts its settings.
PYTHON_PATH
Define path to python
Default:
python_path=/usr/bin/python
SCRIPTS_DIR
Define a path to the dir with python scripts. Before establishing connection for
a particular segment via network# segget checks SCRIPTS_DIR.
If SCRIPTS_DIR contains net#.py file, segget will launch schedule() function
from this file to apply settings for connetion and accept or reject this
segment for the moment. net#.py file is a python script file
with a user-writen schedule() function.
It's necessary to import functions before using get("variable"),
set("variable",value), accept_segment() and reject_segment() in schedule().
get() function can obtain values for the following variables:
connection.num, connection.url, connection.max_speed_limit,
network.num, network.mode, network.active_connections_count,
distfile.name, distfile.size, distfile.dld_segments_count,
distfile.segments_count, distfile.active_connections_count,
segment.num, segment.try_num, segment.size, segment.range
set() function can change connection.max_speed_limit, see example:
-----------------EXAMPLE STARTS-----------------
from functions import *
import time;
def schedule():
localtime = time.localtime(time.time());
hour=localtime[3];
# disable downloading distfiles that have size more than 5 000 000 bytes
# from 8-00 to 22-00.
if hour>8 and hour<22 and (get("distfile.size"))>5000000:
print "reject because distfile is too big"
reject_segment()
# set speed limit 50 000 cps for distfiles larger than 1 000 000 bytes
if get("distfile.size")>1000000:
print "limit connection speed"
set(connection.max_speed_limit, 50000)
accept_segment()
-----------------EXAMPLE ENDS-----------------
From example above localtime returns following tuple:
Index Attributes Values
0 tm_year e.i.: 2008
1 tm_mon 1 to 12
2 tm_mday 1 to 31
3 tm_hour 0 to 23
4 tm_min 0 to 59
5 tm_sec 0 to 61 (60 or 61 are leap-seconds)
6 tm_wday 0 to 6 (0 is Monday)
7 tm_yday 1 to 366 (Julian day)
8 tm_isdst -1, 0, 1, -1 means library determines DST
Therefore localtime[3] provides hours.
Segment will be accecpted by default if it was neither accepted nor rejected
during the schedule() function.
sagget saves logs of resulting stdout and stderr in the log folder
separatly for each network. Hence, if there's an error in net3.py file python
error message would be saved to net3_script_stderr.log. Results of print would
be saved in net3_script_stdout.log.
Default:
scripts_dir=./scripts
SCRIPT_SOCKET_PATH
Segget uses AF_UNIX domain sockets for communication with python.
Specify path for the socket on your filesystem.
Default:
script_socket_path=/tmp/segget_script_socket
Diffstat (limited to 'segget/scripts/functions.py')
-rw-r--r-- | segget/scripts/functions.py | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/segget/scripts/functions.py b/segget/scripts/functions.py new file mode 100644 index 0000000..07fe90a --- /dev/null +++ b/segget/scripts/functions.py @@ -0,0 +1,77 @@ +import sys +import socket + +def get(var_name): + print("GET::"+var_name) + client_socket.send ("g<c>"+var_name) + data = client_socket.recv(512) + print "RECIEVED:" , data + #connection.num, + #connection.max_speed_limit, + #network.num, + #network.active_connections_count, + #distfile.size, + #distfile.dld_segments_count, + #distfile.segments_count, + #distfile.active_connections_count, + #segment.num, + #segment.try_num, + #segment.size, + if ((var_name=="connection.url") or (var_name=="distfile.name") or (var_name=="segment.range")): + return data + else: + return int(data) + +def set(var_name,var_value): + var_value_str=str(var_value); + print("SET::"+var_name+"="+var_value_str) + client_socket.send ("s<c>"+var_name+"<n>"+var_value_str) + data = client_socket.recv(512) + print "RECIEVED:" , data + if (data=="o<r>"): + return 0 + else: + return 1 + +def accept_segment(): + print "Accepting segment" + client_socket.send ("a<c>") + client_socket.close() + sys.exit(0) + +def reject_segment(): + print "Rejecting segment" + client_socket.send ("r<c>") + client_socket.close() + sys.exit(0) +# in case users forget to use quotes +class Tconnection: + num="connection.num" + max_speed_limit="connection.max_speed_limit" + url="connection.url" + +class Tnetwork: + num="network.num" + mode="network.mode" + active_connections_count="network.active_connections_count" + +class Tdistfile: + name="distfile.name" + size="distfile.size" + dld_segments_count="distfile.dld_segments_count" + segments_count="distfile.segments_count" + active_connections_count="distfile.active_connections_count" + +class Tsegment: + num="segment.num" + try_num="segment.try_num" + size="segment.size" + range="segment.range" + +connection=Tconnection +network=Tnetwork +distfile=Tdistfile +segment=Tsegment + +client_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) +client_socket.connect("/tmp/segget_script_socket")
\ No newline at end of file |