Сценарий уведомления Python uTorrent не завершается
Я создал небольшой скрипт на python для отправки уведомления, когда торрент загружается с uTorrent на коробку ubuntu 12.04. Когда я запускаю pgrep -l torrent, я вижу множество скриптов и не могу их убить. Utorrent вызывает скрипт, передавая имя торрента. Сценарий работает нормально и завершается при запуске из терминала, но не закрывается, когда utorrent вызывает его. Я попытался добавить sys.exit() в конец скрипта, но это не остановило процесс и не отправило уведомление.
Что я могу сделать, чтобы закрыть скипет? а также какие-либо идеи, как я могу убить эти процессы? Я пытался убить pkill и htop.
Благодарю.
#!/usr/bin/python
import sys
import os
import socket
def network_notify(message):
host = ['192.168.0.4', '192.168.0.6'] #server IP
port = 50000
size = 1024
title = 'Torrent Downloaded'
for i in host:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((i,port))
except:
None
else:
s.send('\"' + title + '\" \"' +message+'"')
data = s.recv(size)
s.close()
if len(sys.argv) > 1:
name = ' '.join(sys.argv[1:])
network_notify(name)
Вот пгреп
james@Netb$ pgrep -l torrent
27516 torrentNotify.p
27518 torrentNotify.p
27520 torrentNotify.p
27521 torrentNotify.p
27522 torrentNotify.p
27529 torrentNotify.p
27531 torrentNotify.p
27540 torrentNotify.p
27541 torrentNotify.p
27545 torrentNotify.p
27546 torrentNotify.p
27550 torrentNotify.p
27551 torrentNotify.p
27552 torrentNotify.p
27553 torrentNotify.p
27555 torrentNotify.p
27558 torrentNotify.p
27567 torrentNotify.p
27570 torrentNotify.p
27571 torrentNotify.p
27573 torrentNotify.p
27574 torrentNotify.p
28959 torrentNotify.p
28965 torrentNotify.p
28970 torrentNotify.p
1 ответ
Попробуйте это, и когда вы запустите скрипт, убедитесь, что он вызывается. /path/to/script.py >> debug.txt
для сохранения отладки utorrent. Это должно дать вам лучшую обработку ошибок и явно закрыть сокет, когда закончите. Я также добавил небольшой тайм-аут.
Я предполагаю, что сокет - это зависание в части скрипта. вызов shutdown останавливает весь трафик, затем close закроет сокет.
#!/usr/bin/python
import logging
import os
import socket
import sys
def network_notify(name):
hosts = ['192.168.0.4', '192.168.0.6'] #server IP
port = 50000
size = 1024
title = 'Torrent Downloaded'
for host in hosts:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Set the socket to timeout after 10 seconds
s.settimeout(10.0)
s.connect((host, port))
except socket.error, socket.timeout:
logging.error('Something is wrong with host: %s' % host)
continue
# Send the data
s.sendall('"%s" "%s"' % (title, name))
data = s.recv(size)
# Shutdown and close the socket
s.shutdown(socket.SHUT_RDWR)
s.close()
if len(sys.argv) > 1:
name = ' '.join(sys.argv[1:])
network_notify(name)
sys.exit()