Python uTorrent通知脚本不会终止



我创建了一个小python脚本,用于在ubuntu 12.04盒子上使用uTorrent下载torrent时发送通知。当我运行pgrep-l torrent时,我可以看到大量的scipt,但无法杀死它们。utorent调用传递torrent名称的脚本。该脚本运行良好,从终端运行时退出,但在utorent调用它时不会关闭。我尝试将sys.exit()添加到脚本底部,但它没有停止进程,也没有停止发送通知。

我能做些什么来关闭scipt?还有任何想法我可以如何杀死这些过程?我试图杀死普基尔和赫托普。

谢谢。

#!/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)

这是pgrep

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

尝试一下,当您运行脚本时,请确保在调用脚本时执行/path/to/script.py >> debug.txt以自动保存调试。这将为您提供更好的错误处理,并在完成时显式关闭套接字。我还添加了一个小的超时。

我猜插座是挂在脚本的一部分。调用shutdown将停止所有流量,然后关闭将关闭套接字。

#!/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()

最新更新